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