Show answer step by step Write a value returning function named sumofodds that t
ID: 3843486 • Letter: S
Question
Show answer step by step Write a value returning function named sumofodds that takes two int parameters. It returns the sum of the squares of all odd numbers between the first and second parameter. For example, if the first parameter is 6 and the second is 13, the function returns the value 7 + 9 + 11 + 13. If the parameters are 5 and 8, the function returns the value of 5 + 7. If the parameters are 13 and 2, the first parameter is larger, so the function returns theta. (a) Give the function prototype: (b) Give an example of the function call: (c) Write the function definition(header an body):Explanation / Answer
Ans (a) Function Prototype:
int sumOfOdds(int a, int b);
Ans (b) Function call
int sum= sumOfOdds(a,b); // for calling the function we take value of a and b as input using cin>>a>>b;
// a and b are two numbers , we want to calculate the sum of odds in between a and b
Ans (c) Function Body
int sumOfOdds(int a, int b)
{
if(a>b)
{
return 0;
}
else
{
int sum=0;
for(int i=a;i<=b;i++)
{
if(i%2!=0)
{
sum+=i;
}
}
return sum;
}
}