
C Program to check if a number is prime or not
Algorithm
Input: Input a numbers
output: Check if a number is prime or not
- Start
- Input a numbers say n
- i = 2
- check =0
- while ( i < = n/2) repeat 5.1 & 5.2
5.1 if (n % i == 0)
5.1.1 check++
5.2 i++ - if ( check == 0)
6.1 Print prime - else
7.1 Print not prime - stop
C Program
#include <stdio.h>
int main( )
{
int n, check = 0;
printf("Enter a number: ");
scanf("%d", &n);
for (int i = 2; i <= n / 2; i++)
{
if (n % i == 0)
check++;
}
if (check == 0)
printf("The number %d is prime", n);
else
printf("The number %d is not prime", n);
return 0;
}
Note: This Program was created using visual studio code. The Program file can be downloaded by clicking here
Comments