Find the factorial of a number using recursion.
Base cases, recursive calls, and return values.
A recursive function calls itself to solve a smaller version of the same problem. For factorial:
n == 0 or n == 1, return 1n * factorial(n - 1)This continues until the base case is reached.
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial = %d\n", factorial(n));
return 0;
}
Input: 5
Output: 120
Print the Fibonacci sequence up to n terms using recursion.
Recursive calls, base cases, and sequence generation.
The Fibonacci sequence is defined as:
A recursive function computes the next number using the previous two values. This continues until the base cases are reached.
#include <stdio.h>
int fib(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
}
return fib(n - 1) + fib(n - 2);
}
int main() {
int n, i;
printf("Enter number of terms: ");
scanf("%d", &n);
printf("Fibonacci sequence: ");
for (i = 0; i < n; i++) {
printf("%d ", fib(i));
}
printf("\n");
return 0;
}
Input: 6
Output: 0 1 1 2 3 5
Convert a decimal number to binary using recursion.
Division, remainders, recursion, and number systems.
To convert decimal to binary, we repeatedly divide the number by 2 and store the remainder. The recursion prints the remainder after the recursive call, so the output is in the correct order.
#include <stdio.h>
void decimalToBinary(int n) {
if (n == 0) {
return;
}
decimalToBinary(n / 2);
printf("%d", n % 2);
}
int main() {
int n;
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("Binary: ");
decimalToBinary(n);
printf("\n");
return 0;
}
Input: 10
Output: 1010
Convert a binary number to decimal using recursion.
Recursion, positional values, and number systems.
Each binary digit has a positional value that doubles as we move left. The recursive function multiplies the current result by 2 and adds the next digit: result = result * 2 + digit This continues until all digits are processed.
#include <stdio.h>
int binaryToDecimal(char str[], int index) {
if (str[index] == '\0') {
return 0;
}
return binaryToDecimal(str, index + 1) * 2 + (str[index] - '0');
}
int main() {
char binary[100];
printf("Enter a binary number: ");
scanf("%s", binary);
printf("Decimal: %d\n", binaryToDecimal(binary, 0));
return 0;
}
Input: 1010
Output: 10
Convert a hexadecimal number to decimal using recursion.
Recursion, hexadecimal digits, positional values, and character conversion.
Hexadecimal digits have values from 0 to 15. For letters A to F, we convert them to their numeric values:
#include <stdio.h>
#include <string.h>
int hexValue(char ch) {
if (ch >= '0' && ch <= '9') {
return ch - '0';
}
if (ch >= 'A' && ch <= 'F') {
return ch - 'A' + 10;
}
if (ch >= 'a' && ch <= 'f') {
return ch - 'a' + 10;
}
return 0;
}
int hexToDecimal(char str[], int index, int length) {
if (index == length) {
return 0;
}
return hexToDecimal(str, index + 1, length) * 16 + hexValue(str[index]);
}
int main() {
char hex[100];
printf("Enter a hexadecimal number: ");
scanf("%s", hex);
printf("Decimal: %d\n", hexToDecimal(hex, 0, strlen(hex)));
return 0;
}
Input: 1A
Output: 26