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


In this program, You will learn how to check a number is prime or not using a 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 a function in c.

#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("Number is prime:%d", n);
   } else {
       printf("Number is not prime :%d", n);
   }

   return 0;
}

Output:

Enter a number:17                                                                                                                      
Number is prime:17