In this tutorial, we will write a program to reverse an input number using recursion in C. Before that you may go through the following topic in C programming.
C Program to Reverse a given Number using Recursive Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <stdio.h> int recursive_func(int, int); int main() { int num, result; printf("Enter the number: "); scanf("%d", &num); //calling recursive function result = recursive_func(num, 0); printf("Reverse of %d is %d", num, result); return 0; } int recursive_func(int num, int rev) { if (num == 0) return rev; else return recursive_func(num/10, rev*10 + num%10); } |
Output:
Enter the number: 789456
Reverse of 789456 is 654987