Solved C Recursion Problems: Factorial, Fibonacci, Sum of Digits & More

“`html

🔁 Solved C Recursion Problems

Recursion is an important concept in C programming where a function calls itself to solve a problem step by step. In this post, we solve several practical C recursion problems, including factorial, sum of digits, prime factors, Fibonacci numbers and decimal-to-binary conversion.

💡 Tip: Try to trace the recursive calls and their return values while studying each program. This will help you understand how recursion works inside the function call stack.

“`
💡 If you are new to recursion, first read our basic 1 + 2 + 3 + 4 + 5 recursion example . Then continue with this post to practice and solve more C recursion problems. This will help you understand recursion step by step and build a strong foundation in C programming.
“`html

🔁 Solved C Recursion Problems

In this post, we solve some important C programming recursion problems step by step. These problems are useful for beginners, programming students and GATE aspirants who want to understand how recursive functions work in C.

💡 Tip: Before studying these problems, understand the basic idea of function calls, base conditions and recursive calls. Once these three ideas are clear, recursion becomes much easier to follow.

🔹 Question 1: Factorial Using Recursion

Problem: Find the factorial of a given number using recursion.

🧠 Recursion Idea:
For a positive integer n:
n! = n × (n − 1)!
The recursion stops when n = 0 or n = 1.
#include <stdio.h>

long long 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 = %lld", factorial(n));

    return 0;
}

✅ Example: 5! = 5 × 4 × 3 × 2 × 1 = 120

🔹 Question 2: Sum of Digits

Problem: A 5-digit positive integer is entered through the keyboard. Calculate the sum of its digits:

  • Without recursion
  • Using recursion
💡 Logic: The last digit can be obtained using n % 10. Remove the last digit using n / 10.

1️⃣ Without Recursion

#include <stdio.h>

int main()
{
    int n, sum = 0;

    printf("Enter a 5-digit number: ");
    scanf("%d", &n);

    while (n != 0)
    {
        sum = sum + n % 10;
        n = n / 10;
    }

    printf("Sum of digits = %d", sum);

    return 0;
}

2️⃣ Using Recursion

#include <stdio.h>

int sumDigits(int n)
{
    if (n == 0)
        return 0;

    return (n % 10) + sumDigits(n / 10);
}

int main()
{
    int n;

    printf("Enter a 5-digit number: ");
    scanf("%d", &n);

    printf("Sum of digits = %d", sumDigits(n));

    return 0;
}

Example: 12345 → 1 + 2 + 3 + 4 + 5 = 15

🔹 Question 3: Prime Factors Using Recursion

Problem: A positive integer is entered through the keyboard. Find its prime factors and modify the function to obtain the prime factors recursively.

🧠 Idea: Try dividing the number by successive integers. Whenever a divisor divides the number exactly, print it and continue with the reduced number.
#include <stdio.h>

void primeFactors(int n, int divisor)
{
    if (n == 1)
        return;

    if (n % divisor == 0)
    {
        printf("%d ", divisor);
        primeFactors(n / divisor, divisor);
    }
    else
    {
        primeFactors(n, divisor + 1);
    }
}

int main()
{
    int n;

    printf("Enter a positive integer: ");
    scanf("%d", &n);

    printf("Prime factors: ");

    primeFactors(n, 2);

    return 0;
}

Example: 60 → 2 × 2 × 3 × 5

🔎 Observe: The recursive function keeps reducing the number until n == 1.

🔹 Question 4: First 25 Fibonacci Numbers

Problem: Write a recursive function to obtain the first 25 numbers of the Fibonacci sequence.

The sequence begins: 1, 1, 2, 3, 5, 8, 13, 21, 34…

🧠 Formula:
F(n) = F(n − 1) + F(n − 2)
#include <stdio.h>

int fibonacci(int n)
{
    if (n == 1 || n == 2)
        return 1;

    return fibonacci(n - 1) + fibonacci(n - 2);
}

int main()
{
    int i;

    printf("First 25 Fibonacci numbers:\n");

    for (i = 1; i <= 25; i++)
        printf("%d ", fibonacci(i));

    return 0;
}
📌 Important: Each Fibonacci number is obtained from the sum of the previous two numbers.

🔹 Question 5: Decimal to Binary Using Recursion

Problem: A positive integer is entered through the keyboard. Find its binary equivalent using recursion.

💡 Key idea: Divide the number by 2 repeatedly. The remainder gives the binary digit. The recursive calls are used so that the digits are displayed in the correct order.
#include <stdio.h>

void binary(int n)
{
    if (n > 1)
        binary(n / 2);

    printf("%d", n % 2);
}

int main()
{
    int n;

    printf("Enter a positive integer: ");
    scanf("%d", &n);

    printf("Binary equivalent = ");

    binary(n);

    return 0;
}

Example: 10 → 1010

🔎 Why does this work? The recursive call reaches the smallest value first. The printf() statement executes while the calls return, producing the binary digits in the proper order.

🔹 Question 6: Display Binary Equivalent

Problem: Write a function to find the binary equivalent of a given decimal integer and display it.

💡 Note: This problem is closely related to Question 5. It is useful for comparing different implementations of decimal-to-binary conversion.
#include <stdio.h>

void displayBinary(int n)
{
    if (n > 1)
        displayBinary(n / 2);

    printf("%d", n % 2);
}

int main()
{
    int n;

    printf("Enter a decimal integer: ");
    scanf("%d", &n);

    printf("Binary equivalent = ");

    displayBinary(n);

    return 0;
}

Example: Decimal 25 → Binary 11001

🔹 Question 7: Factorial of an Integer

Problem: Write a function to calculate the factorial value of any integer entered through the keyboard.

🧠 Recursion pattern:
factorial(n) = n × factorial(n − 1)
Base condition: factorial(0) = 1
#include <stdio.h>

long long fact(int n)
{
    if (n == 0)
        return 1;

    return n * fact(n - 1);
}

int main()
{
    int n;

    printf("Enter an integer: ");
    scanf("%d", &n);

    printf("Factorial = %lld", fact(n));

    return 0;
}

Example: 6! = 720

🎯 What You Should Notice in These Programs

  • A recursive function must have a base condition.
  • The function must make a recursive call with a smaller or simpler problem.
  • Recursive calls are stored in the function call stack.
  • Some problems, such as Fibonacci, involve more than one recursive call.
  • Understanding what happens when recursive calls return is very important.

🚀 Practice tip: Don’t just run these programs. Try to trace the function calls on paper. This is one of the best ways to understand recursion in C.

💻 Keep Learning C Programming!
More programming tutorials, solved problems and GATE preparation resources are available at EngineersTutor.
🌐 EngineersTutor.com   |   ▶️ EngineersTutor YouTube Channel
“`
0

Gopal Krishna

Hey Engineers, welcome to the award-winning blog,Engineers Tutor. I'm Gopal Krishna. a professional engineer & blogger from Andhra Pradesh, India. Notes and Video Materials for Engineering in Electronics, Communications and Computer Science subjects are added. "A blog to support Electronics, Electrical communication and computer students".

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »