Prime Number Program in C Language

This is just a simple program for Prime Number.  Logic is simple. Checking the inputed number if it is divided by 2 or not. We just need to use a for loop. If you any other efficient  code feel free to share with us.

 

Here is my Code:

int n, i, prime = 1;

printf(“Enter a positive number: \n”);
scanf(“%d”,&n);

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

if(n%i==0)
{
prime=0;
break;
}
}

if (prime==1)
printf(“%d is a prime number. \n”,n);
else
printf(“%d is not a prime number. \n”,n);

(Visited 27 times, 1 visits today)

About the Author:

Suniv Ashraf

Leave A Comment