Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
51 views

Recursive Function

A recursive function in C programming is a function that calls itself during its execution, sometimes multiple times, to solve a problem. It breaks the problem down into smaller sub-problems until it reaches a base case, at which point the solutions to the sub-problems are used to build the overall solution. The document provides examples of using recursive functions to calculate the factorial of a number, generate the Fibonacci sequence, and find the greatest common divisor of two numbers. Each example illustrates how the problem is broken down and solved through repeated self-calls until a base case is reached.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Recursive Function

A recursive function in C programming is a function that calls itself during its execution, sometimes multiple times, to solve a problem. It breaks the problem down into smaller sub-problems until it reaches a base case, at which point the solutions to the sub-problems are used to build the overall solution. The document provides examples of using recursive functions to calculate the factorial of a number, generate the Fibonacci sequence, and find the greatest common divisor of two numbers. Each example illustrates how the problem is broken down and solved through repeated self-calls until a base case is reached.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

10/12/23, 11:16 AM What is Recursive Function in C Programming?

Recursive Function
The Recursive function is a function, that repeatedly calls itself in order to solve a
problem, breaking the problem down into smaller and smaller sub-problems until it
reaches a base case, at which point the solution is built up from the solutions to the
sub-problems. Let’s see how the recursive function looks in C language.

Understand Recursive Function

In the below example, the function rec() calls itself so the function rec() is called the
recursive function.

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

int main()
{
rec();
}

Examples of the Recursive Functions in C Programming:


We will see a few examples to understand the recursive function in C programming:

Example 1: Factorial of the number using the recursive function.

The Factorial of the number N is the multiplication of natural numbers q to N.

Factorial( N ) = 1 * 2 * 3 * ….. * N-1 * N

Let’s see how to find the factorial of the given number using the recursive function.

// recursive function to find factorial


#include <stdio.h>
int factorial(int n) {
https://www.prepbytes.com/blog/c-programming/what-is-recursive-function-in-c-programming/ 1/4
10/12/23, 11:16 AM What is Recursive Function in C Programming?
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
int n;
printf("Enter the number: ");
scanf("%d",&n);
int fact = factorial(n);
printf("\nThe factorial of %d is: %d\n",
n, fact);
return 0;
}

Output:

Enter the number: 5


The factorial of 5 is: 120

In the C program, we have created the recursive function factorial(), in which there is
one base to terminate the recursive class and the base case is n==0 because factorial
of 0 and 1 is 1. If it is not the base case then the function calls itself for the n-1 th term
and multiplies it with n and returns the answer.

Example 2: Fibonacci Series using the recursive function

Fibonacci series is the series, where the Nth term is the sum of the last term ( N-1 th)
and the second last term (N-2 th).

fibonacci (N) = fibonacci (N-1) + fibonacci (N-2)

Let’s see how to find the fibonacci series using the recursive function.

// recursive function to find fibonacci


#include <stdio.h>
int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n -
2);
}
int main() {

https://www.prepbytes.com/blog/c-programming/what-is-recursive-function-in-c-programming/ 2/4
10/12/23, 11:16 AM What is Recursive Function in C Programming?
int n;
printf("Enter the number: ");
scanf("%d",&n);
int fibo = fibonacci(n);
printf("The %dth Fibonacci number is:
%d\n", n,fibo);
return 0;
}

Output:

Enter the number: 8


The 8th Fibonacci number is: 21

In the C program, we have created the recursive function fibonacci(), in which there is
one base to terminate the recursive class and the base case is n<=1 because
Fibonacci of 0 and 1 is 1. If it is not the base case then the function calls itself for the n-
1 th term and adds it with the n-2 th term and returns the answer.

Example 3: GCD of the given two numbers using the recursive function

The greatest common divisor (GCD) of two numbers is the largest positive integer that
divides both of the numbers without leaving a remainder. Let’s see how to find the GCD
of the two numbers using the recursive function.

// recursive function to find GCD


#include <stdio.h>
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int main() {
int result = gcd(24, 32);
printf("The GCD of 24 and 32 is: %d\n",
result);
return 0;
}

Output:

https://www.prepbytes.com/blog/c-programming/what-is-recursive-function-in-c-programming/ 3/4
10/12/23, 11:16 AM What is Recursive Function in C Programming?

The GCD of 24 and 32 is: 8

In the C program, we have created the recursive function gcd(), in which there is one
base to terminate the recursive class and the base case is b==0 we will return a. If it is
not the base case then we will return gcd(b, a%b).

https://www.prepbytes.com/blog/c-programming/what-is-recursive-function-in-c-programming/ 4/4

You might also like