Click here to Skip to main content
15,918,976 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have
Int RangeSum(int start, int end)
{
Int i, sum=0;

Some one can explained for me how I need to do this?
It should return the sum of odd numbers within the range

What I have tried:

I tried with for function but it’s doesn’t work, how I can convert it in if function? Or maybe have another way for beginners? Without scanf and printf

for (i=0; I<10; I++)
{
If (i%2!=0)
{
Sum=sum+i;
}
}
Return sum;
Posted
Updated 8-Dec-22 4:33am

I guess your code is this:
C++
Int RangeSum(int start, int end) {
	Int i, sum=0;
	for (i=0; I<10; I++) 	{
		If (i%2!=0) 		{
			Sum=sum+i;
		}
	}
	Return sum;
}

You want the sum of odds between start and end.
The problem is that you don't use those variables in the for loop.
Your for loop goes from 0 to 10, and you want the loop to go from start to end.
Solution should be rather easy.
 
Share this answer
 
Comments
Richard MacCutchan 8-Dec-22 10:14am    
All those capital letters are sure going to confuse the C compiler.
Patrice T 8-Dec-22 10:21am    
Just copied code from question :) and gave hint about the solution.
Never meant to do all the work.
Your code is broadly correct, but all those capital letters in type and variable names will cause compilation errors. Also, your for loop needs to be from start to end; I show it as inclusive of the end value.
C++
int RangeSum(int start, int end)
{
    int i, sum=0;
    for (i = start; i <= end; i++)
    {
        if (i % 2 != 0)
        {
            sum = sum + i;
        }
    }
    return sum;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900