+ 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; }
5 Réponses
+ 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;
}
}
+ 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);
}
+ 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';
+ 1
@ Chomtich thank you
+ 1
and i wonder if it is possible to use a single c++ program printing the minimum and maximum at the same time?????