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

Recursion Programming Questions

Uploaded by

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

Recursion Programming Questions

Uploaded by

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

Recursion Programming Questions

I am giving you a question on C programming, find some


related questions based on this : "State the advantages of
using recursion. Write a program to count the number of
digits of a number using recursion."

Here are some related questions based on your provided topic:

1. Conceptual Questions:

What is recursion? Explain with an example.

Compare recursion and iteration. List their advantages and disadvantages.

What are the base case and recursive case in recursion? Why are they important?

2. Application-Based Questions:

Write a program to calculate the factorial of a number using recursion.

Write a recursive program to calculate the nth Fibonacci number.

Write a program to reverse a string using recursion.

Develop a recursive program to find the greatest common divisor (GCD) of two
numbers.

Write a recursive function to calculate the power of a number (i.e., ab ).

3. Logical and Problem-Solving Questions:

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.

Write a recursive function to check if a given string is a palindrome.

4. Advanced Questions:

Explain tail recursion and write a program to compute the sum of an array using tail
recursion.

How would you optimize a recursive function using memoization? Provide an


example.

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?

yes give solution of all the questions

Here are detailed solutions for the questions related to recursion in C programming:

1. What is recursion? Explain with an example.


Answer:
Recursion is a process where a function calls itself to solve a smaller instance of the same
problem. It continues until a base case is reached, which stops the recursion.

Example:

#include <stdio.h>

// Recursive function to calculate factorial


int factorial(int n) {
if (n == 0 || n == 1) { // Base case
return 1;
}
return n * factorial(n - 1); // Recursive case
}

int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}

2. Compare recursion and iteration. List their advantages and


disadvantages.

2/9
Answer:

Aspect Recursion Iteration

Definition A function calls itself to solve a problem. Repeated execution of a block of


code (loop).

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.

Performance Slower due to overhead of multiple function Faster as no function call


calls. overhead.

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.

3. Base Case and Recursive Case


Answer:

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>

void reverse(char str[], int index, int len) {


if (index >= len / 2) return;
char temp = str[index];
str[index] = str[len - index - 1];
str[len - index - 1] = temp;
reverse(str, index + 1, len);
}

int main() {
char str[] = "hello";
int len = strlen(str);
reverse(str, 0, len);
printf("Reversed string: %s\n", str);
return 0;
}

7. GCD Using Recursion


Code:

#include <stdio.h>

int gcd(int a, int b) {


if (b == 0) return a; // Base case
return gcd(b, a % b); // Recursive case
}

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;
}

8. Power Calculation Using Recursion


Code:

#include <stdio.h>

int power(int base, int exp) {


if (exp == 0) return 1; // Base case
return base * power(base, exp - 1);
}

int main() {
int base = 2, exp = 5;
printf("%d raised to the power %d is %d\n", base, exp, power(base, exp));
return 0;
}

9. Handling Negative Numbers for Digit Counting


Code:

#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;
}

10. Palindrome Check Using Recursion


Code:

#include <stdio.h>
#include <string.h>

int isPalindrome(char str[], int start, int end) {


if (start >= end) return 1; // Base case
if (str[start] != str[end]) return 0; // Mismatch
return isPalindrome(str, start + 1, end - 1);
}

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;
}

11. Tail Recursion for Sum of Array


Code:

7/9
c

#include <stdio.h>

int sumArray(int arr[], int n, int currentSum) {


if (n == 0) return currentSum; // Base case
return sumArray(arr, n - 1, currentSum + arr[n - 1]); // Tail recursive call
}

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;
}

12. Optimizing Recursion Using Memoization


Example: Fibonacci using memoization.
Code:

#include <stdio.h>

int fib[100] = {0};

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

You might also like