I need to write a program that will read three numbers as input and
Output the largest number of the three input values
The output should be similar to:
1 2
Please enter three numbers :-10 15 600
The largest number is: 600
#include <iostream>
usingnamespace std;
int main ()
{
int num1, num2, num3;
cout << "Please enter three numbers : " ;
cin >> num1;
cin >> num2;
cin >> num3;
if (num1 > num2 && num1 > num3)
cout << "The largest number is:" << num1 << endl;
elseif (num2 > num1 && num2 > num3)
cout << "The largest number is:" << num2 << endl;
elseif (num3 > num1 && num3 > num2)
cout << "The largest number is:" << num3 << endl;
return 0;
}
As you can see, I've formatted it for easier reading. Other than that, I've done nothing.
You may want to look at std::min and std::max in <algorithm> ( http://cplusplus.com/reference/algorithm/ ), they can shorten your code by a lot. Also, what happens if the user enters "a b c" when you ask it for numbers? What about "one two three"? Your program will crash. Have a look at istream::bad(), and istream::fail() ( http://cplusplus.com/reference/iostream/istream/ ). Those should help you in handling those errors.