0% found this document useful (0 votes)
63 views

Function Calls Itself /: Write A Program in C To Find The Factorial of A Number Using Recursion

Recursion is a process where a function calls itself directly or indirectly. In programming languages, a function is recursive when it calls itself during its execution. This allows problems to be broken into smaller sub-problems of the same type. Recursion requires a base case that does not call the function again, otherwise it will result in infinite looping. The examples provided demonstrate using recursion to find the factorial, print natural numbers, generate Fibonacci series, calculate digit sum and find greatest common divisor of two numbers.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Function Calls Itself /: Write A Program in C To Find The Factorial of A Number Using Recursion

Recursion is a process where a function calls itself directly or indirectly. In programming languages, a function is recursive when it calls itself during its execution. This allows problems to be broken into smaller sub-problems of the same type. Recursion requires a base case that does not call the function again, otherwise it will result in infinite looping. The examples provided demonstrate using recursion to find the factorial, print natural numbers, generate Fibonacci series, calculate digit sum and find greatest common divisor of two numbers.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

What is called Recursion?

Recursion is the process of repeating items in a self-similar way. In programming languages, if a


program allows you to call a function inside the same function, then it is called a recursive call of
the function.

void recursion() {
recursion(); /* function calls itself */
}

int main() {
recursion();
}

The C programming language supports recursion, i.e., a function to call itself. But while using
recursion, programmers need to be careful to define an exit condition from the function,
otherwise it will go into an infinite loop.
Recursive functions are very useful to solve many mathematical problems, such as calculating
the factorial of a number, generating Fibonacci series, etc.
Write a program in C to find the Factorial of a number using recursion.
#include<stdio.h>
int findFactorial(int);

int main()
{
int n1,f;
printf("\n\n Recursion : Find the Factorial of a number :\n");
printf("-------------------------------------------------\n");
printf(" Input a number : ");
scanf("%d",&n1);
f=findFactorial(n1);//call the function findFactorial for factorial
printf(" The Factorial of %d is : %d\n\n",n1,f);
return 0;
}

int findFactorial(int n)
{
if(n==1)
return 1;
else
return(n*findFactorial(n-1));// calling the function findFactorial to itself recursively
}
Input a number : 5
Expected Output :

The Factorial of 5 is : 120

Write a program in C to print first 50 natural numbers using recursion.

#include<stdio.h>

int numPrint(int);

int main()

int n = 1;

printf("\n\n Recursion : print first 50 natural numbers :\n");

printf("-------------------------------------------------\n");

printf(" The natural numbers are :");

numPrint(n);

printf("\n\n");

return 0;

int numPrint(int n)

if(n<=50)

printf(" %d ",n);

numPrint(n+1);

}
Expected Output :

The natural numbers are : 1 2 3


4 5 6 7 8 9 10 11 12 13
14 15 16 17 18 19 20 21
22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38
39 40 41 42 43 44 45 46 47
48 49 50

Write a program in C to Print Fibonacci Series using recursion.

Test Data :
Input number of terms for the Series (< 20) : 10
Expected Output :

Input number of terms for the Series (< 20) : 10


The Series are :
1 1 2 3 5 8 13 21 34 55

#include<stdio.h>

int term;
int fibonacci(int prNo, int num);

void main()
{
static int prNo = 0, num = 1;
printf("\n\n Recursion : Print Fibonacci Series :\n");
printf("-----------------------------------------\n");

printf(" Input number of terms for the Series (< 20) : ");
scanf("%d", &term);
printf(" The Series are :\n");
printf(" 1 ");
fibonacci(prNo, num);
printf("\n\n");
}

int fibonacci(int prNo, int num)


{
static int i = 1;
int nxtNo;

if (i == term)
return (0);
else
{
nxtNo = prNo + num;
prNo = num;
num = nxtNo;
printf("%d ", nxtNo);

i++;
fibonacci(prNo, num); //recursion, calling the function fibonacci itself
}
return (0);
}

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

Test Data :
Input any number to find sum of digits: 25
Expected Output :

The Sum of digits of 25 = 7

#include <stdio.h>

#include<conio.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);//call the function for calculation

printf(" The Sum of digits of %d = %d\n\n", n1, sum);

getch();
}

int DigitSum(int n1)


{
if(n1 == 0)
return 0;

return ((n1 % 10) + DigitSum(n1 / 10));//calling the function DigitSum itself


}

Write a program in C to find GCD of two numbers using recursion. 

Test Data :
Input 1st number: 10
Input 2nd number: 50
Expected Output :

The GCD of 10 and 50 is: 10

#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;

You might also like