Prime Number program in C

A prime number is a positive integer that is divisible only by **1** and itself. For example: 2, 3, 5, 7, 11, 13, 17, 19 etc.

// C program to check if a number is prime or not #include <stdio.h> #include <stdbool.h> bool checkPrime(int num) { int i; bool flag = false; // 0 and 1 are not prime numbers if (num == 0 || num == 1) flag = true; for (i = 2; i <= num / 2; ++i) { // if num is divisible by i, then n is not prime if (num % i == 0) { flag = true; break; } } return flag; } int main() { int num; printf(" Enter the number:"); scanf("%d", &num); if (checkPrime(num) == false) printf("%d is a Prime Number\n", num); else printf("%d is not a Prime Number\n", num); return 0; }

$ gcc prime.c $ ./a.out Enter the number:2 2 is a Prime Number $ ./a.out Enter the number:14 14 is not a Prime Number $ ./a.out Enter the number:56 56 is not a Prime Number $ ./a.out Enter the number:17 17 is a Prime Number $ |

If you like the post, Please share your feedback!

**also see**

C Programming language |
Go Programming language |

Linked List |
Array |

Stack |
Queue |

Puzzle |
Reasoning |

Aptitude |
HTML |