Basic Recursion in C Programming | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

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.

Number Factorial

The following example calculates the factorial of a given number using a recursive function −

#include <stdio.h>

int factorial(unsigned int i) {

if(i <= 1) {
return 1;
}
return i * factorial(i - 1);
}

int main() {
int i = 15;
printf("Factorial of %d is %d
", i, factorial(i));
return 0;
}

When the above code is compiled and executed, it produces the following result −

Factorial of 15 is 2004310016

Fibonacci Series

The following example generates the Fibonacci series for a given number using a recursive function −

#include <stdio.h>

int fibonacci(int i) {

if(i == 0) {
return 0;
}

if(i == 1) {
return 1;
}
return fibonacci(i-1) + fibonacci(i-2);
}

int main() {

int i;

for (i = 0; i < 10; i++) {
printf("%d
", fibonacci(i));
}

return 0;
}

When the above code is compiled and executed, it produces the following result −

0	1	1	2	3	5	8	13	21	34
The document Basic Recursion in C Programming | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Basic Recursion in C Programming - Programming and Data Structures - Computer Science Engineering (CSE)

1. What is recursion in C programming?
Ans. Recursion is a programming technique in which a function calls itself to solve a problem. It involves breaking down a problem into smaller subproblems and solving them recursively until the base case is reached.
2. How does recursion work in C programming?
Ans. When a function is called recursively, a new instance of the function is created with its own set of local variables. This new instance executes the function code, and if necessary, calls itself again. This process continues until the base case is reached, at which point the function calls start returning and the problem is solved.
3. What is the base case in recursion?
Ans. The base case in recursion is the condition that determines when the recursive calls should stop. It is the simplest form of the problem that can be solved directly without any further recursion. The base case acts as a termination condition for the recursive calls, preventing infinite recursion.
4. What are the advantages of using recursion in C programming?
Ans. Recursion can lead to more concise and elegant code by breaking down complex problems into smaller, more manageable parts. It can also simplify the logic of certain algorithms, making them easier to understand and implement. Additionally, recursion can be used to solve problems that have a natural recursive structure, such as tree traversal or factorial calculation.
5. What are the limitations of recursion in C programming?
Ans. Recursion can consume a significant amount of memory due to the creation of multiple function instances on the call stack. This can lead to stack overflow errors if the recursion depth is too large. Recursion can also be less efficient in terms of time complexity compared to iterative solutions for certain problems.
119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Previous Year Questions with Solutions

,

Basic Recursion in C Programming | Programming and Data Structures - Computer Science Engineering (CSE)

,

Basic Recursion in C Programming | Programming and Data Structures - Computer Science Engineering (CSE)

,

MCQs

,

Viva Questions

,

Summary

,

practice quizzes

,

video lectures

,

mock tests for examination

,

Sample Paper

,

Objective type Questions

,

pdf

,

Extra Questions

,

past year papers

,

Exam

,

study material

,

Semester Notes

,

shortcuts and tricks

,

ppt

,

Free

,

Basic Recursion in C Programming | Programming and Data Structures - Computer Science Engineering (CSE)

,

Important questions

;