| | |
Loop counting odd and even numbers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2004
Posts: 2
Reputation:
Solved Threads: 0
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.
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++ Syntax (Toggle Plain Text)
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.
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 ...
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.

Here is a start ...
C++ Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- switch, and while loop outputting integer numbers and their squares (C++)
- odd even numbers (C++)
- making counter for odd numbers (C++)
- Adding the sum of odd numbers. Please help (Visual Basic 4 / 5 / 6)
- (Noob) need some help w/ Prime Numbers (C++)
- print odd and even numbers (Assembly)
Other Threads in the C++ Forum
- Previous Thread: C++ Improving my code - semaphores and mutex
- Next Thread: infinite loop
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets







