+ 8

Can anyone tell me How can we find out greatest among two or three numbers without using any conditional operators or statements

As I going to solve tricky programs I found this program. Can anyone tell me how can I solve this?

20th Nov 2018, 11:24 AM
Prajakta
Prajakta - avatar
12 odpowiedzi
+ 15
You can ask for input after declaring variables #include <iostream> using namespace std; bool isGreater(int num1, int num2); int main() { int num1 = 0, num2 = 0; cin >> num1; cout << "First number entered: " << num1 << endl; cin >> num2; cout << "Second number entered: " << num2 << endl; if(isGreater(num1, num2)) cout << num1 << " is greater than " << num2 << endl; else cout << num2 << " is greater than " << num1 << endl; return 0; } bool isGreater(int num1, int num2) { return (num1 > num2); }
21st Nov 2018, 10:04 AM
blACk sh4d0w
blACk sh4d0w - avatar
+ 9
In my opinion, this could be the way to achieve the goal. #include <iostream> using namespace std; bool isGreater(int num1, int num2); int main() { int num1 = 25, num2 = 50; if(isGreater(num1, num2)) cout << num1 << " is greater than " << num2 << endl; else cout << num2 << " is greater than " << num1 << endl; return 0; } bool isGreater(int num1, int num2) { return (num1 > num2); }
20th Nov 2018, 4:56 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 7
nAutAxH AhmAd Okay. But suppose if I want to take the numbers from user then what can I do?
21st Nov 2018, 4:20 AM
Prajakta
Prajakta - avatar
+ 4
nAutAxH AhmAd It's working Thanks for your help😊
21st Nov 2018, 10:35 AM
Prajakta
Prajakta - avatar
+ 4
You could use the max() or min() method
21st Nov 2018, 4:24 PM
emmanuel
emmanuel - avatar
+ 4
emmanuel Thanks for help😊
22nd Nov 2018, 1:50 AM
Prajakta
Prajakta - avatar
+ 3
You could use booleans e. g. (Python): return "a is largest" * (a>b and a>c) etc.
20th Nov 2018, 12:15 PM
David Ashton
David Ashton - avatar
+ 3
((a+b)+abs(a-b))/2 will yield the larger of a or b. Without using min/max or the ">" or "<" operators
21st Nov 2018, 9:59 PM
Udi Finkelstein
Udi Finkelstein - avatar
+ 3
Udi Finkelstein Thank You
22nd Nov 2018, 1:50 AM
Prajakta
Prajakta - avatar
+ 3
You can use ternary operator That is my code https://code.sololearn.com/c7PRT09bMHa7/?ref=app
17th Dec 2018, 2:24 PM
Hafsa Mohamed
Hafsa Mohamed - avatar
+ 1
Min(a>b)?a:b; Use these concept
24th Dec 2018, 7:38 AM
Jkilop
- 1
Thanks....)))))))
22nd Nov 2018, 9:04 PM
SheVa™