we have seen how to test whether a given number is prime or not. The program segment given below uses a for loop to test each number in a given range and print it if it is prime.
class PrimeNumbers100 { public static void main(String args[] ) { int i,j,x,m; System.out.println("Prime Numbers up to 100 are\n"); System.out.print("2"+ " "); System.out.print("3"+ " "); for(i=4;i<=100;i++) { x=0; for(j=2;j<=i-1;j++) { m=i%j; if(m==0) { x=1; break; } } if(x==0) { System.out.print(i + " "); } } } }