0Pricing
C Academy · Lesson

Classic Recursive Problems

Factorial and Fibonacci.

Classic Recursive Problems is a free C Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Classic Problems

Some problems map naturally onto recursion. Learning the classics gives you patterns you can reuse.

In this lesson we cover factorial, Fibonacci, sum of digits, greatest common divisor, and reversing output.

Factorial

The factorial of n is n times the factorial of n minus 1, with 1! equal to 1.

This is the textbook recursion: a clear base case and one recursive call.

#include <stdio.h>

long factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

int main(void) {
    printf("%ld\n", factorial(6));
    return 0;
}

Fibonacci Numbers

Each Fibonacci number is the sum of the two before it. The recursive definition needs two base cases: fib(0)=0 and fib(1)=1.

This version makes two calls per step.

int fib(int n) {
    if (n < 2) return n;
    return fib(n - 1) + fib(n - 2);
}

Running Fibonacci

Here is the full program. fib(10) should print 55.

Note that this naive version repeats work, so it is slow for large n.

#include <stdio.h>

int fib(int n) {
    if (n < 2) return n;
    return fib(n - 1) + fib(n - 2);
}

int main(void) {
    printf("%d\n", fib(10));
    return 0;
}

Sum Of Digits

To add the digits of a number, take the last digit with n % 10 and recurse on the rest with n / 10.

The base case is when n reaches 0.

int digit_sum(int n) {
    if (n == 0) return 0;
    return (n % 10) + digit_sum(n / 10);
}

Digit Sum In Action

For 1234 the sum is 1+2+3+4 = 10. Let us confirm with a full program.

#include <stdio.h>

int digit_sum(int n) {
    if (n == 0) return 0;
    return (n % 10) + digit_sum(n / 10);
}

int main(void) {
    printf("%d\n", digit_sum(1234));
    return 0;
}

Greatest Common Divisor

Euclid's algorithm is naturally recursive. The GCD of a and b equals the GCD of b and a % b.

When b becomes 0, a is the answer.

int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

GCD Full Program

The GCD of 48 and 18 is 6. This program prints it.

#include <stdio.h>

int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

int main(void) {
    printf("%d\n", gcd(48, 18));
    return 0;
}

Reversing A Number

Recursion can also drive output. By printing the last digit after recursing, you naturally reverse the order of processing.

This helper prints each digit of a number on its own using recursion.

#include <stdio.h>

void print_digits(int n) {
    if (n == 0) return;
    print_digits(n / 10);
    printf("%d ", n % 10);
}

int main(void) {
    print_digits(729);
    printf("\n");
    return 0;
}

Power Function

Raising a base to an exponent is recursive too: base^exp equals base times base^(exp-1).

The base case is exponent 0, which returns 1.

long power(int base, int exp) {
    if (exp == 0) return 1;
    return base * power(base, exp - 1);
}

Patterns You Will Reuse

Notice the shared shape: check a base case, then combine the current step with the result of a smaller call.

Once you spot this pattern, many problems become short recursive functions.

Quick Check

Pick the correct base cases.

Recap

Factorial, Fibonacci, digit sum, GCD, and power all share one recursive pattern: handle the base case, then combine the current value with a smaller subproblem.

These templates carry over to many other tasks.

Frequently asked questions

Is the “Classic Recursive Problems” lesson free?

Yes — the full text of “Classic Recursive Problems” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.

What will I learn in “Classic Recursive Problems”?

Factorial and Fibonacci. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Classic Recursive Problems” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. How Recursion Works
  2. Classic Recursive Problems
  3. Recursion vs Iteration
  4. Avoiding Stack Overflow
← Back to C Academy