Printing Digits Of Numbers in Java Assignment

Let’s write the following method

public void printDigits( int n){

}

 

Walk through of how the code should work:

the algorithm inolves

1) extract last digit

2) print last digit

3) remove last digit

keep doing this until there are no more digits

print-numbrs-screen-shot

Example :

Let’s look at printDigits( 345)

Walk through of how the code should work:

the algorithm inolves

1) extract last digit ‘5’

2) print last digit print ‘5’

3) remove last digit now n should be ’34’

do this for ‘4’ and then ‘3’ until there are no more digits

You need a while loop. Think about it. What is the value of n when there are no more digits?