10 February, 2013

C Program To Print Prime Numbers Upto A User-Specified Range.

This Post Contains A C Program To Print Prime Numbers Upto A User-Specified Range With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'For-loop', 'While-loop' & 'Nested Loops' 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 Print Prime Numbers Upto A User-Specified Range.

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

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

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

   /* Asking For The Input From User */
   printf ( " \n Enter Range : " );
   scanf ( " %d ",  &range );
   printf ( " \n Prime Nos. Upto Range %d Are As Follows : \n ", range );

   /* Source Code For Computing Prime Numbers Upto The Given Range */
   for ( i=1; i<=range; i++ )
   {
      j = 1, k = 0;
      while ( j <= i )
      {
          if ( i % j == 0 )
          k++;
          j++;
      }
      if ( k == 2 )
      {
         printf ( " %d  \t ", i );
      }
    }
    getch();
}
/* End Of Main Program */

Output :

Enter Range : 5

Prime Nos. Upto Range 5 Are As Follows :

2        3        5




No comments:

Post a Comment

Subscribe To:

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