What is Recursion?
The process in which a function calls itself directly or indirectly is called recursion and
the corresponding function is called as recursive function. Using recursive algorithm,
certain problems can be solved quite easily. Recursion involves several numbers of recursive
calls. However, it is important to impose a termination condition of recursion.
1.sum of digit
Step1: Read a Number n
Step2: call sum(n)
Step 3: perform sum of digit operation in recursive function
Step 4: print the sum of the digit
#include <stdio.h>
int sum (int a);
int main()
int num, result;
printf("Enter the number: ");
scanf("%d", &num);
result = sum(num);
printf("Sum of digits in %d is %d\n", num, result);
return 0;
// Recursive function for sum of digit
int sum (int num)
if (num != 0)
return (num % 10 + sum (num / 10)); //Recursive call
else
return 0;
Sample output:
Input :
Enter the number: 123
Sum of digits in 123 is 6
ii) Factorial
Step1: Read a Number n
Step2: call fact(n)
Step 3: perform factorial operation in recursive function
Step 4: print the factorial of the number
#include <stdio.h>
int fact (int);
int main()
int n,f;
printf("Enter the number:");
scanf("%d",&n);
f = fact(n);
printf("factorial of the entered number is = %d",f);
// Recursive Function for Factorial of a number
int fact(int n)
if (n==0)
return 0;
else if ( n == 1)
return 1;
else
{
return n*fact(n-1);
Sample output:
Enter the number:5
factorial of the entered number is = 120