Prime Numbers Using C++ Programming Language

Prime numbers: The number greater than one and which has no other divisors other than one and itself.

Composite numbers: The natural numbers greater than one and other than prime numbers are called as composite numbers.



Q; Program code for Prime numbers.


Program Code:

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int count=0 , num ;
cout<<endl;
cout<<"********************************************************************************"<<endl;
cout<<"                                  Prime Numbers                               "<<endl;
cout<<"********************************************************************************"<<endl<<endl;

for (int num=1; num<=150 ; num++)
{
count=0;
for(int i = 2 ; i<=num/2 ; i++)
{
   if(num%i==0)
   {
      count++;
      break;
       }
   }
   if (count==0 && num !=1)
cout<<" "<<num<<" ";

}
cout<<endl<<endl;
cout<<"********************************************************************************"<<endl;

   getch();
}

Output:
Click on pic to enlarge it.

Hope that you will find this helpful.


No comments:

Post a Comment

Please disable your ad blocker to support this website.

Our website relies on revenue from ads to keep providing free content.