Answer to Question #194103 in C++ for Priyanshu

Question #194103

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++


1
Expert's answer
2021-05-16T23:49:24-0400
#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;
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS