Skip to content
Home » C program to check whether a given number is prime or not

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

Prime number is a number which is divisible by 1 and itself

#include <stdio.h>
int main(){
    int i=2,num,count=0;
    printf("Enter a number:");
    scanf("%d",&num);
    if(num==1){
        count=1;
    }
    else{
        while(i<=num/2){
            if(num%i==0){
                count=1;
                break;
            }
            i++;
        }
    }
    count==1?printf("%d is not a Prime",num):printf("%d is a Prime",num);
}

Output 1:

Enter a number:6
6 is not a Prime

Output 2:

Enter a number:5
5 is a Prime