find a largest number

I got a problem here.

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:

 ``12`` `````` Please enter three numbers :-10 15 600 The largest number is: 600 ``````

anyway,use IF-Then-Else statement

thank you very much
Last edited on
this is what i wrote , see has any problem?

thank you!

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````# include using namespace 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 && num2 > num3) { cout<< " The largest number is：" < num1 && num3 > num2) { cout<< " The largest numberis：" <
You should indent your code. Also, you have some unneeded brackets:

 ``12345678910111213141516171819202122`` ``````#include using namespace 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; else if (num2 > num1 && num2 > num3) cout << "The largest number is：" << num2 << endl; else if (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.
Last edited on
you have to extend you code more:

 ``12345678910111213141516171819202122`` ``````#include using namespace 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; else if (num2 > =num1 && num2 > =num3) cout << "The largest number is：" << num2 << endl; else if (num3 >= num1 && num3 > =num2) cout << "The largest number is：" << num3 << endl; return 0; }``````
BIg thanks!! I got it!
Topic archived. No new replies allowed.