C Program to Calculate Power of a Number

C Program to Calculate Power of a Number

A classroom arithmetic challenge needs a result built from a sequence, not a formula copied without understanding its starting value.

A nonnegative integer power multiplies the base by itself exponent times; exponent zero gives one.

Before this exercise, review repeating work with loops. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why integer power uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Use 0 and explain how the initial accumulator affects the answer.

Integer Power: follow the data

  1. Given data

    A classroom arithmetic challenge needs a result built from a sequence, not a formula copied without understanding its starting value.

  2. Rule

    A nonnegative integer power multiplies the base by itself exponent times; exponent zero gives one.

  3. Observed result

    Integer Power: 8

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 three, the product changes 1 → 2 → 4 → 8.

An accumulator stores the result after each completed iteration. Choose its initial value to match the operation: zero for addition, one for multiplication. The loop must eventually stop before the counter or result exceeds its representation.

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>
int main(void) {
  int n = 3;
  unsigned long long result = 1;
  for (int i = 0; i < n; i++) result *= 2;
  printf("Integer Power: %llu\n", result);
  return 0;
}

Expected output

Integer Power: 8
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int n = 3;
  unsigned long long result = 1;
  for (int i = 0; i < n; i++) result *= 2;
  printf("Integer Power: %llu\n", result);
  return 0;
}

The chosen n is 3.

For base two and exponent three, the product changes 1 → 2 → 4 → 8.

Follow the worked calculation

  1. Start with the sample

    The chosen n is 3.

  2. Apply the rule

    For base two and exponent three, the product changes 1 → 2 → 4 → 8.

  3. Check the result

    Integer Power: 8

    Integer Power: 8
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>
int main(void) {
  int n = 3;
  unsigned long long result = 1;
  for (int i = 0; i < n; i++) result *= 2;
  printf("Integer Power: %llu\n", result);
  return 0;
}

Use 0 and explain how the initial accumulator affects the answer.

Worked answer

The changed loop produces 1. With zero iterations, the initialized accumulator is the result; this is why multiplying examples must not start from zero.

C17 · complete program

#include <stdio.h>
int main(void) {
  int n = 0;
  unsigned long long result = 1;
  for (int i = 0; i < n; i++) result *= 2;
  printf("Integer Power: %llu\n", result);
  return 0;
}

Expected output

Integer Power: 1
Run in C compiler →

Recognize the important boundaries

Restrict the base and exponent so every product fits; unsigned wraparound is not the intended mathematical power.

The example uses a deliberately small nonnegative input. A negative input needs an explicit rejection or a separately defined interpretation.

Check your understanding

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

Why does this multiplication accumulator start at one?
Choose one answer. You can try again.

Answer and explanation

One is the neutral value for multiplication.

Correct. The initial value must preserve the operation’s identity before any terms are processed.

Use the idea independently

A nonnegative integer power multiplies the base by itself exponent times; exponent zero gives one.

Continue with loop initialization and termination.

Continue with recursive alternatives.

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 Print the Fibonacci Series →