20 February, 2013

C Program To Find & Print Twin Prime Numbers.

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

/* Declaration Of Header Files */

# include < stdio.h >
# include < conio.h >

/* Start Of Main Program */

void main()
{

   /* Declaration Of Variables */
   int   i,   j,   k,   r,   t,   a[ 10 ] ;
   clrscr();

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

   /* Source Code For Finding & Printing Twin Prime Numbers */
   i    =    1 ;  t    =    0 ;
   while   (   i   <=    r    )
   {
              j  =  1 ;  k  =  0 ;
              while   (   j  <=  i   )
              {
                       if   (   i   %   j   ==   0   )
                                k++ ;
                                j++ ;
              }
               if   (   k   ==   2   )
              {
                        a[ t ]  =  i;
                         t++ ;
              } 
              i++ ;
   }
   for    (   i  =  0;    i   <   t;    i++  )
   {
            if   (   a[ i + 1 ]  -  a[ i ]   ==   2    )
                     printf ( "  \n %d & %d Are Twin Prime Nos. ",  a[i],  a[i+1] );

   }  
   getch();
}
/* End Of Main Program */ 


Output :

Enter Range  :  10

3  &  5  Are  Twin  Prime  Nos.

7  &  9  Are  Twin  Prime  Nos. 




No comments:

Post a Comment

Subscribe To:

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