Trouble with loops - Calculate sum of even and odd integers
Please support our C++ advertiser: Programming Forums
![]() |
•
•
Posts: 21
Reputation:
Solved Threads: 0
As a homework assignment, I was asked to write a program in C++ that calculates the sum of the even and odd integers in a list of integers given by the user. I think I'm on the right track, but I can't seem to get my loop right. Here's what I have so far:
Thanks for the help
cplusplus Syntax (Toggle Plain Text)
#include <iostream> #include <cmath> using namespace std; int main() { int num=0, even=0, odd=0, i = 0; char stop = 'a'; cout << "Enter a list of integers followed by the '$' sign: " << endl; cin >> num; stop = num; while(stop != '$') { if(num % 2 == 0) //If the number is even even = even + num; //Add it to the current sum of even integers else if(num % 2 != 0) //if the number is odd odd = odd + num; //Add it to the current sum of odd integers else { i = 1; //Otherwise, make i equal to 1 } cin >> num; stop = num; } if(i == 1) cout << "Please enter only integers" << endl; else if(i == 0) { cout << "The sum of the even integers is: " << even << endl; cout << "The sum of the odd integers is: " << odd << endl; } return 0; }
Thanks for the help
Last edited by ShadowOfBlood : Oct 6th, 2008 at 2:43 am.
Look at line 13
Now ask yourself, "self, whats stop?"
"a character."
"self, whats num?"
"an integer."
Whats probably going down is that the the char (stop) is being assigned an integer, which in effect is tossing your loop off.
Are you only interested in positive integers? Because then you could ask for a negative value to terminate that list of integers.
stop = num;
Now ask yourself, "self, whats stop?"
"a character."
"self, whats num?"
"an integer."
Whats probably going down is that the the char (stop) is being assigned an integer, which in effect is tossing your loop off.
Are you only interested in positive integers? Because then you could ask for a negative value to terminate that list of integers.
while(num != -1)
Last edited by chococrack : Oct 6th, 2008 at 2:54 am.
I would love to change the world, but they won't give me the source code
•
•
Posts: 21
Reputation:
Solved Threads: 0
Nvm, I was going about it wrong. I figured it out. Here's what I have:
Thanks for the help
cplusplus Syntax (Toggle Plain Text)
#include <iostream> #include <cmath> using namespace std; int main() { int even = 0, odd = 0, dig, num; cout << "Please enter a list of numbers (ex. - 12345)" << endl; cin >> num; while (num > 0) { dig = num % 10; num = num / 10; if(dig % 2 == 0) even = even + dig; else odd = odd + dig; } cout << "The sum of the even integers is: " << even << endl; cout << "The sum of the odd integers is: " << odd << endl; return 0; }
Thanks for the help
Dont You Think it is giving you the wrong answer?
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
2. Please Use CODE TAGS .
•
•
Posts: 89
Reputation:
Solved Threads: 2
•
•
•
•
Nvm, I was going about it wrong. I figured it out. Here's what I have:
cplusplus Syntax (Toggle Plain Text)
#include <iostream> #include <cmath> using namespace std; int main() { int even = 0, odd = 0, dig, num; cout << "Please enter a list of numbers (ex. - 12345)" << endl; cin >> num; while (num > 0) { dig = num % 10; num = num / 10; if(dig % 2 == 0) even = even + dig; else odd = odd + dig; } cout << "The sum of the even integers is: " << even << endl; cout << "The sum of the odd integers is: " << odd << endl; return 0; }
Thanks for the help
what is quite confusing is the 'num' integer.
You said that you are taking input of a list of integers while you have just asked for a single integer.
I assume that you have taken the input of "list of integers" in a single integer 'num' itself, i.e., you have considered the value of 'num' as a list of integers (digit by digit)

if this is what you have assumed, i think this is a wrong approach. You would have rather used an array.

Think upon it.
Bhoot
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cmath> using namespace std; int main() { int even = 0, odd = 0, dig, num; cout << "Please enter a list of numbers (-7777 to terminate)" << endl; cin >> num; while (num != -7777) // off the wall check value { // dig = num % 10; // num = num / 10; <--- what are these for? if(dig % 2 == 0) // if the number is even even += num; // add it to the previous even sum else odd += num; // otherwise add it to the odd sum cout << "Enter next number: "; cin >> num; } cout << "The sum of the even integers is: " << even << endl; cout << "The sum of the odd integers is: " << odd << endl; return 0; }
I would love to change the world, but they won't give me the source code
![]() |
Other Threads in the C++ Forum
- Previous Thread: Class Link-List help!?
- Next Thread: Comparision Between Tarbo C++, Visual C++
•
•
•
•
Views: 1490 | Replies: 9 | Currently Viewing: 1 (0 members and 1 guests)





Linear Mode