C++ program that display all the prime numbers between 2 and 1000
Posted by Samath
Last Updated: December 24, 2014

Develop a program to display all the prime numbers between 2 and 1,000, inclusive. Display eight prime numbers per line. Numbers are separated by exactly one space.

Solution:

#include <iostream>

using namespace std;

int main()
{
 int num, i, countFactors;
 int countBreak = 0;

 cout << "Prime numbers (1-1000) : " << endl;

 for (num = 1; num <= 1000; num++)
 {
  countFactors = 0;

  for (i = 2; i <= num; i++)
  {
   if (num % i == 0)
   {
    countFactors++;    
   }
  }

  if (countFactors == 1)
  {
   cout << num << " ";
   countBreak++;

   if(countBreak == 8)
   {
	   cout<<endl;
	   countBreak = 0;
   }
  }
 }
 system("pause");
 return 0;
}