Interview Questions   Tutorials   Discussions   Programs   Videos   

C++ - Write the prime number program in c++?




330
views
asked marvit August 18, 2014 11:12 AM  

Write the prime number program in c++?


           

1 Answers



 
answered By bhjahnavi   0  
/*program to find prime numbers */
#include<iostream>
using namespace std;

int main()
{
 int num;
 bool prime;

 cout << "Please enter a positive integer" << endl;
 cin >> num;

 for(int i = 3; i <= num; i++)
  {
  prime = true;
  for(int n = 2; n <= i - 1; n++)
   {
   if(i % n == 0)
   {
    prime = false;
   }
  }
  if(prime){
   cout << i << " is prime" << endl;
  }
 }

 return 0;
}
flag   
   add comment

Your answer

Join with account you already have

FF

Preview


Ready to start your tutorial with us? That's great! Send us an email and we will get back to you as soon as possible!

Alert