Program to find Prime Number

Program to find Prime Number

We know that, prime number is a number which is not divisible by any other number except by 1 and itself.

Program:
#include<stdio.h>
void main()
{
int f=0,n,i;
printf("enter number for checking whether number is prime or not: ");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i==0)
{
f=1;
break;
}
}
if(f==0)
{
printf("Prime number");
}
else
{
printf("Composite number");
}
}

Output:




Note:
We have used f as flag here.
It is used to indicate whether number is prime or not at end if else condition.
Also, we have used break statement.
If break statement is encountered by compiler it skips loop.




No comments:

Post a Comment