+ 1

Why is wrong in my code ??(Max of 2 no.) Tell the point at which my code went wrong !!

#include <iostream> using namespace std; int max(int num1, int num2) { //complete the function if(num1>=num2 || num2>=num1){ return max(num1,num1); } } int main() { //getting inputs int first; cin >> first; int second; cin >> second; cout<<max(first,second); //call the function and print result return 0; }

14th Feb 2022, 8:06 AM
Sabnis Ashutosh
Sabnis Ashutosh - avatar
5 Respuestas
+ 7
if(num1 >= num2 || num2 >= num1) always evaluates true because of the combined conditions. You may try using if-else statements and return the max number
14th Feb 2022, 8:50 AM
Simba
Simba - avatar
+ 2
int max(int num1, int num2){ if(num1>=num2){ return num1; } return num2; }
14th Feb 2022, 8:51 AM
Mohammadmehdi Karami
Mohammadmehdi Karami - avatar
+ 2
You may use the code like this, You cant return a function using int function. You have to return int value. You have to check your condition for case by case. #include <iostream> using namespace std; int max(int num1, int num2) { //complete the function if(num1>=num2 ) //if num1>=num2 max is num1 { return num1;} else //else max is num2 return num2; } int main() { //getting inputs int first; cin >> first; int second; cin >> second; int maximum =max(first,second); cout<<maximum; //call the function and print result return 0; }
15th Feb 2022, 7:46 AM
pubudi kaushalya
+ 1
Thank You !!!
14th Feb 2022, 3:04 PM
Sabnis Ashutosh
Sabnis Ashutosh - avatar