Finding the two largest numbers

How can I find what is the two largest variables out of three, without using arrays.
1
2
3
4
5
int x,y,z;
int largestNum;

if ((x > y) && (x > z))
{// X is largest} 


Do the same for Y and Z.
Last edited on
Thank you, but how do I sign first largest and second?
1
2
3
4
5
6
7
8
9
10
11
12
13
int x, y, z;

if ((x > y) && (x > z))
{
        if (y > z)
        {
                cout << x << y << endl;
        }
        else
        {
                 cout << x << z << endl;
        }
}


This outputs two largest of three

Same goes for y and z and z and x
Lemonsugar that doesn't account for z
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
    int a, b, c ;
    std::cout << "enter three integers: " ;
    std::cin >> a >> b >> c ;

    // if a is smaller than b, swap a and b
    if( a < b ) { int temp = a ; a = b ; b = temp ; }
    // at this point, we know that a is not smaller than b
    // if b is smaller than c, swap b and c
    if( b < c ) { int temp = b ; b = c ; c = temp ; }
    // at this point, we know that c is not larger than either a or b

    // print out a and b; both are not smaller than c
    std::cout << "the largest two are: " << a << " and " << b << '\n' ;
}
Last edited on
Topic archived. No new replies allowed.