Recursion Questions In C
What is C is Recursion?
“The recursion is a method of repeatedly invoking a function until a specific condition is satisfied. Recursive functions make calls to themselves, either directly or indirectly. These types of calls to functions are known as recursive calls.”
Recursion is utilized in C to resolve a challenging issue. By dividing a complicated problem into smaller pieces of code, we can solve it with recursion. Recursion also allows us to tackle the various issues, such as finding the number's factorial, creating Fibonacci sequences, creating subsets, etc.
Recursive Procedures
Recursive functions are those that call an additional instance of themselves. There is a call to itself within the function's body of the recursive functions. Furthermore, these functions are capable of having several recursive calls.
Fundamental Design of Recursive Functions
The recursive functions have the following fundamental syntax:
function_name (args) of type {
// statements for functions
// foundational state
// situation of recursion (recursive call)
}
C recursion-related problems and their answers:
1. Calculating Factorials:
Example Code:
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int number;
printf("Enter the number: ");
scanf("%d", &number);
printf("Factorial of %d = %d\n", number, factorial(number));
return 0;
}
Output:
2. The Series Fibonacci
Example code:
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1)
return n;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int number;
printf("Enter the number of values in Fibonacci series: ");
scanf("%d", &number);
printf("Fibonacci series:\n");
for (int i = 0; i < number; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");
return 0;
}
Output:
3. Total Numbers:
Example code:
#include <stdio.h>
int sum of digits(int n) {
if (n == 0)
return 0;
else
return (n % 10) + sum of digits(n / 10);
}
int main() {
int num;
printf("Enter any positive integer: ");
scanf("%d", &num);
printf("Sum of the digits: %d\n", sum of digits(num));
return 0;
}
Output:
4. Function of Power:
Example code:
#include <stdio.h>
int power(int base, int exponent) {
if (exponent == 0)
return 1;
else
return base * power(base, exponent - 1);
}
int main() {
int base, exponent;
printf("Enter the base value: ");
scanf("%d", &base);
printf("Enter the exponent value: ");
scanf("%d", &exponent);
printf("%d^%d = %d\n", base, exponent, power(base, exponent));
return 0;
}
Output:
5. Indirect Recursion:
Example code:
#include <stdio.h>
void function_A(int n)
{
if (n < 1) {
return;
}
printf("%d ", n);
n = n - 1;
function_B(n);
}
void function_B(int n)
{
if (n < 2) {
return;
}
printf("%d ", n);
n = n / 2;
function_A(n);
}
int main()
{
function_B(30);
return 0;
}
Output:
6. Tree Recursion for Fibonacci Number:
Example code:
#include <stdio.h>
int Fibonacci(int n)
{
if (n <= 1) {
return n;
}
else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
int main()
{
int n = fibonacci(7);
printf("%d", n);
return 0;
}
Output:
Principles of C Recursion
Two objects comprise the fundamentals of recursion and are necessary components of any recursion method. These are the following:
- Case Recursion
- Base Situation
1. Case of Recursion:
The recursive call in the recursive function is called the "recursion case." It selects the kind of recursion that will take place and how the issue will be broken down into more manageable sub-issues.
2. Base Condition:
The base condition establishes the endpoint of the recursion. The state determines the recursion's endpoint. When selecting the base circumstance, we must exercise caution because an inaccurate or missing base circumstance could cause the recursion to continue to run indefinitely.
Recursion Applications
Recursion is widely used in artificial intelligence (AI) to handle various issues, from basic ones like creating linked lists. Recursion is often used for the following purposes:
- Tree-Graph Methods
- Equational Issues
- Split and Take Over
- Modular Programming
- Conversion of Postfix to Infix
- Algorithms for Searching and Sorting
- Benefits of Repeating
Recursive methods have the following benefits over other approaches
- Recursion is a valuable tool for shortening code.
- Recursion is a valuable tool for solving specific issues, such as traversing trees and the tower of Hanoi.
- Recursive algorithms are more straightforward to develop for recursive data structures, such as linked lists, trees, and so forth.