14,303,124 members
Rate this:
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
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:

## Solution 1

Maciej Los 16-May-17 15:44pm

5ed!
Rate this:

## Solution 2

If is not enoug: you have to someway iterate, in order to test for number primality. See, for instance: Primality test - Wikipedia[^].
Maciej Los 16-May-17 15:44pm

5ed!
Rate this:

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