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

Posted on 2008-11-16
Last Modified: 2013-11-17
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;



    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;



  cout << x << endl;





Open in new window

Question by:arivillafane
    LVL 7

    Accepted Solution

    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;
      return 0;

    Open in new window


    Author Closing Comment

    you forgot to declare i

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
    Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
    THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now