Question #47759

Write a complete program that reads at most 30 positive integer values, or it will stops reading if the user enters -9999
If the user enters a negative number, the number should not be accepted and he should have the chance to re-enter the number again.

After you stop reading integers from keyboard, the program should be successfully calculate and print the following:

The number of integers between 0 & 200 is: …
The number of integers between 200 & 500 is:….
The number of integers above 500 is:…
The number of primes is:….
The maximum prime number entered is:
The maximum number among all integers is:….

Expert's answer

Answer on Question #47759 - Programming, C++

Write a complete program that reads at most 30 positive integer values, or it will stops reading if the user enters -9999

If the user enters a negative number, the number should not be accepted and he should have the chance to re-enter the number again.

After you stop reading integers from keyboard, the program should be successfully calculate and print the following:

The number of integers between 0 & 200 is: ...

The number of integers between 200 & 500 is:...

The number of integers above 500 is:...

The number of primes is:...

The maximum prime number entered is:

The maximum number among all integers is:...

Solution.

//Connecting library
#include <iostream>
using namespace std;
//Function computing the sum of prime numbers
bool isPrime(int a)
{
    for(int j = 2; j < a; j++)
    {
        if(a % j == 0)
        {
            return 0;
        }
    }
    if(a>1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
//The main function
int main()
{
    int N=30;
    int a[30]; //array
    for(int i=0; i<N; i++)
    {
        cout << endl << "Please enter the number #" << i + 1 << ": ";
        cin >> a[i];
        if (a[i] == -9999)
        {
            N = i;
            break;
        }
        while (a[i] < 0)
        {
            cout << endl << "Please, re-enter: ";
            cin >> a[i];
        }
    }
    cout << "======" << endl << "The number of integers between 0 & 200 is: " << endl;
    for (int i = 0; i < N; i++)
    {
        if (a[i] >= 0 && a[i] <= 200)
        {
            cout << a[i] << ", ";
        }
    }
    cout << endl << endl << "The number of integers between 200 & 500 is: " << endl;
    for (int i = 0; i < N; i++)
    {
        if (a[i] >= 200 && a[i] <= 500)
        {
            cout << a[i] << ", ";
        }
    }
    cout << endl << endl << "The number of integers above 500 is: " << endl;
    for (int i = 0; i < N; i++)
    {
        if (a[i] > 500)
        {
            cout << a[i] << ", ";
        }
    }
    cout << endl << endl << "The number of primes is: " << endl;
    for (int i = 0; i < N; i++)
    {
        if (isPrime(a[i]))
        {
            cout << a[i] << ", ";
        }
    }
    cout << endl << endl << "The maximum prime number entered is: " << endl;
    int maxPrime = 0;
    for (int i = 0; i < N; i++)
    {
        if (isPrime(a[i]))
        {
            if (maxPrime < a[i])
            {
                maxPrime = a[i];
            }
        }
    }
    cout << maxPrime;
    cout<<endl<<endl<<"The maximum number among all integers is: "<<endl;
    int max = 0;
    for (int i = 0; i < N; i++)
    {
        if (max < a[i])
        {
            max = a[i];
        }
    }
    cout << max;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS