Practical / Solution
Recursion — Exercise 1
Problem
Write a C program using recursion to calculate the factorial of a number.
Program
#include <stdio.h>
long long factorial(int n)
{
// Base case
if (n <= 1)
return 1;
// Recursive call
return n * factorial(n - 1);
}
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial = %lld\n", factorial(n));
return 0;
}
Explanation
Recursion occurs when a function calls itself. Every recursive function
must have a base case to stop further calls. Here, n <= 1
is the base case.
Expected Output
Enter a number: 5
Factorial = 120
Recursion — Exercise 2
Problem
Write a C program using recursion to calculate the sum of the first
N natural numbers.
Program
#include <stdio.h>
int sumNatural(int n)
{
// Base case
if (n == 0)
return 0;
// Recursive call
return n + sumNatural(n - 1);
}
int main()
{
int n;
printf("Enter N: ");
scanf("%d", &n);
printf("Sum = %d\n", sumNatural(n));
return 0;
}
Explanation
The function reduces the problem by one value in every call.
When n becomes zero, recursion stops.
Expected Output
Recursion — Exercise 3
Problem
Write a C program to generate the first N Fibonacci terms using recursion.
Program
#include <stdio.h>
int fibonacci(int n)
{
// Base cases
if (n == 0)
return 0;
if (n == 1)
return 1;
// Recursive calls
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main()
{
int n, i;
printf("Enter number of terms: ");
scanf("%d", &n);
printf("Fibonacci series: ");
// Generate Fibonacci terms
for (i = 0; i < n; i++)
{
printf("%d ", fibonacci(i));
}
return 0;
}
Explanation
Each Fibonacci term is obtained by adding the previous two terms.
The first two terms form the base cases of the recursive function.
Expected Output
Enter number of terms: 7
Fibonacci series: 0 1 1 2 3 5 8
Recursion — Exercise 4
Problem
Write a C program using recursion to calculate the power of a number.
Calculate baseexponent.
Program
#include <stdio.h>
long long power(int base, int exponent)
{
// Base case
if (exponent == 0)
return 1;
// Recursive multiplication
return base * power(base, exponent - 1);
}
int main()
{
int base, exponent;
printf("Enter base and exponent: ");
scanf("%d %d", &base, &exponent);
printf("Result = %lld\n", power(base, exponent));
return 0;
}
Explanation
The exponent is reduced by one during every recursive call.
When the exponent becomes zero, the function returns 1.
Expected Output
Enter base and exponent: 2 5
Result = 32
Recursion — Exercise 5
Problem
Write a C program using recursion to calculate the sum of digits
of an integer.
Program
#include <stdio.h>
int sumDigits(int n)
{
// Base case
if (n == 0)
return 0;
// Add last digit and process remaining digits
return (n % 10) + sumDigits(n / 10);
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0)
num = -num;
printf("Sum of digits = %d\n", sumDigits(num));
return 0;
}
Explanation
The last digit is extracted using % 10, while
n / 10 removes that digit before the next recursive call.
Expected Output
Enter a number: 4527
Sum of digits = 18
Recursion — Exercise 6
Problem
Write a C program using recursion to calculate the greatest common
divisor (GCD) of two numbers.
Program
#include <stdio.h>
int gcd(int a, int b)
{
// Base case
if (b == 0)
return a;
// Recursive Euclidean algorithm
return gcd(b, a % b);
}
int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("GCD = %d\n", gcd(a, b));
return 0;
}
Explanation
The Euclidean algorithm repeatedly replaces the pair with
(b, a % b). Recursion stops when the second value becomes zero.
Expected Output
Enter two numbers: 48 18
GCD = 6
Recursion — Exercise 7
Problem
Write a C program using recursion to print the numbers from N down to 1.
Program
#include <stdio.h>
void printDescending(int n)
{
// Stop when n reaches zero
if (n == 0)
return;
printf("%d ", n);
// Recursive call with smaller value
printDescending(n - 1);
}
int main()
{
int n;
printf("Enter N: ");
scanf("%d", &n);
printf("Numbers: ");
printDescending(n);
return 0;
}
Explanation
The function prints the current value before making the next recursive
call. Recursion ends when n becomes zero.
Expected Output
Enter N: 5
Numbers: 5 4 3 2 1
Recursion — Exercise 8
Problem
Write a C program using recursion to reverse the digits of a number.
Program
#include <stdio.h>
void reverseNumber(int n)
{
// Print last digit
printf("%d", n % 10);
// Continue with remaining digits
if (n >= 10)
{
reverseNumber(n / 10);
}
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0)
num = -num;
printf("Reversed number = ");
if (num == 0)
printf("0");
else
reverseNumber(num);
printf("\n");
return 0;
}
Explanation
The function prints the last digit first and then recursively processes
the remaining digits. This naturally produces the digits in reverse order.
Expected Output
Enter a number: 12345
Reversed number = 54321
Recursion — Exercise 9
Problem
Write a C program using recursion to calculate the sum of elements
of a one-dimensional array.
Program
#include <stdio.h>
int arraySum(int arr[], int n)
{
// Base case
if (n == 0)
return 0;
// Add last element and process remaining elements
return arr[n - 1] + arraySum(arr, n - 1);
}
int main()
{
int arr[100], n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("Sum = %d\n", arraySum(arr, n));
return 0;
}
Explanation
The function processes one array element at a time. The problem size
decreases from n to n - 1 until no elements remain.
Expected Output
Enter number of elements: 5
Enter 5 elements: 10 20 30 15 25
Sum = 100
Recursion — Exercise 10
Problem
Write a C program using recursion to convert a decimal number
into its binary representation.
Program
#include <stdio.h>
void decimalToBinary(int n)
{
// Process higher-order bits first
if (n > 1)
{
decimalToBinary(n / 2);
}
// Display the current binary digit
printf("%d", n % 2);
}
int main()
{
int num;
printf("Enter a decimal number: ");
scanf("%d", &num);
if (num == 0)
{
printf("Binary = 0\n");
}
else
{
printf("Binary = ");
decimalToBinary(num);
printf("\n");
}
return 0;
}
Explanation
The function repeatedly divides the decimal number by 2 using recursion.
The digits are printed while the recursive calls return, producing the
binary representation in the correct order.
Expected Output
Enter a decimal number: 13
Binary = 1101