
adding odd numbers only
Hello all,
Im new here and was hoping to get a bit of help with a program i am writing for class. The program has the user enter a start number then a ending number. Then displays only the Odd number. Ive been successful up to that point. The next part has been giving me a bit of a headache. Can you help?
After the odd numbers are displayed the program needs to Add those numbers together and display the sum. So if the user enters in 1 and 10 it will display 1,3,5,7,9 then 25 for the total sum. Any guidance would be much appreciated and I hope to hang around here and help anyone that might need it in the future.
Code:
#include <iostream.h>
#include <ctype.h>
#include <math.h>
void main()
{
char doAgn = 'Y';
int startNum= 0;
int endNum = 0;
int oddCount = 0;
int sum = 0;
int sumNum = 0;
while (toupper(doAgn) =='Y'){
//get inputs
cout<<"Please enter the beginning number:";
cin>>startNum;
cout<<"Please enter the ending number:";
cin>>endNum;
//display the odd numbers
for (int i = startNum; i < endNum; i++){
oddCount++;
if(oddCount % 2 !=0)
cout<<oddCount<<endl;
}//end For
//display the total sum for the odd numbers displayed
????????
//display the average for all the odd numbers displayed
//hopefully i will grasp this after understanding the above line of code.
//clear out odd count
oddCount = 0;
}//end while
}//end main

keep track of the sum in a variable. Keep on adding to it whenever you find an odd number

thats what i figured needs to happen. The problem for me is how do i code after the (oddCount % 2 !=0) finds the odd number once then again "then make those two numbers add" up that I can store in a variable. thanks for the point in the right direction skorman00.

if that test comes out to true, you know you have an odd number right? In addition to displaying it, also add it to the running sum.

You wrote:
for (int i = startNum; i < endNum; i++){
oddCount++;
if(oddCount % 2 !=0)
cout<<oddCount<<endl;
Try this:
for (int i = startNum; i < endNum; i++)
{
if(oddCount % 2 !=0)
{
oddCount++;
cout<<oddCount<<endl;
}
}
Hope this helps,
BigSteve

thank you for your help. I'll keep thinking this one through :)

oh i see bigSteve let me try that real quick

well now that doesnt display my oddCount ... i guess this is one to sleep on or something

that threw you into an infinite loop. You only incrimented oddCount if it was odd, so it would just stop incrimenting if it became even. I'll now point out that you are using oddCount to keep track of the odd numbers, which starts at 0, not the number you got from the user. Keep track of that with i instead, which you have set up properly.

What is the point of oddCount?
Here is some pseudocode/logic to your program. I hope it helps.
Code:
while still getting input
{
declare total variable
get starting number
get ending number
for i = from startingnumber to endingnumber
{
if i is an odd number
{
print i
total += i
}
}
print total
}

Thanks peoples for the help. My c++ book doesnt have a thing about the += operator in it :( Thats what was causing me the problems. How would you achieve the same effect without it?

