Recursion Programming Questions
Recursion Programming Questions
1. Conceptual Questions:
What are the base case and recursive case in recursion? Why are they important?
2. Application-Based Questions:
Develop a recursive program to find the greatest common divisor (GCD) of two
numbers.
How does recursion handle stack memory, and what is the risk of stack overflow in
recursive functions?
Modify the program to count the number of digits to handle negative numbers as
input.
4. Advanced Questions:
Explain tail recursion and write a program to compute the sum of an array using tail
recursion.
Discuss how recursion can be converted into iteration and write a program to
demonstrate this.
1/9
Would you like me to provide detailed solutions to any of these questions?
Here are detailed solutions for the questions related to recursion in C programming:
Example:
#include <stdio.h>
int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
2/9
Answer:
Code Simpler and more intuitive for problems like Often more complex but efficient
simplicity tree traversal or factorial. in terms of memory.
Memory usage Uses stack memory; risk of stack overflow. Uses heap memory; no risk of
stack overflow.
Advantages Easier to solve complex problems with a Efficient for most problems
smaller code base. without deep nested calls.
Disadvantages Can be inefficient for deep recursive calls. Can become verbose for some
complex problems.
Base Case: The condition that stops the recursion. Without it, the function would call
itself indefinitely, causing a stack overflow.
Recursive Case: The part of the function where it calls itself to solve smaller
subproblems.
Example:
#include <stdio.h>
int sumUpTo(int n) {
if (n == 0) // Base case
return 0;
return n + sumUpTo(n - 1); // Recursive case
}
3/9
4. Factorial Calculation Using Recursion
Code:
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
}
int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
5. Fibonacci Sequence
Code:
#include <stdio.h>
int fibonacci(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n = 7;
printf("Fibonacci number at position %d is %d\n", n, fibonacci(n));
return 0;
}
4/9
6. Reverse a String Using Recursion
Code:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "hello";
int len = strlen(str);
reverse(str, 0, len);
printf("Reversed string: %s\n", str);
return 0;
}
#include <stdio.h>
5/9
int main() {
int a = 56, b = 98;
printf("GCD of %d and %d is %d\n", a, b, gcd(a, b));
return 0;
}
#include <stdio.h>
int main() {
int base = 2, exp = 5;
printf("%d raised to the power %d is %d\n", base, exp, power(base, exp));
return 0;
}
#include <stdio.h>
int countDigits(int n) {
n = n < 0 ? -n : n; // Handle negative numbers
if (n < 10) return 1; // Base case
6/9
return 1 + countDigits(n / 10); // Recursive case
}
int main() {
int num = -12345;
printf("Number of digits in %d is %d\n", num, countDigits(num));
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "radar";
if (isPalindrome(str, 0, strlen(str) - 1))
printf("%s is a palindrome\n", str);
else
printf("%s is not a palindrome\n", str);
return 0;
}
7/9
c
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Sum of array elements: %d\n", sumArray(arr, size, 0));
return 0;
}
#include <stdio.h>
int fibonacciMemo(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
if (fib[n] != 0) return fib[n]; // Use cached result
fib[n] = fibonacciMemo(n - 1) + fibonacciMemo(n - 2);
return fib[n];
}
int main() {
int n = 7;
printf("Fibonacci number at position %d is %d\n", n, fibonacciMemo(n));
8/9
return 0;
}
Would you like any explanation or further elaboration on any of the solutions?
9/9