Skip to main content

Given Number is Prime or Not (Efficient Algorithm)

#include <stdio.h> #include <math.h> int main() { int n, flag = 0; printf("Enter a number : "); scanf("%d" , &n ); if(n < 2) { printf("\nNumber should be greater than 2"); return 0; } for(int i = 2 ; i <= sqrt(n) ; i++ ) { if( n % i == 0 ) { flag = 1; break; } } if(flag == 0) printf("\n%d is a Prime number", n); else printf("%d is not a Prime number", n); }



Comments