Prime Numbers Between 1-1000

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:

  1. This comment has been removed by the author.

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

    ReplyDelete
  3. help number between 1-1000 help tell me answer

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

    ReplyDelete

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

    ReplyDelete