Input: N = 123
The reverse of the given number is 321.
Input: N = 12532
The reverse of the given number is 23521.
Approach: Follow the steps below to solve the problem:
- Recursively iterate every digit of N.
- If the current value of N passed is less than 10, return N.
if(num < 10)
- Otherwise, after each recursive call (except the base case), return the recursive function for next iteration:
return reverse(N/10) + ((N%10)*(pow(10, (floor(log10(abs(N)))))))
where, floor(log10(abs(x))) gives the count of digits of x
((x%10)*(pow(10, (floor(log10(abs(x))))))) places the extracted unit place digits (x%10) to their desired positions
Below is the implementation of the above approach:
Time Complexity: O(log10N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Reverse a Doubly linked list using recursion
- Reverse a stack using recursion
- How to solve problems related to Number-Digits using Recursion?
- Write a program to reverse digits of a number
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum digits to be removed to make either all digits or alternating digits same
- Program to find all Factors of a Number using recursion
- Find N numbers such that a number and its reverse are divisible by sum of its digits
- Find the Largest number with given number of digits and sum of digits
- Number of digits in the nth number made of given four digits
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Program to check if an array is palindrome or not using Recursion
- C++ Program to print an Array using Recursion
- C program to create copy of a singly Linked List using Recursion
- Program for length of a string using recursion
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Reverse digits of an integer with overflow handled
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.