https://i1.wp.com/eblogarithm.com/wp-content/uploads/2016/10/Prime-Number-Program-in-C-Language.png?fit=640%2C360

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 21 times, 1 visits today)



There are no comments

Add yours

Leave a Reply

%d bloggers like this: