147k views
3 votes
Consider the method below, which prints the digits of an arbitrary positive integer in reverse order, one digit per line. The method should print the last digit first. Then, it should recursively print the integer obtained by removing the last digit. Select the statements that should be used to complete the method. public static void printReverse(int value) { if (value > 0) { _____________________ // print last digit _____________________ // recursive call to print value without last digit } }

User Jiyeh
by
7.9k points

1 Answer

3 votes

Answer:

System.out.println(value % 10);

printReverse(value / 10);

Step-by-step explanation:

System.out.println(value % 10);

To print the last digit, we print the value modulo 10 as the result of the value modulo 10 gives us the last digit.

printReverse(value / 10);

To call the recursive method again without the last digit, we now pass the result of dividing the value with 10 inside the printReverse method as parameter. The division by 10 will remove the last digit from the value and result will be the remaining digits.

User TomKPZ
by
8.7k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.