+ 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?
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);
}
+ 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);
}
+ 7
nAutAxH AhmAd Okay.
But suppose if I want to take the numbers from user then what can I do?
+ 4
nAutAxH AhmAd It's working
Thanks for your help😊
+ 4
You could use the max() or min() method
+ 4
emmanuel Thanks for help😊
+ 3
You could use booleans e. g. (Python):
return "a is largest" * (a>b and a>c) etc.
+ 3
((a+b)+abs(a-b))/2 will yield the larger of a or b.
Without using min/max or the ">" or "<" operators
+ 3
Udi Finkelstein Thank You
+ 3
You can use ternary operator
That is my code
https://code.sololearn.com/c7PRT09bMHa7/?ref=app
+ 1
Min(a>b)?a:b;
Use these concept
- 1
Thanks....)))))))