Q:

Java Modulus and division

Will someone please explain why this code returns whatever is entered in as x
public static void calc(int x)
 {
   if((x / 10) != 0)
   {
     calc(x / 10);
   }
   System.out.print(x % 10);
 }

A:

Your calc method is a recursive method.
If the result of performing an int division is not 0, i.e. if the number is at least 10, then divide by 10 and call itself.
Eventually, you will reach a number that is less than 10.  The if condition will be false, and x % 10 will be x itself.  But what is x at this point?  It's the first digit of the number.
Then the base case finishes, and the recursive calls all call x % 10 to print.   At each level, another digit is printed, until the original call prints the last digit.
Example:
calc(456)
    x is 456.
    x / 10 is 45, not 0.  Call calc(x / 10)
    calc(45)
        x is 45.
        x / 10 is 4, not 0.  Call calc(x / 10)
        calc(4)
            x is 4.
            x / 10 is 0.
            Print x % 10, or 4.
        Print x % 10, or 5.
    Print x % 10, or 6.

456

