A 5-digit positive integer is entered through the keyboard. Write a program with the
following recursive functions
i) rsum() to find the sum of the digits
ii) rrev() to reverse the number
#include<stdio.h>
#include<conio.h>
int rsum(int n)
{
int s=0,r;
if(n==0)
return 0;
else
{
r=n%10;
n=n/10;
s=s+r;
return s+rsum(n);
}
}
void rrev(int n)
{
if(n==0)
return;
else
{
printf("%d",n%10);
n=n/10;
return rrev(n);
}
}
int main()
{
int n,t;
printf("\nEnter number to sum digit:");
scanf("%d",&n);
t=rsum(n);
printf("\nThe sum of the digit:%d",t);
printf("\nEnter the number to print reverse:");
scanf("%d",&n);
printf("\nThe reverse of the number:");
rrev(n);
getche();
}
following recursive functions
i) rsum() to find the sum of the digits
ii) rrev() to reverse the number
#include
#include<conio.h>
int rsum(int n)
{
int s=0,r;
if(n==0)
return 0;
else
{
r=n%10;
n=n/10;
s=s+r;
return s+rsum(n);
}
}
void rrev(int n)
{
if(n==0)
return;
else
{
printf("%d",n%10);
n=n/10;
return rrev(n);
}
}
int main()
{
printf("\nEnter number to sum digit:");
scanf("%d",&n);
t=rsum(n);
printf("\nThe sum of the digit:%d",t);
printf("\nEnter the number to print reverse:");
scanf("%d",&n);
printf("\nThe reverse of the number:");
rrev(n);
getche();
}
0 comments:
Post a Comment