Help with outputting the max and min number using If Else

Nov 26, 2016 at 1:36pm
Hi there new to the forum and new to c++ and programming (been doing it for 2 days)
Anyway I am trying to self learn using a book and an online course and i'm stuck with one of the practice problems.

the problem is: Write a program that takes in 3 numbers and outputs the biggest number and the smallest number using if else.

so far i only have it outputting the biggest of the 3 but can't figure out how to get the smallest as well.
I know this is probably really easy but my brain just feels fried at the moment and i cant think straight.

Any tips or advice pointing me in the right direction would be greatly appreciated.

My code so far.

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
#include <iostream>
using namespace std;

int main()
{
	int num1 = 0;
	int num2 = 0;
	int num3 = 0;

	cout << "Please enter 3 numbers leaving a space between each: ";
	cin >> num1 >> num2 >> num3;

	if (num1 > num2 && num1 > num3) 
	{
		cout << num1 << " is the biggest value." << endl;
	}
	else if (num2 > num1 && num2 > num3)
	{
		cout << num2 << " is the biggest value" << endl;
	}
	else if (num3 > num1 && num3 > num2)
	{
		cout << num3 << " is the biggest value" << endl;
		
	}
	else
	{
		cout << "Numbers are not distinct" << endl;
	}

	return 0;
}
Nov 26, 2016 at 2:03pm
#include <iostream>
using namespace std;

int main()
{
int num1 = 0;
int num2 = 0;
int num3 = 0;

cout << "Please enter 3 numbers leaving a space between each: ";
cin >> num1 >> num2 >> num3;

if (num1 > num2 && num1 > num3)
{
cout << num1 << " is the biggest value." << endl;
}
else if (num2 > num1 && num2 > num3)
{
cout << num2 << " is the biggest value" << endl;
}
else if (num3 > num1 && num3 > num2)
{
cout << num3 << " is the biggest value" << endl;

}
else
{
cout << "Numbers are not distinct" << endl;
}

if (num1 < num2 && num1 < num3)
{
cout << num1 << " is the smallest value." << endl;
}
else if (num2 < num1 && num2 < num3)
{
cout << num2 << " is the smallest value" << endl;
}
else if (num3 < num1 && num3 < num2)
{
cout << num3 << " is the smallest value" << endl;

}


return 0;
}
Nov 26, 2016 at 2:11pm
Thank you nisor
i thought about this but was trying to think of a way with less code involved
but this has helped me a lot, i should probably just do what i think and not try to ake things complicated for myself.
thanks
Nov 26, 2016 at 3:24pm
What about creating a function :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int max(int x, int y, int z) {
        if(x>y  && x>z) return x;
        else if(y>x && y>z) return y;
        else return z;
}

int main() {
        int num1,num2,num3;
        cout << "Please enter 3 numbers leaving a space between each: ";
	cin >> num1 >> num2 >> num3;

        int maximum = max(num1,num2,num3);
        cout << maximum << endl
}


We supose that input numbers are integers, else in function max should be double type of variables.
You will understand better with function.
Best Regards.
Last edited on Nov 26, 2016 at 3:26pm
Nov 26, 2016 at 4:40pm
The C++11 implementation of function std::max(),
as well as the usual std::max(num1, num2)
allows an initialiser list as a parameter.
 
#include <algorithm> 

 
    int biggest = std::max({num1, num2, num3});

this can handle any number of items.
http://www.cplusplus.com/reference/algorithm/max/

There is also the related std::max_element which could be used with a range of values in an array for example.
http://www.cplusplus.com/reference/algorithm/max_element/
Nov 26, 2016 at 5:25pm

just change greater sign with less than

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace std;

int main()
{
	int num1 = 0;
	int num2 = 0;
	int num3 = 0;

	cout << "Please enter 3 numbers leaving a space between each: ";
	cin >> num1 >> num2 >> num3;

	if (num1 > num2 && num1 > num3) 
	{
		cout << num1 << " is the biggest value." << endl;
	}
	else if (num2 > num1 && num2 > num3)
	{
		cout << num2 << " is the biggest value" << endl;
	}
	else if (num3 > num1 && num3 > num2)
	{
		cout << num3 << " is the biggest value" << endl;
		
	}
	else
	{
		cout << "Numbers are not distinct" << endl;
	}
	
	if (num1 < num2 && num1 < num3)  // just change greater sign with less than 
	{
		cout << num1 << " is the smallest value." << endl;
	}
	else if (num2 < num1 && num2 < num3)
	{
		cout << num2 << " is the smallest value" << endl;
	}
	else if (num3 < num1 && num3 < num2)
	{
		cout << num3 << " is the smallest value" << endl;
		
	}
	else
	{
		cout << "Numbers are not distinct" << endl;
	}
	
	

	return 0;
}
Nov 26, 2016 at 5:33pm
without using and operator

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
using namespace std;

int main()
{
	int num1 = 0;
	int num2 = 0;
	int num3 = 0;

	cout << "Please enter 3 numbers leaving a space between each: ";
	cin >> num1 >> num2 >> num3;
	// greater number
	if(num1>num2)
	{
		if(num1>num3)
		{
			cout<<num1<<" is the greatest number"<<endl;
		}
		else
		{
			cout<<num3<<" is the greatest number"<<endl;
		}
	}
	
	else if(num2>num3)
	{
		cout<<num2<<" is the greatest number"<<endl;
	}
	else
	{
		cout<<num3<<" is the greatest number"<<endl;
	}
	
	
	// smallest number
	
	
	
	if(num1<num2)
	{
		if(num1<num3)
		{
			cout<<num1<<" is the smallest number"<<endl;
		}
		else
		{
			cout<<num3<<" is the smallest number"<<endl;
		}
	}
	
	else if(num2<num3)
	{
		cout<<num2<<" is the smallest number"<<endl;
	}
	else
	{
		cout<<num3<<" is the smallest number"<<endl;
	}
	
	
	
	
	

	return 0;
}
Nov 26, 2016 at 8:15pm
Thanks for all the help guys, problem solved.
really appreciate the replys
Topic archived. No new replies allowed.