S-K
S-K

Reputation: 79

Prime numbers display and count c++

The code is working fine for displaying prime numbers, but I need them to be displayed and to also display the number of prime numbers.

The code is for prime numbers in a selected interval.

Here's my code:

bool prastevila_interval(int z, int k) //primes in interval
{
    for (int i = z; i < k; ++i){
        for (int j = 2; j <= i; j++){
            if (!(i % j) && (i != j))
                break;
            else if (i == j){
                cout<< i <<endl;
            }

        }
    }
return true;
}

//--------------------------------------------------------------------
int main() { 

                //Primes in interval
    cout<<"__________PRASTEVILA V INTERVALU__________"<<endl;

    cout << "Zacetek intervala : ";
    cin >> z;                          //starting point of interval

    cout << "Konec intervala : ";
    cin >> k;                          //ending point of interval

    cout<<"Prastevila v intervalu so: "<<endl;

    prastevila_interval(z, k) == true;

    cout <<"Stevilo prastevil na interval od "<< z 
         <<" do "<< k <<" je : "<< counter << endl;   
    // this counter is not working ---^
}

How to count the number of primes?

P.S. : I tried everything already and I just can't figure it out.

Upvotes: 2

Views: 581

Answers (5)

Roman Pustylnikov
Roman Pustylnikov

Reputation: 1932

You just need to update the counter on each prime number found. Since your method return always true, I suggest to change it like so:

int prastevila_interval(int z, int k) //primes in interval
{
    int counter = 0;
    for (int i = z; i < k; ++i){
        for (int j = 2; j <= i; j++)
        {
            if (!(i % j) && (i != j))
                    break;
            else if (i == j)
            {
                    cout<< i <<endl;
                    counter++;
            }
         }      
     }
return counter;
}

and so:

cout <<"Stevilo prastevil na interval od "<< z 
     <<" do "<< k <<" je : "<< prastevila_interval(z,k) << endl; 

Note also, that this code:

prastevila_interval(z,k) == true;

doesn't do anything and generally meaningless. Your just check here whether the return value is true and don't do anything with it.

Upvotes: 2

Sakib Ahammed
Sakib Ahammed

Reputation: 2480

You can try it like:

bool prime(int num)
{
    if (num == 2) return true;
    else
        for (int j=2; j<=sqrt(num); j++)
        {
            if ( num%j == 0)
                return false;
        }
    return true;
}

int prastevila_interval(int z, int k) //primes in interval
{
    int counter =0;
    for (int i=z; i<k; ++i)
    {
        if (prime(i))
        {
            cout<< i <<" ";
            counter++;
        }

    }
    cout<<endl;
    return counter;
}

int main()
{

    //Primes in interval
    cout<<"__________PRASTEVILA V INTERVALU__________"<<endl;
    cout<<"Zacetek intervala : ";
    cin>>z;                          //starting point of interval
    cout<<"Konec intervala : ";
    cin>>k;                          //ending point of interval
    cout<<"Prastevila v intervalu so: "<<endl;
    int counter = prastevila_interval(z,k);
    cout<<"Stevilo prastevil na interval od "<<z<<" do "<<k<<" je : "<<counter<<endl;

Upvotes: 1

Daffyd
Daffyd

Reputation: 104

Just add a counter to your function and increment the counter with one each time you find a prime.

bool prastevila_interval(int z, int k, int &counter) //primes in interval
{
    for (int i=z; i<k; ++i){
            for (int j=2; j<=i; j++)
        {
            if (!(i%j)&&(i!=j))
                break;
            else if (i == j)
            {
                counter++;
                cout<< i <<endl;
            }   
    }
    return true;
}

Initialize a counter in your main function and pass it to the function as usual.

Upvotes: 1

Ziezi
Ziezi

Reputation: 6467

You could modify your function to:

int prastevila_interval(int z, int k) //primes in interval   
{
    int counter = 0;      

    for (int i = z; i < k; ++i){
        for (int j = 2; j <= i; j++){
            if (!(i % j) && (i != j))
                break;
            else if (i == j){
                counter++;
                cout<< i <<endl;
         }
    }
return counter;
}

and then call it in your main as:

int prime_number = prastevila_interval(lower_bound, upper_bound);

cout << "Number of primes:" << prime_numbers << endl;

Upvotes: 1

yati sagade
yati sagade

Reputation: 1375

Pass a pointer or a reference to the counter to your function, and have it update it.

bool prastevila_interval(int z, int k, int& counter) //primes in interval
{
    counter = 0;
    for (int i=z; i<k; ++i){
        for (int j=2; j<=i; j++)
        {
            if (!(i%j)&&(i!=j))
                break;
            else if (i == j){
                cout<< i <<endl;
                ++counter;
            }

        }
    }
    return true;
}

Calling this will be like:

int counter = 0;
prastevila_interval(lo, hi, counter);

Another way you can do this is by having prastevila_interval return an int instead of a bool, which is the number of primes printed.

Upvotes: 1

Related Questions