0

The person must create a while loop to create an algorithm extractDigits that prints the individual digits of an integer. Ex) extractDigits(123) outputs "3", "2", "1".

I am not sure why exactly this method is correct and able to return each digit.

while(num > 0)
       {
            System.out.println(num % 10);
            num = (num/10);
        }
Cœur
  • 37,241
  • 25
  • 195
  • 267
ICEFIRE_16
  • 15
  • 6

1 Answers1

0

The modulus operator % returns the remainder of the two numbers after division

In excel MOD(123,10) = 3 similarly in java 123 % 10 = 3

then

when you divide (123/10) = 12 and loop continues until num=0

vels4j
  • 11,208
  • 5
  • 38
  • 63
  • Thanks for the quick and helpful replies! My mistake was I forgot that when it divides by ten, it gets rid of the remainder and assigns a new value without it. Than it just repeats. – ICEFIRE_16 Nov 06 '19 at 05:06