+ 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; }
5 Antworten
+ 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
+ 2
int max(int num1, int num2){
if(num1>=num2){
return num1;
}
return num2;
}
+ 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;
}
+ 1
Thank You !!!