w3resource

C++ Exercises: Asked user to input positive integers to process count, maximum, minimum, and average

C++ For Loop: Exercise-15 with Solution

Write a C++ program that asks the user to enter positive integers in order to process count, maximum, minimum, and average or terminate the process with -1.

Visual Presentation:

C++ Exercises: Asked user to input positive integers to process count, maximum, minimum, and average or terminate the process with -1

Sample Solution :-

C++ Code :

#include <iostream> // Include input-output stream header
#include <climits> // Include limits header for INT_MAX
#include <iomanip> // Include header for input/output manipulation

using namespace std; // Using standard namespace to avoid writing std::

int main() // Start of the main function
{
    int posnum, ctr = 0, sum = 0; // Declaration of integer variables 'posnum', 'ctr', 'sum' and initialization to 0
    int max = 0; // Initialization of 'max' to 0
    int min = INT_MAX; // Initialization of 'min' to the maximum integer value available in the system
    int terval = -1; // Variable 'terval' initialized with -1 as a termination condition

    // Display a message to input positive integers or -1 to terminate
    cout << "\n\n Input a positive integers to calculate some processes or -1 to terminate:\n";
    cout << "----------------------------------------------------------------------------\n";
    cout << " Input positive integer or " << terval << " to terminate: ";

    // Read input until termination condition is met (-1 is entered)
    while (cin >> posnum && posnum != terval) 
    {
        if (posnum > 0) // Check if the input is positive
        {
            ++ctr; // Increment the counter for positive numbers
            sum += posnum; // Calculate sum of positive numbers
            if (max < posnum)
                max = posnum; // Update maximum value if the current input is larger
            if (min > posnum)
                min = posnum; // Update minimum value if the current input is smaller
        }
        else 
        {
            // Display an error message for negative inputs or inputs other than -1
            cout << "error: input must be positive! if negative, the value will only be -1! try again..." << endl;
        }
        // Prompt for the next input or termination
        cout << " Input positive integer or " << terval << " to terminate: ";
    }

    // Display the result after termination
    cout << "\n Your input is for termination. Here is the result below: " << endl;
    cout << " Number of positive integers is: " << ctr << endl;
    if (ctr > 0) 
	{
        cout << " The maximum value is: " << max << endl;
        cout << " The minimum value is: " << min << endl;
        cout << fixed << setprecision(2); // Set output to fixed precision of 2 decimal places
        cout << " The average is " << (double)sum / ctr << endl; // Calculate and display the average of positive integers
    }
}

Sample Output:

 Input a positive integers to calculate some processes or -1 to terminate:                                                                      
----------------------------------------------------------------------------                                                                  
 Input positive integer or -1 to terminate: 25                         
 Input positive integer or -1 to terminate: 15                         
 Input positive integer or -1 to terminate: 35                         
 Input positive integer or -1 to terminate: -1                         
                                                                       
 Your input is for termination. Here is the result below:              
 Number of positive integers is: 3                                     
 The maximum value is: 35                                              
 The minimum value is: 15                                              
 The average is 25.00

Flowchart:

Flowchart: Asked user to input positive integers to process count, maximum, minimum, and average

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C++ to find the sum of series 1 - X^2/2! + X^4/4!-.... upto nth term.
Next: Write a program in C++ to list non-prime numbers from 1 to an upperbound.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/cpp-exercises/for-loop/cpp-for-loop-exercise-15.php