If you have any query feel free to chat us!
Happy Coding! Happy Learning!
Copy code
int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
Copy code
int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
Copy code
int countChar(char* str, char c) {
if (*str == '\0') {
return 0;
}
if (*str == c) {
return 1 + countChar(str + 1, c);
} else {
return countChar(str + 1, c);
}
}
Copy code
int findMax(int* arr, int size) {
if (size == 1) {
return arr[0];
}
int max = findMax(arr, size - 1);
if (arr[size - 1] > max) {
max = arr[size - 1];
}
return max;
}
Copy code
bool isPrime(int n, int i) {
if (n <= 1) {
return false;
}
if (i == 1) {
return true;
}
if (n % i == 0) {
return false;
}
return isPrime(n, i - 1);
}
bool isPrimeNumber(int n) {
return isPrime(n, n / 2);
}
Please note that these are just sample solutions to the problems, and there are many ways to solve a problem using recursion in C. Also, these problems are not meant to be difficult, the goal is to give you an idea of how recursion works in C and how it can be used to solve problems.
Comments: 0