Print all Prime numbers between 1 to n using while loop

Java Program to Print Prime numbers between 1 to n using while loop

Java
public class Main
{
	public static void main(String[] args) {
		int n = 10,i=2,j;
		
		while(i<=n){
		    boolean isPrime=true;
		    j=2;
		    while(j*j<=i){
		        if(i%j==0){
		            isPrime=false;
		            break;
		        }
		        j++;
		    }
		    if(isPrime){
		        System.out.println(i+" prime");
		    }else{
		        System.out.println(i+ "not prime");
		    }
		    i++;
		}
	}
}

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.