Factorial Calculation in C

Level 395 keystrokes

Calculates the factorial of a number using a for loop.

C code

// fact starts at 1 so that the first multiplication keeps its value
1int n = 5, fact = 1;
// i takes the values 1 to n
2for (int i = 1; i <= n; i++) {
// Running product: 1, 2, 6, 24, 120
3 fact *= i;
4}
// Print 5! = 120 (a 32-bit int overflows from 13! on)
5printf("%d! = %d\n", n, fact);

Output

5! = 120
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C code and how often they appear.

  • =× 6
  • ;× 5
  • ,× 3
  • "× 2
  • (× 2
  • )× 2
  • %× 2
  • +× 2
  • !× 1
  • {× 1

More C examples at level 3

← C typing practice