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

recursion

The document explains recursion in C programming, detailing its structure, key components (base case and recursive case), and real-world applications such as calculating factorials and navigating tree structures. It provides examples of recursive functions including factorial, Fibonacci series, power function, and GCD. Additionally, it offers tips for avoiding errors and guidance on when to use recursion effectively.

Uploaded by

jaynunezdeped
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

recursion

The document explains recursion in C programming, detailing its structure, key components (base case and recursive case), and real-world applications such as calculating factorials and navigating tree structures. It provides examples of recursive functions including factorial, Fibonacci series, power function, and GCD. Additionally, it offers tips for avoiding errors and guidance on when to use recursion effectively.

Uploaded by

jaynunezdeped
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

C Programming: Recursion and Math

Operations
🌀 1. RECURSION
🔹 What is Recursion?
Recursion is a programming technique where a function calls itself to solve smaller
instances of the same problem.
🔹 Structure of a Recursive Function
return_type function_name(parameters) {
if (base_condition) {
// stop recursion
return result;
} else {
// recursive call
return function_name(modified_parameters);
}
}
🔹 Two Key Parts:
 Base Case: Stops recursion (prevents infinite calls)
 Recursive Case: Calls itself with modified input

🔸 Real-World Applications of Recursion


 Solving factorials, Fibonacci series
 Calculating GCD (Greatest Common Divisor)
 Navigating tree structures (e.g., file systems)
 Backtracking algorithms (e.g., Sudoku solver, maze solver)
 Implementing algorithms like Merge Sort, Quick Sort

✅ Recursive Examples
➤ Factorial
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}

➤ Fibonacci Series
int fibonacci(int n) {
if (n == 0 || n == 1)
return n;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}

PROGRAM
#include <stdio.h>

int factorial(int n) {
//base case
if(n == 0) {
return 1;
} else {
return n * factorial(n-1);
}
}

int fibbonacci(int n) {
if(n == 0){
return 0;
} else if(n == 1) {
return 1;
} else {
return (fibbonacci(n-1) + fibbonacci(n-2));
}
}

int main() {
int n = 5;
int i;

printf("Factorial of %d: %d\n" , n , factorial(n));


printf("Fibbonacci of %d: " , n);

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

➤ Power Function (x^y)


int power(int x, int y) {
if (y == 0)
return 1;
else
return x * power(x, y - 1);
}
➤ GCD using Recursion
int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}

🛠 Usage in Functions
➤ Sum of N Numbers (Recursive)
int sum(int n) {
if (n == 0)
return 0;
else
return n + sum(n - 1);
}
➤ Multiplication Using Recursion
int multiply(int x, int y) {
if (y == 0)
return 0;
else
return x + multiply(x, y - 1);
}

⚠️Tips to Avoid Recursive Errors


 Always include a base case
 Be careful with stack overflow (too many recursive calls)
 Use iteration when recursion is too deep or inefficient

📌 When to Use Recursion


Use recursion when:
 Problem has repetitive structure
 Each smaller version of the problem is similar
 You are dealing with divide-and-conquer algorithms

You might also like