I trying to write a code that reads a set of integers and then finds and prints the sum of the even and odd integers. The numbers will be read in from the user via the cin statement. You do not know how many numbers the user may enter. When the user enters a 0 (zero), then the user is done entering numbers. I have most of the code finished, but i can't seem to get it to accuretly display when I enter the zero. This is what i have so far.

#include <iostream>

#include <cmath>

using namespace std;

int main()

{

    int even = 0;

    int odd = 0;

    int number;

    int figure;

    cout << "Insert your numbers and press enter" << endl;
    cout<<"0 will terminate the program"<<endl;

    cin >> number;

    while (number != 0 )

    {

        figure = number % 10;

        number = number / 10;

if(figure % 2 == 0)

        even = even + figure;

        else

        odd = odd + figure;

}
    cout << "The sum of the even integers is: " << even << endl;

    cout << "The sum of the odd integers is: " << odd << endl;

return 0;

}

Recommended Answers

You are not even close to being done.

  1. You only get input from the user once instead of within a loop
  2. You do not need both number and figure according to your spec
  3. Why are you doing a div and a mod?

You should be doing

Jump to Post

All 3 Replies

You are not even close to being done.

  1. You only get input from the user once instead of within a loop
  2. You do not need both number and figure according to your spec
  3. Why are you doing a div and a mod?

You should be doing

do {
    show prompt
    get input

    process even or odd

} while not zero

Using proper indentation will help you to see part of the problem.

why did you write number = number / 10;
if you remove this, the program may work

if you remove this, the program may work

No. It won't. Not even close.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.