Program to illustrate reverse of a given number using recursion
//reverse of a given number using recursion
#include<stdio.h>
int sum,r; // global declaration
int reverse(int); // function declaration
int main()
{
int n,rev;
printf("enter any value:\n");
scanf("%d",&n);
rev=reverse(n);
printf("%d reverse is = %d",n,rev);
return 0;
}
int reverse(int n)
{
if(n != 0)
{
r= n % 10;
sum=sum * 10 + r;
n = n / 10;
reverse(n);
}
else
return sum;
}
output:
//reverse of a given number using recursion
#include<stdio.h>
int sum,r; // global declaration
int reverse(int); // function declaration
int main()
{
int n,rev;
printf("enter any value:\n");
scanf("%d",&n);
rev=reverse(n);
printf("%d reverse is = %d",n,rev);
return 0;
}
int reverse(int n)
{
if(n != 0)
{
r= n % 10;
sum=sum * 10 + r;
n = n / 10;
reverse(n);
}
else
return sum;
}
output:
No comments:
Post a Comment