Answer:
I am writing a code in C++ programming language. Let me know if you need it in some other programming language. Here is the code:
C++ Code:
#include <iostream> //include iostream header file for input and output
#include <climits> // for INT_MAX and INT_MIN
using namespace std;
//namespace used by computer to detect objects like cin, cout
int main() { // enters the body of main function
int number; // input number
int totalnum= 0; //counts the number of integers entered
cout<<"how many integers would you like to enter:";
//asks the user how many integers they would like to enter
cin>>totalnum; //reads the number of integers entered
int maximum = INT_MIN;
// maximum of the numbers, initialized to minimum
int minimum= INT_MAX;
// minimum of the numbers, initialized to maximum
char stop = 'q'; // stop entering the numbers by typing q
// Read Input numbers until q is entered
cout << "Enter the integers or " << stop << " to exit ";
/*loop starts which reads the numbers entered by user and it will stop when q is typed*/
while (cin >> number && number != stop) {
//if the entered number is greater than value in maximum variable
if (maximum < number)
maximum = number; //assigns that number to maximum variable
//if the entered number is less than value in maximum variable
if (minimum > number)
minimum = number; } //assigns that number to maximum variable
cout << endl;
// displays the number of integers that user entered
cout << "Total integer numbers entered are" << totalnum<< endl;
if (totalnum> 0)//if the integers are entered by the user {
//displays maximum of the numbers
cout << "Maximum is " << maximum << endl;
//displays minimum of the numbers
cout << "Minimum is " << minimum << endl; } }
Step-by-step explanation:
The program asks the user how many integers he would like to enter. After he enters the value then the user is prompted to enter integers. User can enter integers (positive and negative) and to stop entering numbers he should type "q". The character q is used as exit so that the program can process the integers and find the maximum and minimum of the input numbers. <climits> is used to define the size of integers or we can say it defines limits for integer types . INT_MAX and INT_MIN are the macros that specify the maximum and minimum values of the integers. INT_MAX specifies that maximum value for a int type variable is +2147483647 and INT_MIN specifies that minimum value for a int type variable is -2147483648.
The while loop used will keep on reading the input numbers as they are entered by the user and the loop will break when the user reaches exit by entering the character q. In the loop body each number entered by the user is checked. If the input number is greater than the maximum then this number will be assigned to maximum variable. IF the input number entered is less than the value of the minimum variable then the number will be stored in the minimum variable. This way the program will find the maximum and minimum of the input numbers. After the loop breaks when the user enters q and stops entering integers the value stored in the maximum and minimum variables are displayed.
Output
how many integers would you like to enter: 5
Enter the integers or q to exit 3
-4
6
2
1
3
q
Total integer numbers entered are 5
Maximum is 6
Minimum is -4