Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
i have done it with for loop. but i want it in if.

What I have tried:

Java
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.

 
Share this answer
 
Comments
Maciej Los 16-May-17 15:44pm    
5ed!
If is not enoug: you have to someway iterate, in order to test for number primality. See, for instance: Primality test - Wikipedia[^].
 
Share this answer
 
Comments
Maciej Los 16-May-17 15:44pm    
5ed!
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'.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900