C Programming Tutorial

Advertisements

Write program to display the series of prime number upto n in C++



       #include<iostream.h>
       #include<conio.h>
       void main()
       {
               int num,i,n,temp;
              clrscr();

               cout<<"\nEnter any number : ";
               cin>>num;

               for(n=1;n<=num;n++)
				 {
                     temp=0;

                     for(i=2;i<=n/2;i++)
                     {
                           if(n%i == 0)
                           {
                               temp=1;
                                break;
                           }
                     }

                     if(temp==0)
                        cout<<n<<" ";
               }

              getch();
       }

   Output : 

              Enter any number : 50
              1 2 3 5 7 11 13 17 19 23 29 31 41 43 47




Advertisement