Find Prime Numbers From Given Range by using Java Program
Code:
import java.util.*;
public class PrimeNumber
{
public static void main(String[] args)
{
int n,i,j,k,c=1;
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter The First Number : ");
k = sc.nextInt();
System.out.print("\nEnter The Last Number : ");
n = sc.nextInt();
System.out.println("\nPrime Numbers : ");
for(i=k;i<=n;i++)
{
for(j=2;j<i;j++)
{
if(i%j==0)
{
c=0;
break;
}
else
{
c=1;
}
}
if(c==1)
{
System.out.println("\n"+i);
}
}
System.out.println();
}
}
Output of program:
import java.util.*;
public class PrimeNumber
{
public static void main(String[] args)
{
int n,i,j,k,c=1;
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter The First Number : ");
k = sc.nextInt();
System.out.print("\nEnter The Last Number : ");
n = sc.nextInt();
System.out.println("\nPrime Numbers : ");
for(i=k;i<=n;i++)
{
for(j=2;j<i;j++)
{
if(i%j==0)
{
c=0;
break;
}
else
{
c=1;
}
}
if(c==1)
{
System.out.println("\n"+i);
}
}
System.out.println();
}
}
Output of program:
No comments