Recursion - digits in reverse order

前端 未结 15 1072
一向
一向 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:49

    This should work

     int rev = 0;
     int reverse(int num)
    {
        if (num < 10) {
            rev = rev*10 + num;
        }
        else {
            rev = rev*10 + (num % 10);
           num = reverse(num / 10);
    
        }
        return rev;
    }
    

提交回复
热议问题