954,184 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Loop counting odd and even numbers

i have a program that has one little glitch but i dont know how to fix it. If you can help it would be great.

int main()
{
int a, b, c;
b=0;
c=0;
do
{
cout << "Please enter a positive integer (negative integer to stop):"<< ' ';
cin>> a;
if (a%2 == 0)
{
c++;
}
else
b++;
}
while (a>=0);
cout << "You have entered" << ' '<< b << ' ' <<"odd numbers." << endl;
cout << "And" << ' ' << c << ' ' <<"even numbers." << endl;
system("PAUSE");
return 0;
}

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.

c++help
Newbie Poster
2 posts since Mar 2004
Reputation Points: 10
Solved Threads: 0
 

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 ;)

Here is a start ...

int main()
{
int input, even=0, odd=0;
do
{
cout << "Please enter a positive integer (negative integer to stop):"<< ' ';
cin>> input;
if ( (even%2 == 0) && (input >= 0) )
  even++;
else if (input >= 0)
  odd++;
}
while (input>=0);

cout << "You have entered" << ' '<< odd << ' ' <<"odd numbers." << endl;
cout << "And" << ' ' << even << ' ' <<"even numbers." << endl;
system("PAUSE");
return 0;
}


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.

cscgal
The Queen of DaniWeb
Administrator
19,421 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 

thanks for the help, I really appreciate it. I have been really struggling with this language. I look forward to being a part of your community.

c++help
Newbie Poster
2 posts since Mar 2004
Reputation Points: 10
Solved Threads: 0
 

Hey there. That's great. Welcome aboard.

cscgal
The Queen of DaniWeb
Administrator
19,421 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 

can you help me make a code on arrays in finding even and odd numbers

avaughn
Newbie Poster
1 post since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

Hi avaughn, welcome to DaniWeb!

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.

David

daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
 

please help me how to determine odd and even number and how can i count all the odd or even numbers?

mcroneil
Newbie Poster
1 post since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

i need some help about the bubble sort. i dont knw how to start.

rheg
Newbie Poster
1 post since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

@mcroneil - Please show us your attempt and we can help you debug it.

@rheg - You should start a new thread when you have a new question.

daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
 

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
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;
}
}
}

clueless215
Newbie Poster
1 post since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

Hi clueless215 - welcome to DaniWeb. Here are a few guidelines which will help you get the most out of the forum.

1) When you have a new question, start a new thread rather than post at the end of someone else. What you have done here is called "thread hijacking" and is frowned upon.

2) Use code tags. That is, surround your code with [ code ] your code [ /code ] (without the spaces inside the brackets. It makes it much easier to read.

3) Don't use abbreviations like 'hmwk'.

As to your question - you have done the hard part already! (Identifying that "%2 = 0" determines if the number is even.) Now you just have to make two sum variables (sumEven and sumOdd) and use them to store the count along the way (sumEven += number).

Good luck,

David

daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You