write a function that takes two int arguments and returns reference of the odd number out of two.If both the arguments are odd,then the reference of the smaller one is returned in c++
#include<iostream>
using namespace std;
//Function declaration
int& refFunction(int& num1, int& num2);
//Driver program
int main(){
int x = 4;
int y = 9;
cout<<(refFunction(x, y));
return 0;
}
//Function definition
int& refFunction(int& num1, int& num2)
{
//If num 1 is odd and 2 even
if(num1%2 != 0 && num2%2 == 0)
{
return num1;
}
//If num1 is even and num2 is odd
else if(num1%2 == 0 && num2%2 != 0)
{
return num2;
}
//If both are odd
else
{
//Return the small one
return (num1 < num2) ? num1 : num2;
}
}
Comments
Leave a comment