Question
Find all the Prime numbers between a given range.
Prime number is a number that is greater than 1 and divided by 1 or itself only. In other words, prime numbers can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the prime numbers.
Enter the lower range
1
Enter the Upper range
100
Prime Numbers:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
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 PrimeNumberInRange
{
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("Prime Numbers:");
for(i=lowerRange;i< upperRange;i++)
{
/*prime numbers start from 2.Therfore, check the number only if number is greater than 1*/
if(i>1)
{
flag=0;
for(j=2;j< i;j++)
{
/*Checking if i is divisible by j.*/
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
{
System.out.print(i+" ");
}
}
}
}
}
Python
lowerRange=int(input("Enter lower value:"))
upperRange=int(input("Enter upper value:"))
isPrimeNumber=True
print("Prime numbers between",lowerRange,"and",upperRange,":")
for i in range(lowerRange,upperRange):
# Checking if the given number is less than 2
# if it is then it is not a prime number
if(i>1):
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==True):
print(i,end=" ")