Please help , thanks

How can i add the odd numbers between the first number and the second number ?
thanks

[code]
#include<iostream>
using namespace std;

int main()

{
int i,sum=0,firstnum=1,secondnum=0;
char ch;

while(!(firstnum<secondnum))

{
cout<<"Enter starting number: ";
cin>>firstnum;
cout<<"Enter ending number(must be > startingnumber): ";
cin>>secondnum;
}
i=firstnum;
cout<<"The odd numbers between "<<firstnum<<" and"<<secondnum<<" are:\n";
while(i<=secondnum)
{
if(i%2==0)
sum+=i;
else
{
cout<<i <<""<< endl; //print odd numbers

}
i++;
}


system("pause");



return 0;


}
---
Last edited on
Don't do people's homework.
closed account (jh5jz8AR)
Hey Jibbz,

You are almost done with your code. You have your program printing out the odd numbers. All you need to do is figure out how to add them together.

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
27
28
29
30
31
32
33
34
35
36
#include<iostream>

using namespace std;

int main()

{
    int i,sum=0,firstnum=1,secondnum=0;
    char ch;

    while(!(firstnum<secondnum))
    {
        cout<<"Enter starting number: ";
        cin>>firstnum;
        cout<<"Enter ending number(must be > startingnumber): ";
        cin>>secondnum;
    }
    
    i=firstnum;
    cout<<"The odd numbers between "<<firstnum<<" and"<<secondnum<<" are:\n";
    
    while(i<=secondnum)
    {
        if(i%2==0)
            sum+=i;
        else
        {
            cout<<i <<""<< endl; //print odd numbers
            //  add the odd numbers (i) here
        }
        i++;
    }
    
    system("pause");
    return 0;
}
Topic archived. No new replies allowed.