Ask Question
3 October, 02:58

Write a program that asks the user how many integers they would like to enter. You can assume that this initial input will be an integer > = 1. The program will then prompt the user to enter that many integers. After all the numbers have been entered, the program should display the largest and smallest of those numbers (no, you cannot use lists, or any other material we haven't covered).

+2
Answers (1)
  1. 3 October, 04:33
    0
    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 / /include iostream header file for input and output

    #include / / 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; } }

    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. 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
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a program that asks the user how many integers they would like to enter. You can assume that this initial input will be an integer > ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers