Dark mode logo
Last Updated:
Finds if a number is prime or not

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

  1. Start
  2. Input a numbers say n
  3. i = 2
  4. check =0
  5. while ( i < = n/2) repeat 5.1 & 5.2
    5.1 if (n % i == 0) 
       5.1.1 check++
    5.2 i++
  6. if ( check == 0)
    6.1 Print prime
  7. else
    7.1 Print not prime
  8. 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