How do these two while loops to separate an integer into digits work?

Viewed 73

The code is about to separate an integer into digits, the code works, but I am having trouble with how the two "while" work together.

#include <stdio.h>

int main() {
    int num, temp, factor = 1;

    printf("Enter a 5 digit number: ");
    scanf("%d", &num);

    temp = num;
    while (temp) {
        temp = temp / 10;
        factor = factor * 10;
    }

    while (factor > 1) {
        factor = factor / 10;
        printf("%d   ", num / factor);
        num = num % factor;
    }
    return 0;
}
2 Answers

This not so much a programming question as it is a basic arithmetic one, you mention the whiles confuse you but you have to look at the code as a whole:

//...
temp = num;

Stores the original input to be used ahead.


while (temp) {
    temp = temp/10;
    factor = factor*10;
}

This cycle will divide the value temp by 10 until the result is 0 at which point it will evaluate to false and exit the loop, keep in mind that integer division in C truncates de result, e.g. 2/3 ~ 0.67 = 0, the factor is simply accumulates the number of divisions needed to get to 0, in this case 5 times so 100000.


while (factor>1) {
    factor = factor/10;
    printf("%d   ", num/factor);
    num = num % factor;
}

Here the same principle happens, the first loop divides the number by the factor, so 12345 / 10000 = 1.2345 but since integer division truncates the decimal part you get 1, next the modulus operator(%) is applied, and what it does is to return the decimal part of a division, so 2345, next loop 2345 / 1000 = 2.345 = 2, decimal part 345, and so on.


Note that, since you know the number of digits in the original input, you wouldn't even need the first loop you could simple harcode the factor:

#include <stdio.h>

int main() {
    int num, factor;

    printf("Enter a 5 digit number: ");
    scanf("%d", &num);

    factor= 100000; //<--

    while (factor > 1) {
        factor = factor / 10;
        printf("%d   ", num / factor);
        num = num % factor;
    }

    return 0;
}
#include 

int main() {
    int num, temp, factor = 1;

    printf("Enter a 5 digit number: ");
    scanf("%d", &num);                             60403

    temp = num;                                    60403
    //while (temp) {
        temp = temp / 10;                           6040
        factor = factor * 10;                         10

        temp = temp / 10;                            604
        factor = factor * 10;                        100

        temp = temp / 10;                             60
        factor = factor * 10;                       1000

        temp = temp / 10;                              6
        factor = factor * 10;                      10000

        temp = temp / 10;                              0
        factor = factor * 10;                     100000
    //}

    //while (factor > 1) {
        factor = factor / 10;                      10000
        printf("%d   ", num / factor);                      60403/10000 is 6
        num = num % factor;                          403

        factor = factor / 10;                       1000
        printf("%d   ", num / factor);                        403/1000 is 0
        num = num % factor;                          403

        factor = factor / 10;                        100
        printf("%d   ", num / factor);                        403/100 is 4
        num = num % factor;                            3

        factor = factor / 10;                         10
        printf("%d   ", num / factor);                          3/10 is 0
        num = num % factor;                            3

        factor = factor / 10;                          1
        printf("%d   ", num / factor);                          3/1 is 3
        num = num % factor;                            0
    //}
    return 0;
}
Related