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:

1
2
 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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# include <iostream>
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 numberis:" <<num3 <<endl; 
}

}

return 0;
}
You should indent your code. Also, you have some unneeded brackets:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

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.