15,920,217 members
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

Solution 2

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;
}```

Solution 1

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.

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.

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

Top Experts
Last 24hrsThis month
 Pete O'Hanlon 60 OriginalGriff 60 Amarnath S 50 Rick York 45 k5054 40
 Pete O'Hanlon 920 OriginalGriff 791 Dave Kreskowiak 475 Richard MacCutchan 320 merano99 280

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