recursive function digits of a positive decimal integer in reverse order c++

前端 未结 5 505
清歌不尽
清歌不尽 2021-01-21 18:22

I have an assignment to write a recursive function that writes the digits of a positive integer in reverse order. My problem is that the function doesn\'t display the reverse co

5条回答
  •  孤街浪徒
    2021-01-21 18:43

    This solution will omit trailing zeroes, because it is literally reversing the content of the integer:

    int reverse(int number, int n = 0)
    {
      if (number == 0)
      {
        return n;
      }
      else
      {
        int nextdigit = number%10;
        int nextprefix = n*10+nextdigit;
        return reverse(number/10 ,nextprefix);
      }
    }
    

提交回复
热议问题