Recursion - digits in reverse order

前端 未结 15 1065
一向
一向 2021-01-15 10:32

I need to implement a recursive method printDigits that takes an integer num as a parameter and prints its digits in reverse order, one digit per line.

This is what

15条回答
  •  粉色の甜心
    2021-01-15 10:44

    //Try out this, recursion with singe variable using Math class.
    public static void main(String[] args) {
        // Let the number be 139
        int n=139;
        System.out.println("reverse is "+rev(n));
    }
    static int rev(int n){
        if (n==0)return 0;
        else {
            return n%10*(int) Math.pow(10,(double) (int)Math.log10(n))+rev(n/10);
        }    
    }
    

提交回复
热议问题