on
CSS
- Get link
- X
- Other Apps
#include <stdio.h>
Standard Input
Output library, which is required for functions like printf() and scanf().
void main()
This is the main function, where program execution starts.
int a, i, flag = 0;
stores the number a, b entered by the user.
printf ("enter a positive integer");
scanf ("%d", &a);
user to enter a number.
if (a == 0 || a == 1)
flag=1;
0 and 1 are not prime numbers by definition.
for (i = 2; i <= a / 2; i++)
{
if (a % i == 0)
{
flag = 1;
break;
}
}
loop check if any number between 2 and a/2 divides a completely
if (flag == 0)
printf("%d is
prime number ", a);
else
printf("%d is
not prime number", a);
If no divisor was found (flag is still 0), the number is
prime.
Comments
Post a Comment