# 4 – Best C program to check prime number

Table of Contents

## C program to check prime number

Prime number is a number which is only divisible by itself or by one. Following  C program will help to understand the logic to check whether the number is prime or not.

### Logic to check prime number in C programming

`#include <stdio.h>int main(){    int num,i,flag=0;    printf("Enter any number : ");    scanf("%d", &num);    if(num==1)     {        printf("1 is not a prime or composite number");    }    else     {        for (i=2; i<=num/2; i++)        {            if (num%i == 0)             {                flag = 1;                break;            }        }        if(flag==0)        {            printf("%d is a prime number", num);        }        else        {            printf("%d is not a prime number", num);        }    }    return 0;}`

Output :

Enter any number : 23

23 is a prime number

### Connect with us on following platforms

We are aiming to explain all concepts of C in easiest terms as possible.
ITVoyagers
Author