• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4072
  • Last Modified:

C++ Program to find prime numbers between 2 and 10,000

I need help i need to develop a program in C++ that prints all prime number between 2 and 10,000. All i have so far is not working out.
#include <iostream>
 
using std::cout;
using std::endl;
 
#include <iomanip>
using std::setw;
 
 
int primo();// funcion prototipo
 
int main() 
{
    cout << "Los numeros primos entre 2 a 10,000 son: " << endl;
    primo();
    system("pause");
    return 0;
}
 
int primo ()// La funcion primo
{
   int x;
   int y;
   int z;
   
 for( x = 2; x <= 10000; x++)
 {      
  for( y = 2; y <=(x-1); y++)
  {
    z = x % y;
 
    if( z == 0)
   z =(x+1)% y;
   else
    { 
  cout << x << endl;
}
}
}
}

Open in new window

0
arivillafane
Asked:
arivillafane
1 Solution
 
aherpsCommented:
Hi, try this one:

Not tested..... But I think it should work.
#include <iostream>
#include <math.h>
using namespace std;
 
int numMin = 2;
int numMax = 10000;
int pt = 0;
bool is_prime;
 
 
int main() 
{
 
  while (numMin <= numMax) 
  {
      is_prime = true;
      for (pt = 1; pt <= sqrt(numMin); pt++) 
      {
        if (numMin % pt == 0)
          is_prime = false;
      }
      if (is_prime)
        cout << numMin << endl;
      i++;   
  }
  return 0;
}

Open in new window

0
 
arivillafaneAuthor Commented:
you forgot to declare i
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now