Answer to Question #156277 in C++ for allan

Question #156277

You need to create a program that uses loops to perform an 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-19T01:56:49-0500
#include <iostream>
using namespace std;




int sum(int Num1, int Num2)
{
    int sum = 0;
    for(int i = min(Num1, Num2); i <= max(Num1, Num2); i++)
    {
        if(i%2 == 0)
            sum+=i;
    }
    return sum;
}




int main(){
	int Num1, Num2;
    cin >> Num1 >> Num2;
    int 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