# Thread: C Programming Need Help with While Loop Sum of Odd numbers

1. ## C Programming Need Help with While Loop Sum of Odd numbers

Hi, I am currently working on an assignment where I am suppose to be creating a loop that outputs the sum of all odd numbers between 20 and 100. I thought my coding was right however the program is displaying the answer is 2480 instead of the correct answer of 2400. My code is posted below: Thanks!
---------------------------------------------------------------------------
insert
Code:
```//Declared variables for program and initializing
int odd = 19, number = 2, result = 0, result2 = 0, totalOdd = 0;

do{

result = odd + number;

//The first result adds 19 and 2 in which equals to 21
result2 = result + number;

//The second result adds 21 + 2, which equals 23, and 2 is incremented by 1
number++;
totalOdd += result2; //Calculates the total sum of the odd numbers

}while (result2 > 20 && result2 < 100);//Condition is set for result2 to be greater than 20, but less than 100
printf("The sum of the odd numbers between 20 and 100 is %i\n", totalOdd);
//Outputs the total sum``` 2. Wow, that is hard to follow code for a newbie to write.
I take it you are not allowed to use a for loop.
I am surprised the answer is so close to being correct.

Tim S. 3. Originally Posted by MackAttack Hi, I am currently working on an assignment where I am suppose to be creating a loop that outputs the sum of all odd numbers between 20 and 100. I thought my coding was right however the program is displaying the answer is 2480 instead of the correct answer of 2400. My code is posted below: Thanks!
---------------------------------------------------------------------------
insert
Code:
```//Declared variables for program and initializing
int odd = 19, number = 2, result = 0, result2 = 0, totalOdd = 0;

do{

result = odd + number;

//The first result adds 19 and 2 in which equals to 21
result2 = result + number;

//The second result adds 21 + 2, which equals 23, and 2 is incremented by 1
number++;
totalOdd += result2; //Calculates the total sum of the odd numbers

}while (result2 > 20 && result2 < 100);//Condition is set for result2 to be greater than 20, but less than 100
printf("The sum of the odd numbers between 20 and 100 is %i\n", totalOdd);
//Outputs the total sum```
Code:
```int isOdd(int value)
{
return value % 2 == 0;
}```
Now loop through your numbers and increment a counter if the test passes. 4. Originally Posted by Sir Galahad Code:
```int isOdd(int value)
{
return value % 2 == 0;
}```
Now loop through your numbers and increment a counter if the test passes.
Assuming that stahta01 is right to guess that this assignment stipulates the use of a do while loop rather than a more natural for loop or computing with a closed form expression, I think it'll be simpler to just keep odd and totalOdd, start odd at 21 then construct the loop condition with += to get the next odd number while checking to see if that next odd number is less than 100. 5. Odd numbers between 20 and 100 are between 21 (not 19) and 99:

Code:
```#include <stdio.h>

int main( void )
{
int n = 21;   // first odd value between 20 and 100.
int sum = 0;

do
{
sum += n;
n += 2;     // next odd value.
} while ( n < 100 );

printf( "%d\n", sum );
}``` 6. Originally Posted by flp1969 Odd numbers between 20 and 100 are between 21 (not 19) and 99:

Code:
```#include <stdio.h>

int main( void )
{
int n = 21;   // first odd value between 20 and 100.
int sum = 0;

do
{
sum += n;
n += 2;     // next odd value.
} while ( n < 100 );

printf( "%d\n", sum );
}```
It's nice to be helpful, but remember, this is a homework assignment... 7. Originally Posted by Sir Galahad It's nice to be helpful, but remember, this is a homework assignment...
Yep, I notice... Since did a good try I thought it wasn't that harmul to show a different approach... Popular pages Recent additions c programming, odd, result, sum of odd numbers, while loop 