Q. Write a recursive C function to print the digits of a number in reverse order. Use this function in main to accept a number and print the digits in reverse order separated by tab.
Example 3456 6 5 4 3
(Hint: Recursiveprint(n) = print n if n is single digit number
= print n % 10 + tab + Recursiveprint( n/10)
Example 3456
6 5 4 3
Program
#include <stdio.h>
#include <math.h>
int rev(int, int);
int main()
{
int num, result;
int length = 0, temp;
printf("Enter an integer number to reverse: ");
scanf("%d", &num);
temp = num;
while (temp != 0)
{
length++;
temp = temp / 10;
}
result = rev(num, length);
printf("The reverse of %d is %d.\n", num, result);
return 0;
}
int rev(int num, int len)
{
if (len == 1)
{
return num;
}
else
{
return (((num % 10) * pow(10, len - 1)) + rev(num / 10, --len));
}
}
Output:
Enter an integer number to reverse: 67334
The reverse of 67334 is 43376.
Example 3456 6 5 4 3
(Hint: Recursiveprint(n) = print n if n is single digit number
= print n % 10 + tab + Recursiveprint( n/10)
Example 3456
6 5 4 3
Program
#include <stdio.h>
#include <math.h>
int rev(int, int);
int main()
{
int num, result;
int length = 0, temp;
printf("Enter an integer number to reverse: ");
scanf("%d", &num);
temp = num;
while (temp != 0)
{
length++;
temp = temp / 10;
}
result = rev(num, length);
printf("The reverse of %d is %d.\n", num, result);
return 0;
}
int rev(int num, int len)
{
if (len == 1)
{
return num;
}
else
{
return (((num % 10) * pow(10, len - 1)) + rev(num / 10, --len));
}
}
Output:
Enter an integer number to reverse: 67334
The reverse of 67334 is 43376.
0 Comments