The problem is Write a program with a loop that lets the user enter a series of
ID: 3685807 • Letter: T
Question
The problem is
Write a program with a loop that lets the user enter a series of integers. The user should enter 99 to signal the end of the series. After all the numbers have been entered, the program should display the largest and smallest numbers entered.
I ahve it worked out in c++ but I can't figure out how to get the smallest value, without using an array. Please use the code I am providing and fill in the missing stements to find the samllest number and comment what you did.
Thanks.
#include<fstream>
#include<iomanip>
using namespace std;
int main()
{
int num{}, max{}, min{};
max = min = num;
cout << "Enter a series of numbers, enter -99 to stop." << endl;
cin >> num;
while (num != -99)
{
if (num > max)
max = num;
if (num < min)
min = num;
cout << "enter another number" << endl;
cin >> num;
}
cout << "The largest number is " << max << " The smallest number is " << min << endl;
return 0;
}
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int max=0,num=0,min=10000000;
cout << "Enter a series of numbers, enter -99 to stop." << endl;
while (num != -99)
{
cin >> num;
if(num==-99)break;// add this and all
if (num>max)
max=num;
if (num<min)
min=num;
}
cout << "The largest number is " << max << " The smallest number is " << min << endl;
return 0;
}
OUTPUT:
Enter a series of numbers, enter -99 to stop.
3 4 5 6 7 9 12 99 8383 -99
The largest number is 8383
The smallest number is 3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.