All prime number between 1 to n
previous NextExample
import java.util.Scanner;
class Raj
{
public static void main(String[] args) {
int n;
int cn;
int count;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number ");
n=sc.nextInt();
System.out.println("list of prime Number between 1 to "+n+":");
for (int i=1;i<=n ;i++ ) {
cn = i;
count=0;
for (int j=1;j<=cn ;j++ ) {
if (cn%j==0) {
count++;
}
}
if (count==2) {
System.out.println(cn);
}
}
}
}
Output :
Enter the number 10 list of prime number between 1 to 10: 2 3 5 7