C Programming Code Examples C > Mathematics Code Examples C Program to Check if a given Number is Prime number C Program to Check if a given Number is Prime number This program takes a number and checks whether a given number is prime or not. - Take a number as input. - Check if the number is divisible by any of the natural numbers starting from 2. - If it is, then it is not a prime number. Otherwise it is a prime number. - Exit. #include <stdio.h> #include <stdlib.h> void main() { int num, x, flag; printf("Enter a number \n"); scanf("%d", &num); if (num <= 1) { printf("%d is not a prime numbers \n", num); exit(1); } flag = 0; for (x = 2; x <= num / 2; x++) { if ((num % x) == 0) { flag = 1; break; } } if (flag == 0) printf("%d is a prime number \n", num); else printf("%d is not a prime number \n", num); } Take a number as input and store it in the variable num. If the number is lesser than or equal to 1, then print the output as " It is not a prime number". Initialize the variable flag to zero. Using for loop, check if the input number is divisible by any of the natural numbers starting from 2. If it is, then assign the variable flag with 1. Print the output as "It is a prime number", if the variable flag ==0. Otherwise print the output as "It is not a prime number" and exit.