Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to print all prime numbers between 1 and 50

807601Jan 7 2008 — edited Jan 9 2008
Hi all,

I'm trying to print out all prime numbers between 1 and 50 using just while and if statements but need some help

Can anyone give me an if statement that will print out the prime numbers.

here is my code so far but its not working correctly
public static void main(String [] args) {
    	
    int x, z;
    	
    x = 1;
    	
    z = 50;
    	
    while (x <= z)
    	{
    	if (x==2 || x/x==1 && x/1==x && x%2!=0 ) {System.out.println(x);}
    		
    	x++;
    		
    	}
Can anyone point out where I'm going wrong.

It might be a while before I reply but all replies will be answered.

Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 6 2008
Added on Jan 7 2008
17 comments
1,303 views