Prerequisite:
Prime Number: A natural Number is a prime number if it is only divided by 1 and itself.
For example – 1,2,3,5,7,11,13,17…etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include<stdio.h> int main() { long int i,i_var; unsigned primeFlag=0; /*it will set when number divide by some other digit except 1 and itself*/ printf("\n Program to check entered number is Prime or not"); printf("\nPlease enter any Number: "); scanf("%ld",&i_var); for(i=2;i<i_var;i++) { if((i_var%i)==0) { primeFlag=1; break; } else { ; /*best practice to use if-else together*/ } } if(primeFlag==1) { printf("\n%ld is also divisible by %ld except 1 and %ld.",i_var,i,i_var); printf("\n Hence %ld is not a prime Number",i_var); } else { printf("\n %ld is a prime Number",i_var); } return 0; } |
1 2 3 4 5 6 |
Output: Program to check entered number is even or odd Please enter any Number: 23221 is also divisible by 11 except 1 and 23221. Hence 23221 is not a Prime Number |