C Program
C Program to Check Number is prime or Not prime
Examples :-
C Program to Check Number is prime or Not prime
Explain Program
Step :1
#include <stdio.h>
Standard Input
Output library, which is required for functions like printf() and scanf().
Step :2
void main()
This is the main function, where program execution starts.
Step :3
int a, i, flag = 0;
stores the number a, b entered by the user.
Step :4
printf ("enter a positive integer");
scanf ("%d", &a);
user to enter a number.
Step :5
if (a == 0 || a == 1)
flag=1;
0 and 1 are not prime numbers by definition.
Step :6
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
Step :7
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.
No comments:
Post a Comment