C Program to check Prime or Composite Number

Prime or Composite Number logic is finding it's total number of factors and if it is equal to 2 then given number is prime else it is composite.


CODE:


#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,s;
printf("Enter the number:");
scanf("%d",&n);
s=0;
for(i=1;i<=n;i++) //Loop to find total  number factors
{
if(n%i==0)
{
s+=1; // Sum Every factors
}
}

if(s==2)  // If number of factors is 2 then prime else not prime
{
printf("Prime Number");
}
else
{
printf("Not a Prime Number");

}
getch();
return 0;
}

Comments