C Program to check a number is prime or not using function


In this program, You will learn how to check a number is prime or not using function in c.


Some list of prime numbers is: 2 3 5 7 11 13


Example: How to check a number is prime or not using function in c

Run it Now
#include<stdio.h>

int check(int n) {
   int i, p = 1;

   for (i = 2; i < n; i++) {
       if (n % i == 0) {
           return p = 0;
           break;
       }
   }
   return p;
}

int main() {

   int n, c;

   printf("Enter a Number :");
   scanf("%d", &n);

   c = check(n);

   if (c == 1) {
       printf("\nThe Number is Prime :%d", n);
   } else {
       printf("\nThe Number is Not Prime :%d", n);
   }

   return 0;
}

Output:

Enter a Number :7
The Number is Prime :7