View Single Post
Join Date: Sep 2008
Posts: 21
Reputation: ShadowOfBlood is an unknown quantity at this point 
Solved Threads: 0
ShadowOfBlood ShadowOfBlood is offline Offline
Newbie Poster

Re: Trouble with loops - Calculate sum of even and odd integers

 
0
  #4
Oct 6th, 2008
Nvm, I was going about it wrong. I figured it out. Here's what I have:

  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int even = 0, odd = 0, dig, num;
  9.  
  10. cout << "Please enter a list of numbers (ex. - 12345)" << endl;
  11. cin >> num;
  12.  
  13. while (num > 0)
  14. {
  15. dig = num % 10;
  16. num = num / 10;
  17.  
  18. if(dig % 2 == 0)
  19. even = even + dig;
  20. else
  21. odd = odd + dig;
  22. }
  23.  
  24. cout << "The sum of the even integers is: " << even << endl;
  25. cout << "The sum of the odd integers is: " << odd << endl;
  26.  
  27. return 0;
  28. }

Thanks for the help
Reply With Quote