0

I have made it to the point it separates every digit into two sides so as to have two different sums, but then It gives me the wrong value of the sum. I'm pretty sure there is a bug in therms of it getting both sides together, here's a section of my commented code for that part.

bool isodd = true;
int oddsum = 0;
int evensum = 0;

// gives me an error
do
{
    int digit = number % 10;

I made this part so as to have switching sides when the digit is in an odd or even position, It gives me the correct values as compared to the example (multiplies the odd positions by two and doesn't for the even) but when I try to sum them it doesn't give me the expected values.

    if (isodd == true)
    {
        printf("oddsum is %i ", oddsum);
        oddsum  = oddsum + digit;
        isodd = false;
    }
    else
    {
        digit = digit * 2;
        printf("even sum is %i ", evensum);
        evensum = evensum + digit;
        isodd = true;
    }
}
while (number /= 10);
  • 2
    Can you give an example of your error, the input number, the erroneous output data and what you believe to be the correct data? More code would help too. – Cliff B Aug 27 at 21:46
  • Sure, I'll get on that immediately. – paula.em.lafon Aug 27 at 23:22
0

It turned out I hadn't read the instructions propperly, the right way to do it was traversing through each character on evensum. The corrected loop looks as follows:

do { int digit = number % 10;

    if (isodd == true)
    {
        oddsum  = oddsum + digit;
        isodd = false;
    }
    else
    {
        digit = digit * 2;
        do
        {
            int separated = digit % 10;
            evensum = evensum + separated;
        }
        while (digit /= 10);

        isodd = true;
    }

This way each second number product of two is separated in it's digits, as the algorithm requires.

Thank you everyone!

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .