+ 2

For the c++ prog. below i used only (if else) to print the minium of 3 numbers; can i do the same thing but by using ( IF)??

using namespace std; int main () { int a, b, c, cout << "Please enter 3 integer" << endl; cin >> a >> b >> c ; if (a <= b && a <= c ) { cout << "The smallest number is: " << a << endl; } else if (b <= a && b <= c ) { cout << "The smallest number is: " << b << endl; } else if (c <= a && c <= b) {cout << "The smallest number is: " << c << endl; }

7th Jul 2017, 7:05 PM
RiGeL
RiGeL - avatar
5 Respuestas
+ 1
If you don't like to use the ELSE statement: int main () { int a, b, c; cout << "Please enter 3 integer" << endl; cin >> a >> b >> c ; if (a <= b && a <= c ) { cout << "The smallest number is: " << a << endl; } else if (b <= a && b <= c ) { cout << "The smallest number is: " << b << endl; } else if (c <= a && c <= b) { cout << "The smallest number is: " << c << endl; } }
7th Jul 2017, 7:29 PM
Chomitch
Chomitch - avatar
+ 4
Or if you'd like to use ternary operator "?": int Min(int x, int y) { return((x < y) ? x : y); } int main () { int a, b, c; cout << "Please enter 3 integer" << endl; cin >> a >> b >> c ; cout << "The smallest number is: " << Min(Min(a, b), c); }
7th Jul 2017, 7:31 PM
Chomitch
Chomitch - avatar
+ 2
It is ! Add the next line before the main : #define Max(x,y) ((x)<(y)?(y):(x)) And then after the last cout add : cout << "Biggest value : " << Max(Max(a,b),c) << '\n';
7th Jul 2017, 8:13 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
@ Chomtich thank you
7th Jul 2017, 7:55 PM
RiGeL
RiGeL - avatar
+ 1
and i wonder if it is possible to use a single c++ program printing the minimum and maximum at the same time?????
7th Jul 2017, 7:58 PM
RiGeL
RiGeL - avatar