How to find the max and min from three numbers with short-circuit boolean operators in C++

1 Answer

0 votes
#include <iostream>

using namespace std;
 
int max_3(int a, int b, int c) {
	int max = a;
	 
	(max < b) && (max = b); 
	 
	(max < c) && (max = c); 
 
	return max;
}
 
int min_3(int a, int b, int c) {
	int min = a;
	 
	(min > b) && (min = b);
	 
	(min > c) && (min = c);
	 
	return min;
}
 
int main()
{
	cout << max_3(8, 9, 3) << endl;
	cout << min_3(9, 7, 4) << endl;
	 
	return 0;
}



/*
run:

9
4

*/

 



answered Mar 25, 2019 by avibootz
edited Mar 25, 2019 by avibootz

Related questions

3 answers 336 views
1 answer 237 views
1 answer 169 views
1 answer 265 views
1 answer 258 views
...