Answer to Question #155270 in C++ for LEEYAH

Question #155270

Output the sum of all odd numbers between Num1 and Num2. (use loop); create a user-defined function called sum(). Declare a variable called sumodd in the main() for the sum(). sum is a value returning function. Use sumodd to hold a returned value.


1
Expert's answer
2021-01-15T05:17:02-0500
#include <iostream>
using namespace std;


int sum(int Num1, int Num2)
{
    if(Num2 < Num1) return 0;
    return (Num2-Num2%2) + sum(Num1, Num2-2);
}


int main(){
	int Num1, Num2;
    cin >> Num1 >> Num2;
    if(Num1 > Num2) swap(Num1, Num2);
    int sumodd;
    sumodd = sum(Num1, Num2);
    std::cout << sumodd;
}




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