C Program to Find Factorial of a Number

C Program to Find Factorial of a Number

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

The factorial of a nonnegative integer multiplies the positive integers through n; zero factorial is one.

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

  1. Explain

    Explain why factorial 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.

Factorial: 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

    The factorial of a nonnegative integer multiplies the positive integers through n; zero factorial is one.

  3. Observed result

    Factorial: 120

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

Understand the operation

The product starts at one and changes to 2, 6, 24 and 120 for n = 5. A zero input leaves the product at one.

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

Expected output

Factorial: 120
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

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

The chosen n is 5.

The product starts at one and changes to 2, 6, 24 and 120 for n = 5. A zero input leaves the product at one.

Follow the worked calculation

  1. Start with the sample

    The chosen n is 5.

  2. Apply the rule

    The product starts at one and changes to 2, 6, 24 and 120 for n = 5. A zero input leaves the product at one.

  3. Check the result

    Factorial: 120

    Factorial: 120
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 = 5;
  unsigned long long result = 1;
  for (int i = 2; i <= n; i++) result *= (unsigned)i;
  printf("Factorial: %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 = 2; i <= n; i++) result *= (unsigned)i;
  printf("Factorial: %llu\n", result);
  return 0;
}

Expected output

Factorial: 1
Run in C compiler →

Recognize the important boundaries

Restrict unsigned-long-long factorial to n at most 20 for the minimum 64-bit range; larger input needs explicit overflow detection or a larger-number representation.

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

The factorial of a nonnegative integer multiplies the positive integers through n; zero factorial is 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 Calculate Power of a Number →