/*****************************************************************/ // File : primenum.c // Purpose : Checks whether the given number is PRIME or NOT a PRIME. // Author : Nanda Kishor K N // Mail Id : knnkishor@yahoo.com, nandakishorkn@rediffmail.com // Website : www.c4swimmers.esmartguy.com ( WEB MASTER ) // Group : c4swimmers@yahoogroups.com ( MODERATOR ) /*****************************************************************/ #include #include /* Name : main() Purpose : This is the main function that accepts the number and checks whether the given number is prime or not. Uses the built-in sqrt() function defined in math.h file Parameter : None */ main() { // Variable Declarations & Initialization // Naming conventions are used like all integer variables int iNum,iFlag=1,iIdx; // Set the Flag printf("Enter number : "); scanf("%d",&iNum); // Check whether the given number n, which is divisible by 2,3,4,...,sqrt(n) // If the number is divisible then it is NOT a PRIME number // Otherwise it is a PRIME number for (iIdx = 2; iIdx <= (int)sqrt(iNum); iIdx++) { if (iNum % iIdx == 0) { iFlag=0; // Reset the Flag & terminate the loop break; } } // Verify the flag status and print the given number as PRIME or NOT a PRIME if (iFlag != 0) printf("It's a PRIME number\n"); else printf("\nIt's NOT a PRIME number\n"); return 0; } // End of main() program