Prime Numbers Between 1-1000

Monday, September 09, 2013 5 Comments A+ a-

C++ program to find prime numbers between 1 to 1000


Problem:
Write a C++ program which prints all the prime numbers from 1-1000. 

Prime numbers are numbers greater than 1 and which has 1 and the number itself as positive factors or divisors. It doesn't have a positive factor or divisors other than 1 and the number itself.

Let's take 5 for example:
1 and 5 (the number itself) can evenly divide 5 so they are factors of 5.
2, 3, 4 are cannot evenly divide 5 so they are not factors of 5.
Therefore, 5 is a Prime Number.

This is a program which shows the Prime Numbers from 1-1000.


/*
Prime Numbers (from 1 - 1000)
*/
#include <iostream>

using namespace std;

int main(void)
{
 int num,i,countFactors;
 char hang;

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

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

  for (i = 2; i <= num; i++)
  {
   //if a factor exists from 2 up to the number, count Factors
   if (num % i == 0)
   {
    countFactors++;    
   }
  }

  //a prime number has only itself as a factor
  if (countFactors == 1)
  {
   cout << num << ", ";
  }
 }

 //to let console stay
 cin >> hang;

 return 0;
}

5 comments

Write comments
Unknown
AUTHOR
November 10, 2013 at 1:32 AM delete This comment has been removed by the author.
avatar
Unknown
AUTHOR
December 14, 2016 at 11:50 AM delete

Aatka prasansa kre layak ghalo nhi btaye he Tay toh ukhar aysan tariff karths ki jano mano bhut bade ..mahapurush he

Reply
avatar
Unknown
AUTHOR
February 11, 2017 at 4:52 AM delete

help number between 1-1000 help tell me answer

Reply
avatar
Unknown
AUTHOR
July 29, 2017 at 1:54 AM delete

The prime number distribution sequence can only provide accurate results if multiple counting of equal terms is averted.

Reply
avatar
Unknown
AUTHOR
November 27, 2017 at 4:33 AM delete


prime number distribution . (PNDS), developed by the NSI team of mathematicians2 which computes the exact count of primes numbers.

Reply
avatar