In: Computer Science
IN C++
Write a program that reads in int values from the user until they
enter a negative number like -1. Once the user has finished
entering numbers, print out the highest value they’ve entered, the
lowest value they’ve
entered, and the total number of numbers they’ve entered. The
negative number they entered should not be taken as one of the
values entered.
Explanation:
Here is the code which takes the values from the user until a negative number is entered.
And then prints the highest value, lowest value and the sum of the values entered.
Code:
#include <iostream>
using namespace std;
int main()
{
int n;
int sum = 0;
cout<<"Enter values (Enter any negative number to stop):
";
cin>>n;
int highest = n, lowest = n;
while(n>=0)
{
sum = sum + n;
if(n>highest)
highest = n;
if(n<lowest)
lowest = n;
cin>>n;
}
cout<<"Highest entered: "<<highest<<endl;
cout<<"Lowest entered: "<<lowest<<endl;
cout<<"Sum: "<<sum<<endl;
return 0;
}
output:
PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
PLEASE COMMENT IF YOU NEED ANY HELP!