You are on page 1of 3

Write a program in C to find the sum of digits of a number using recursion.

#include <stdio.h>
int DigitSum(int num);
int main() {
int n1, sum;
printf("\n\n Recursion : Find the sum of digits of a number :\n");
printf("-----------------------------------------------------\n");
printf(" Input any number to find sum of digits: ");
scanf("%d", &n1); sum = DigitSum(n1);
printf(" The Sum of digits of %d = %d\n\n", n1, sum);
return 0;
}
int DigitSum(int n1)
{
if(n1 == 0)
return 0;
return ((n1 % 10) + DigitSum(n1 / 10));
}
WAP to find GCD of two numbers using recursion.
#include<stdio.h>
int findGCD(int num1,int num2);
int main() {
int num1,num2,gcd;
printf("\n\n Recursion : Find GCD of two numbers :\n");
printf("------------------------------------------\n");
printf(" Input 1st number: ");
scanf("%d",&num1);
printf(" Input 2nd number: ");
scanf("%d",&num2);
gcd = findGCD(num1,num2);
printf("\n The GCD of %d and %d is: %d\n\n",num1,num2,gcd);
return 0;
}
int findGCD(int a,int b)
{ while(a!=b) {
if(a>b)
return findGCD(a-b,b);
else return findGCD(a,b-a);
}
return a;
}
WAP to calculate the power of any number using recursion.
#include <stdio.h>
long int CalcuOfPower(int x,int y) {
long int result=1;
if(y == 0)
return result;
result=x*(CalcuOfPower(x,y-1));
}
int main() {
int bNum,pwr;
long int result;
printf("\n\n Recursion : Calculate the power of any number :\n");
printf("----------------------------------------------------\n");
printf(" Input the base value : ");
scanf("%d",&bNum);
printf(" Input the value of power : ");
scanf("%d",&pwr);
result=CalcuOfPower(bNum,pwr);
printf(" The value of %d to the power of %d is : %ld\n\n",bNum,pwr,result);
return 0;
}

You might also like