Learn about Prime Number Program in C in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
Prime Number Program in C:
Prime Number is generally defined as a Natural Number that is divided by 1 and itself. So the logic obtained is the number can’t be divided by any numbers other than 1 and itself. Ex:2,3,5,7,11,13,17…..etc
#include<stdio.h>
int main()
{
int n,set=0;
printf("Enter the number:");
scanf("%d",&n);
if(n>0)
{
for(int i=2;i<n/2;i++)//checks dividers above 1
{
if(n%i==0)
{
printf("The number is not prime");
set=1;
break;
}
}
if(set==0)
{
printf("The number is prime");
}
}
}
Output:
Enter the number:67
The number is prime
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Also Read: