10 February, 2013

C Program To Find A Prime Number.

This Post Contains A C Program To Find A Prime Number With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'While-loop' & 'If...else' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C Program To Find A Prime Number.

/* Declaration Of Header Files */
# include <stdio.h>
# include <conio.h>

/* Start Of Main Program */
void main()
{

   /* Declaration Of Variables */
   int Num = 0, k, i;
   clrscr();

   /* Asking For The Input From User */
   printf ( " \n Enter Any Number : " );
   scanf ( " %d ", &Num );

   /* Source Code For Computing Prime Number */
   i = 1, k = 0;
   while ( i <= Num )
   {
      if ( Num % i == 0 )
         k++;
         i++;
   }
   if ( k == 2 )
   {
         printf ( " \n %d Is A Prime Number. " , Num );
   }
   else
   {
     printf ( " \n %d Is Not A Prime Number. " , Num );
   }
   getch();
}
/* End Of Main Program */

Output :

Enter Any Number  :  5

5 Is A Prime Number.

Enter Any Number  :  9

9 Is Not A Prime Number.




No comments:

Post a Comment

Subscribe To:

Most Commonly Asked Programs In 'C' & 'C++' Language.