Question
Find all the non-Prime numbers between a given range.
Enter the lower range
1
Enter the Upper range
50
Non Prime Numbers:
4 6 8 9 10 12 14 15 16 18 20 21 22 24 25 26 27 28 30 32 33 34 35 36 38 39 40 42 44 45 46 48 49 50
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
Java
Python
Java
import java.util.*;
public class nonPrimeNumberInRange
{
public static void main(String args[])
{
int upperRange=0,lowerRange=0,i=0,j=0,flag=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the lower range");
lowerRange=sc.nextInt();
System.out.println("Enter the Upper range");
upperRange=sc.nextInt();
System.out.println("Non Prime Numbers:");
for(i=lowerRange;i< upperRange;i++)
{
if(i<=1)
{
System.out.print(i+" ");
}
else
{
flag=0;
for(j=2;j< i;j++)
{
/*Checking if i is divisible by j.*/
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==1)
{
System.out.print(i+" ");
}
}
}
}
}
Python
lowerRange=int(input("Enter lower value:"))
upperRange=int(input("Enter upper value:"))
print("non Prime numbers between",lowerRange,"and",upperRange,":")
for i in range(lowerRange,upperRange):
if(i<=1):
print(i, end=" ")
else:
isPrimeNumber = True
for j in range(2,i):
# Checking if the given number is divisible
#by any other number expect 1 and itself
#If it is then it's not a prime number
if(i%j==0):
isPrimeNumber=False
break
if(isPrimeNumber==False):
print(i,end=" ")