Computer Science, asked by Sayani07, 11 months ago

WAP to input a no, and check if it is prime or not.​

Answers

Answered by debosmita75
3

int main()

{

int n, i, flag = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);

for(i = 2; i <= n/2; ++i)

{

// condition for nonprime number

if(n%i == 0)

{

flag = 1;

break;

}

}

if (n == 1)

{

printf("1 is neither a prime nor a composite number.");

}

else

{

if (flag == 0)

printf("%d is a prime number.", n);

else

printf("%d is not a prime number.", n);

}

return 0;

}

Output

Enter a positive integer: 29 29 is a prime number.

Hope this helps you

Answered by anindyaadhikari13
23

Here is your answer.

&lt;hr/&gt;

class x

{

static void main(int n)

{

int c=0;

for(int I=1;I<=n;I++)

{

if(n%I==0)

c++;

}

if(c==2)

System.out.println("Prime.");

else

System.out.println("Not Prime.");

}

}

&lt;hr/&gt;

Similar questions