This C Program checks if a given number is prime. If a given number that has no other factor than that of the given number itself and 1, then that number is called as prime number.

Here is source code of the C program to check if a given number is prime. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/*`

`* C program to check whether a given number is prime or not`

`* and output the given number with suitable message.`

`*/`

`#include <stdio.h>`

`#include <stdlib.h>`

void main()

`{`

int num, j, 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 (j = 2; j <= num / 2; j++)

`{`

if ((num % j) == 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);

`}`

$ cc pgm16.c $ a.out Enter a number 23 23 is a prime number $ a.out Enter a number 15 15 is not a prime number

**Sanfoundry Global Education & Learning Series – 1000 C Programs.**

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.