View Single Post
Join Date: Oct 2008
Posts: 118
Reputation: chococrack is on a distinguished road 
Solved Threads: 14
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

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

 
0
  #8
Oct 6th, 2008
  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 (-7777 to terminate)" << endl;
  11. cin >> num;
  12.  
  13. while (num != -7777) // off the wall check value
  14. {
  15.  
  16. // dig = num % 10;
  17. // num = num / 10; <--- what are these for?
  18.  
  19.  
  20.  
  21. if(dig % 2 == 0) // if the number is even
  22. even += num; // add it to the previous even sum
  23. else
  24. odd += num; // otherwise add it to the odd sum
  25.  
  26. cout << "Enter next number: ";
  27. cin >> num;
  28. }
  29.  
  30. cout << "The sum of the even integers is: " << even << endl;
  31. cout << "The sum of the odd integers is: " << odd << endl;
  32.  
  33. return 0;
  34. }
I would love to change the world, but they won't give me the source code
Reply With Quote