Shah Jacob
Shah Jacob

Reputation: 137

How to iterate through an integer's digits in C?

I need to somehow iterate through an integer in C without any advanced stuff. I don't really understand how modding a number by 10 has to do with this iteration, or how it magically returns the last integer. Can someone explain how to do this and iterate through an integer and show me code in C please?

My code is not working at all and literally just does nothing, there is no output. But please just show me.

#include <stdio.h>
int main (void)
{
    printf("Enter a value.\n");
    int  x = 0;
    scanf("%d", &x);
    while (x < 0)
    {
        printf("Enter another integer please.\n");
        scanf("%d", &x);
    }
    int counter = 0;
    // tries to iterate through freaking integer but literally doesn't work or does nothing
    while (x > 0)
    {
        counter = counter + x;
        x = x % 10;
        
    }
    printf("The total sum of the integer's digits are %d\n", counter);


}

Upvotes: 4

Views: 3763

Answers (1)

Barmar
Barmar

Reputation: 780724

You have several problems in your code.

The first is doing

x = scanf("%d", &x);

scanf() sets its argument x to the input, and then returns the number of variables that it assigned, which is 1. Then the x = assignment replaces the input with 1. So no matter what you enter, you're setting x to 1. Just use scanf("%d", &x) without assigning to the variable as well.

The next problem is your code to loop through the digits. The % operator returns the remainder in a division. So x % 10 returns the remainder when dividing x by 10, which is the last digit of the number (e.g. 123 / 10 returns 12 with a remainder of 3). When you do

x = x % 10;

you get into an infinite loop, because once you get to a single digit, you just keep setting x to itself (unless the last digit is 0, which fails the while (x > 0) condition). That's why your program doesn't seem to do anything -- it never gets out of this loop.

You should be adding the last digit to counter, and then removing the last digit by dividing by 10. So the loop should be:

    while (x > 0)
    {
        counter = counter + x % 10;
        x = x / 10;
    }

The corrected code is:

#include <stdio.h>
int main (void)
{
    printf("Enter a value.\n");
    int  x = 0;
    scanf("%d", &x);
    while (x < 0)
    {
        printf("Enter another integer please.\n");
        scanf("%d", &x);
    }
    int counter = 0;
    while (x > 0)
    {
        counter = counter + x % 10;
        x = x / 10;
    }
    printf("The total sum of the integer's digits are %d\n", counter);
}

Here's how modulus and division iterates through the digits.

Let's say you have a number x = 5649.

When you calculate x % 10, that returns the last digit, which is 9. Then you set x = x / 10;, which sets it to 564 -- the number without the last digit.

The next time through the loop, you calculate x % 10, which returns the last digit of this, which is 4. Then you set x = x / 10;, which sets it to 56 -- again, the number without the last digit.

The next time you calculate x % 10, which returns 6. Then x = x / 10 sets it to 5.

The next time you calculate x % 10, which returns 5. Then x = x / 10 sets it to 0.

Now the loop stops because the condition was x > 0, which is no longer true.

So as you can see, the loop steps through the digits from right to left, because each time it removes the last digit by dividing by 10. Meanwhile, you're adding each digit to counter when you do counter = counter + x % 10;

Upvotes: 7

Related Questions