Click here to Skip to main content
14,303,124 members
Rate this:
Please Sign up or sign in to vote.
See more:
i have done it with for loop. but i want it in if.

What I have tried:

public class PrimeNumDemo3
{
  public static void main(String as[ ])
  {
     int number=6;
	 int varynum=0;
	 
	
	 if(int i=1;i<=number;i++)
       {
         if(num%i==0)
		 {
			varynum++;
		 }
       }
	   if(varynum==2)
	   {
		  System.out.println("number prime"); 
	   }
   
 else
   { 
    System.out.println("nt prime");

  }
 
}
}
Posted
Updated 16-May-17 8:43am
v2
Comments
Member 13203537 16-May-17 6:03am
   
i am a beginner ..so help me plz..just 3 days of java learning experience i have.
Suvendu Shekhar Giri 16-May-17 6:07am
   
so what is the issue?
Is it giving any error/wrong result?
Kunal Chowdhury «IN» 16-May-17 6:15am
   
Hi Member 13203537,
I would ask you to first learn what is 'if' condition, 'for' loop, 'while' loop etc. and their syntax's. There are plenty of articles available online. The above code that you shared, is syntactically incorrect and will not run.
Rate this:
Please Sign up or sign in to vote.

Solution 1

   
Comments
Maciej Los 16-May-17 15:44pm
   
5ed!
Rate this:
Please Sign up or sign in to vote.

Solution 2

If is not enoug: you have to someway iterate, in order to test for number primality. See, for instance: Primality test - Wikipedia[^].
   
Comments
Maciej Los 16-May-17 15:44pm
   
5ed!
Rate this:
Please Sign up or sign in to vote.

Solution 3

Quote:
How I write a code to know prime number by if

Short answer: you don't, because it is impossible. Loops and ifs are two different beast for different usage. It is like trying to flt with a car instead of a plane.

There is no possible advice other than 'study Java, follow tutorials'.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100