0

I need to wright a program to compute the number of prime numbers in the first 50 "chiliads”. Yes sounds confusing but it just has to out put this basically.

Start End Number of Primes
1 1000 168
1001 2000 135
2001 3000 127
3001 4000 120
4001 5000 119
5001 6000 114
6001 7000 117
7001 8000 107
8001 9000 110
9001 10000 112



49001 50000 98

So far I have only been working on a function proto. to find prime numbers.

#include <iostream>

using namespace std;

int prime_num(int);

int main()
{
    cout << " Enter a number and I will generate the prime numbers up to that number: ";
    int num = 0;
    cin >> num;
    prime_num(num);
        //cout << num << endl; not used
    
}
 
int prime_num( int num)
{
        bool isPrime=true;
        
        // for (initial statement; loop condition ; update statement)
        
        for ( int i = 2; i <= num; i++)
        {
                for ( int j = 2; j <= num; j++)
                {
                        if ( i != j && i % j == 0 )
                        {
                          isPrime=false;
                          break;
                        }
                }
                if (isPrime)
                {
                  cout << i << endl;
                }
                isPrime=true;
        }
}

How do I get this to count the number of primes rather then display them?

And once it has counted them display them like the above example?

THANK YOU!

3
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by hollystyles
0

>How do I get this to count the number of primes rather then display them?

set up a counter...
you initialise the counter before the main body of the for or while loop.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.