C Program to Calculate Power Using Recursion

C Program to Calculate Power Using Recursion

A classroom recursion exercise needs both a smaller subproblem and a valid result when no smaller work remains.

Each call removes one factor from a nonnegative exponent, with one as exponent-zero result.

Before this exercise, review functions and copied parameters and recursion and stopping cases. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why recursive power uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Call the function with zero and explain the base result.

Recursive Power: follow the data

  1. Given data

    A classroom recursion exercise needs both a smaller subproblem and a valid result when no smaller work remains.

  2. Rule

    Each call removes one factor from a nonnegative exponent, with one as exponent-zero result.

  3. Observed result

    Power: 16

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

For base two and exponent four, returned products are 1,2,4,8 and16.

Each call owns a separate parameter value and waits for its smaller call to return. The result combines while calls finish, not merely while the arguments decrease.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
static unsigned long long power(unsigned base, unsigned exponent) {
  if (exponent == 0) return 1;
  return base * power(base, exponent - 1);
}
int main(void) {
  printf("Power: %llu\n", power(2, 4));
  return 0;
}

Expected output

Power: 16
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
static unsigned long long power(unsigned base, unsigned exponent) {
  if (exponent == 0) return 1;
  return base * power(base, exponent - 1);
}
int main(void) {
  printf("Power: %llu\n", power(2, 4));
  return 0;
}

The starting argument is 4.

For base two and exponent four, returned products are 1,2,4,8 and16.

Follow the worked calculation

  1. Start with the sample

    The starting argument is 4.

  2. Apply the rule

    For base two and exponent four, returned products are 1,2,4,8 and16.

  3. Check the result

    Power: 16

    Power: 16
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
static unsigned long long power(unsigned base, unsigned exponent) {
  if (exponent == 0) return 1;
  return base * power(base, exponent - 1);
}
int main(void) {
  printf("Power: %llu\n", power(2, 4));
  return 0;
}

Call the function with zero and explain the base result.

Worked answer

The base call returns 1 without making another recursive call. That result is the appropriate arithmetic identity for power, not an arbitrary stopping label.

C17 · complete program

#include <stdio.h>
static unsigned long long power(unsigned base, unsigned exponent) {
  if (exponent == 0) return 1;
  return base * power(base, exponent - 1);
}
int main(void) {
  printf("Power: %llu\n", power(2, 0));
  return 0;
}

Expected output

Power: 1
Run in C compiler →

Recognize the important boundaries

Restrict the input so both recursion depth and arithmetic stay within documented limits; a nonnegative contract alone does not bound resource use.

Recursion depth consumes execution resources. A loop can express the same small arithmetic recurrence without building a deep call chain.

Check your understanding

Choose the answer, then compare the reasoning for every option.

What must a terminating recursive call do?
Choose one answer. You can try again.

Answer and explanation

Reach a base case without another recursive call.

Correct. The recursive step must move allowed inputs toward that case.

Use the idea independently

Each call removes one factor from a nonnegative exponent, with one as exponent-zero result.

Continue with call and return tracing.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Program to Find GCD Using Recursion →