here is the program. the problem is if i put in these numbers 3,1037,60,-43. i get a answer of 3 odd numbers and 1 even numbers. it should say 2 odd and 1 even. the program is counting the negative number as an odd where it should just end the program.
It's a good start. Let me just tell you, the first thing I noticed was you have variables a, b, and c. But what are they? What do they represent? Variable names should always be indicative of what they do. Also, comments would help
Note that my if statement has a conditional to make sure not to include the negative number. This is the easiest quick fix considering you're using a do while loop like this. You could also just use a nested if statement.
Another way to do it is to prompt the user to enter a first number. And then from then on use a while loop instead of a do while loop, where the prompt to enter a number is on the last line of the loop.
If you look at the post by cscgal above, you will see all of the elements that you need. The "if number%2 == 0" test is how to tell if a number is even or odd. Then you just have to keep track of which ones are which! Please give it a try and post some code and we can help if you get stuck.
Okay so I'm taking a computer science class and I have no idea what I'm doing! The hmwk is to design a program that iterates through integers from 1 to a value determined by user and add the odd numbers in the range and the even numbers in the range and output the sum for both. I am clueless as to how to get the program to add only the odds together and only the evens together... please help. This is what I have thus far...
#include <iostream>
using namespace std;
void main(void)
{
int number, max, increment;
cout << "Enter the max value: ";
cin >> max;
cout << "Enter the increment: ";
cin >> increment;
for(number = 1; number <= max; number = number + increment)
{
cout << number << endl;
if((number%2) == 0)
{
cout << "This number is even." << endl;
}
else if((number%2) == 1)
{
cout << "This number is odd." << endl;
}
else
{
cout << "Error." << endl;
}
}
}
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.