Log in to add a comment

Log in to add a comment

for java,

class prime

{

void main(int n)

{

int no_fac,;// no_fac represents no. of factors

int lo_va;// lo_va represents loop variable

for(lo_va=1;lo_va<=n;lo_va++)

{

no_fac=0;// no. of factors is initially zero

if(n%lo_va==0)// this means "if lo_va is a perfect factor of n"

no_fac++;//no. of factors is increased by 1

}

if(no_fac==2)//only a prime no. has 2 factors, rest all have more than 2

System.out.println("It is a prime no.");

else

System.out.println("It is not a prime no.");

}

}

int main()

{

int num;

cout << "Enter a number ";

cin >> num;

int i=2;

while(i<=num-1)

{

if(num%i==0)

{

cout << "\n" << num << " is not a prime number.";

break;

}

i++;

}

if(i==num)

cout << "\n" << num << " is a prime number.";

return 0;

}