Question: Consider this recursive function: public static void print(int n) { if (n >= 10) print( n / 10); printDigit( n % 10); } The above

Consider this recursive function:

public static void print(int n)

{

if (n >= 10)

print( n / 10);

printDigit( n % 10);

}

The above function flows as such....

print(6371);

print(637)

print(63)

print(6) // I understand till this point**

printDigit(6%10); // From here on out,dont understand how the program even gets to the call printDigit();

printDigit(63%10);

printDigit(637%10);

printDigit(6371%10);

so to the screen, it prints: 6371, 637, 63, 6 ,6, 3, 7, 1

My question is.... how exactly does it print that flow. I understand that print(n/10) reduces the problem every time till it reaches the base case but once the base case is reached (n not greater than or equal to 10) how is it that the call to printDigit(n%10) continues to loop. I know that the implementation of printDIgit() is not written but it is irrelavent in this situation.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!