Recursion in C
Recursion in C
Recursion in C
Recursion is the process by which a function calls itself. C language allows writing
of such functions which call itself to solve complicated problems by breaking them
down into simple and easy problems. These functions are known as recursive
functions.
Syntax
void recursive_function(){
recursion(); // function calls itself
}
int main(){
recursive_function();
}
https://www.tutorialspoint.com/cprogramming/c_recursion.htm 1/7
6/16/24, 12:33 PM Recursion in C
n! = n X (n-1)!
It can be seen that we use factorial itself to define factorial. Hence this is a fit case
to write a recursive function. Let us expand the above definition for calculating the
factorial value of 5.
5! = 5 X 4!
5 X 4 X 3!
5 X 4 X 3 X 2!
5 X 4 X 3 X 2 X 1!
5X4X3X 2X1
= 120
While we can perform this calculation using a loop, its recursive function involves
successively calling it by decrementing the number till it reaches 1.
The following program shows how you can use a non-recursive function to calculate
the factorial of a number −
#include <stdio.h>
#include <math.h>
// function declaration
int factorial(int);
int main(){
int a = 5;
int f = factorial(a);
https://www.tutorialspoint.com/cprogramming/c_recursion.htm 2/7
6/16/24, 12:33 PM Recursion in C
}
int factorial(int x){
int i;
int f = 1;
Output
When you run this code, it will produce the following output −
a: 5
Factorial of a: 120
Let us now write a recursive function for calculating the factorial of a given number.
The following example calculates the factorial of a given number using a recursive
function −
#include <stdio.h>
#include <math.h>
/* function declaration */
int factorial(int i){
https://www.tutorialspoint.com/cprogramming/c_recursion.htm 3/7
6/16/24, 12:33 PM Recursion in C
return 0;
}
Output
a: 5
Factorial of a: 120
When the main() function calls the factorial() function by passing the variable "a", its
value is stored in "i". The factorial() function successively calls itself.
In each call, the value of "i" is multiplied by its earlier value after reducing it by 1, till
it reaches 1. As it reaches 1, the product of all the values between the initial value of
the argument and 1 is returned to the main() function.
While we can perform a sequential search for a certain number in the list using a for
loop and comparing each number, the sequential search is not efficient, especially if
the list is too long.
The binary search algorithm checks if the index "start" is greater than the index
"end". Based on the value present at the variable "mid", the function is called again
to search for the element.
We have a list of numbers arranged in ascending order. Then we find the midpoint of
the list and restrict the checking to either left or right of the midpoint, depending on
whether the desired number is less than or greater than the number at the midpoint.
#include <stdio.h>
https://www.tutorialspoint.com/cprogramming/c_recursion.htm 4/7
6/16/24, 12:33 PM Recursion in C
if (array[mid] == element)
return mid;
int main(void){
int array[] = {5, 12, 23, 45, 49, 67, 71, 77, 82};
int n = 9;
int element = 67;
int index = bSearch(array, 0, n-1, element);
if(index == -1 ){
printf("Element not found in the array ");
}
else{
printf("Element found at index: %d", index);
}
return 0;
}
Output
Example
https://www.tutorialspoint.com/cprogramming/c_recursion.htm 5/7
6/16/24, 12:33 PM Recursion in C
The following example generates the first 10 numbers in the Fibonacci series for a
given number using a recursive function −
#include <stdio.h>
if(i == 0){
return 0;
}
if(i == 1){
return 1;
}
return fibonacci(i-1) + fibonacci(i-2);
}
int main(){
int i;
printf("%d\t\n", fibonacci(i));
}
return 0;
}
Output
When the above code is compiled and executed, it produces the following result −
0
1
1
2
3
5
8
13
https://www.tutorialspoint.com/cprogramming/c_recursion.htm 6/7
6/16/24, 12:33 PM Recursion in C
21
34
https://www.tutorialspoint.com/cprogramming/c_recursion.htm 7/7