How do I compute sum of odd numbers?

This program accepts two integers and calculates the sum of all odd
numbers that are between these two integers. That is, the sum of all odd numbers
that are greater than or equal to the first integer, and less than or equal to the second integer. I must use a count-controlled while loop. I am stuck here.
what should I change to make this program work?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 #include <iostream>
using namespace std;

void main()
{
   int intone, inttwo, number, sum;
   sum = 0;
   
   cout << "Enter the first integer: ";
   cin >> intone;
   
   cout << "Enter the second integer: ";
   cin >> inttwo;
   
   for (number = intone; number <= inttwo; number++)
   while ( intone % 2 != 0 && inttwo % 2 != 0)
   {
      sum = sum + inttwo;
   }

   
   
   cout << "The sum of all odd numbers between " << intone << " and " 
        << inttwo << " is " << sum << ".";
}


Thanks!
Last edited on
instead of a while loop within your for loop, use if statement:

1
2
3
4
5
6
7
for(number = intone; number <= inttwo; number++)
{
       if(number % 2 != 0)
       {
             sum += number;
        }
}
I can't use if statement, I must use while loop. Is there any way to do it even without for loop?
I would start with line 15, what exactly was that supposed to do?
basically what you said with this code: for (number = intone; number <= inttwo; number++)
is if you input number1 : 4, and number2: 8
it would start the loop with number equaling 4, then it will increment by 1, until number equals 8.
if you output you would probably get something like

number: 4
number: 5
number: 6
number: 7
number: 8

fix number two you could do:
line 18 code: sum = sum + inttwo;
is the same thing as sum += inttwo;
but im pretty sure you meant something like this
sum = intone + inttwo //setting sum1 equal to number 1 + number 2.
fix number 3 you could do:
line 16 code: while ( intone %2 != 0 && inttwo % 2 != 0)
is a very bad idea that would mean if both the numbers are odd you want to set the sum equal to the sum of the two numbers for infinte amount of time.
you could either a) use an if statement instead of while
or b) put a break at the end of the while loop.
if you are trying to check if the number is an odd by that
and basically you said that if both numbers are odds you want to add them together.
but if you want to add each odd number seperatly to the total sum you would have to do something like
1
2
if(intone % 2 == 1) sum += intone; // or != 0
if(inttwo %2 == 1) sum += inttwo; // or != 0 

uhmmm...try this maybe?

1
2
3
4
5
6
7
8
9
10
11
number = intone;
while(number <= inttwo)
{
        while(number % 2 != 0)
        {
                 sum += number;
                 number++;
                 continue;
        }
        number++;
}


I don't really see why you can't use if statements. They are one of the main things that make a program easy to do things with under certain conditions.
Last edited on
Thanks for the help guys, I finally got it right!
@crimsonzero2 you could also just do this
1
2
3
4
5
 while ( intone % 2 != 0 && inttwo % 2 != 0)
   {
      sum = intone + inttwo;
      break; //ends the while loop there and its pointless to use a while loop if you are only doing it once imo...
   }
Last edited on
Try this

1
2
3
  for (number = intone; number <= inttwo; number++)
    if ( number % 2 ) 
      sum += number;
He couldn't use if statements otherwise if we are competing on the least amount of lines you could just do this
if(intone % 2 != 0 && inttwo % 2 != 2) sum = intone + inttwo;
@giblit he's trying to add the odd numbers between the two numbers given by the user
oh I see missunderstood sorry makes more sense on why he is using a loop lol
Topic archived. No new replies allowed.