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

Recursive Function Exercises C Solutions

The document provides various recursive function exercises in C, highlighting the importance of base cases to prevent infinite recursion. It includes examples for generating patterns, calculating powers, printing numbers, reversing input, checking for prime numbers, and finding depth until a space is encountered. Additionally, it discusses the trade-offs between recursive and non-recursive methods in terms of clarity and efficiency.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Recursive Function Exercises C Solutions

The document provides various recursive function exercises in C, highlighting the importance of base cases to prevent infinite recursion. It includes examples for generating patterns, calculating powers, printing numbers, reversing input, checking for prime numbers, and finding depth until a space is encountered. Additionally, it discusses the trade-offs between recursive and non-recursive methods in terms of clarity and efficiency.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Recursive Function Exercises - C Language Solutions

1. What is the importance of the stopping case in recursive functions?

The stopping case (base case) is essential to prevent infinite recursion. It tells the
function when to stop calling itself. Without a base case, the program would run
indefinitely and cause a stack overflow.

2. Recursive function to output 2^n - 1 integers.

#include <stdio.h>

void pattern(int n) {
if (n == 1) {
printf("1 ");
return;
}
pattern(n - 1);
printf("%d ", n);
pattern(n - 1);
}

int main() {
int n = 3;
pattern(n); // Output: 1 2 1 3 1 2 1
return 0;
}

3. Recursive function for f(n) where f(n) = 1 if n == 1, else 2 * f(n - 1).

#include <stdio.h>

int f(int n) {
if (n == 1)
return 1;
else
return 2 * f(n - 1);
}

int main() {
printf("%d\n", f(4)); // Output: 8
return 0;
}

4. Which method is preferable in general?

Recursive method: Easier for problems like factorial, Fibonacci, tree traversal.
Non-recursive method: More efficient in memory and performance.
Preferable: Non-recursive, unless recursion is clearer.
Recursive Function Exercises - C Language Solutions

5. Recursive function to print numbers from n to 0.

#include <stdio.h>

void printNumbers(int n) {
if (n < 0)
return;
printf("%d\n", n);
printNumbers(n - 1);
}

int main() {
printNumbers(5);
return 0;
}

6. Recursive function to reverse input and check for space (no arrays/strings).

#include <stdio.h>

int reverseAndCheckSpace() {
char ch = getchar();
if (ch == '\n')
return 0;
int hasSpace = reverseAndCheckSpace();
putchar(ch);
if (ch == ' ')
hasSpace = 1;
return hasSpace;
}

int main() {
printf("Enter text (press Enter to end):\n");
int hasSpace = reverseAndCheckSpace();
printf("\nContains space: %s\n", hasSpace ? "Yes" : "No");
return 0;
}

7. Recursive function to check if a number is prime.

#include <stdio.h>

int isPrime(int n, int i) {


if (n <= 2)
return n == 2;
if (n % i == 0)
return 0;
if (i * i > n)
return 1;
Recursive Function Exercises - C Language Solutions

return isPrime(n, i + 1);


}

int main() {
int num = 7;
if (isPrime(num, 2))
printf("%d is prime\n", num);
else
printf("%d is not prime\n", num);
return 0;
}

8. Recursive function to return depth until a space is found.

#include <stdio.h>

int getDepthUntilSpace(int depth) {


char ch = getchar();
if (ch == ' ')
return depth;
return getDepthUntilSpace(depth + 1);
}

int main() {
printf("Enter characters (space to stop):\n");
int depth = getDepthUntilSpace(0);
printf("Space found at depth: %d\n", depth);
return 0;
}

You might also like