2

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;
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • I am a beginner in stackoverflow if this question is not accepted it is fine – Dave Balanghig Sep 23 '22 at 12:25
  • 9
    I suggest that you work this out with pencil and paper - become the interpreter for the code. You'll learn much better than reading explanations provided here. Make it easy for yourself, start with a 2 or 3 digit number. – High Performance Mark Sep 23 '22 at 12:28
  • 1
    Do you understand what the arithmetic operations `/`, `*`, and `%` do? – mkrieger1 Sep 23 '22 at 12:28
  • 1
    Can you narrow down what aspects of this confuse you by explaining what you *do* understand about it? – Scott Hunter Sep 23 '22 at 12:28
  • 4
    This might be a good time to learn how to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) programs. For example, using a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) you can step through the code statement by statement, while monitoring variables and their values. – Some programmer dude Sep 23 '22 at 12:28
  • 4
    You should learn how to use the debugger, which will let you step through this line by line, which is the best way to understand how code works, IMO. – Alexander Sep 23 '22 at 12:29
  • Hi @HighPerformanceMark. Thanks for the comment. Do you mean like make a flow chart or pseudo code? – Dave Balanghig Sep 23 '22 at 12:29
  • 1
    You should, at some stage, learn how to use a debugger as others have commented. Right now I wouldn't bother - it would just present you with the additional problem of learning how to use the debugger. Pencil and paper. – High Performance Mark Sep 23 '22 at 12:30
  • @mkrieger1 yes I understand them – Dave Balanghig Sep 23 '22 at 12:31
  • 2
    Using pen and paper is also very good. Not a flowchart or anything like that, just do the calculation on paper. Start with some valid value for `num`, then do all the calculations one by one in each loop, and see their results. – Some programmer dude Sep 23 '22 at 12:31
  • 4
    No, I mean take a piece of paper and divide it into columns, one for each of the variables in the program. At the left write the first line of code, on the same line write the values of each variable at the end of the execution of that line. Next line, next line of code, update each variable, ..., when it comes to a loop, loop. Become the interpreter. – High Performance Mark Sep 23 '22 at 12:32
  • 1
    It might be helpful to print out the code on paper and mark which line is currently executed and write next to it the current values of the variables. – mkrieger1 Sep 23 '22 at 12:32
  • thank you I will try to learn debugging and do the paper and pen next – Dave Balanghig Sep 23 '22 at 12:33
  • 1
    To add to my 2nd comment above - when you start to learn how to use a debugger it is an immense help to have a good understanding of how a program works so you can concentrate on the debugger itself. If you start trying to debug a code which you have admitted you don't understand whenever you encounter a 'surprise' you won't know if it is lack of understanding of the code or of the debugger. – High Performance Mark Sep 23 '22 at 12:44
  • 1
    A few `printf`s here and there at strategic spots will also help you to understand what's going on. This is called "printf debugging" and can be an alternative to using a real debugger during the very early stages of your learning process. – Jabberwocky Sep 23 '22 at 12:44

2 Answers2

1

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;
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53
1
#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;
}
pmg
  • 106,608
  • 13
  • 126
  • 198