Power Function in C

Level 3111 keystrokes

Defines a function to calculate the power of a number.

C code

// b is the base and e the exponent
1int power(int b, int e) {
// Start at 1, which is also the answer when e is 0
2 int r = 1;
// Multiply r by b exactly e times
3 for (int i = 0; i < e; i++) r *= b;
// Return the result to the caller
4 return r;
5}
// 2 to the power 8 = 256 (C has no ^ power operator; ^ is XOR)
6printf("%d", power(2, 8));

Output

256
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
  • (× 4
  • )× 4
  • ,× 3
  • =× 3
  • "× 2
  • +× 2
  • {× 1
  • }× 1
  • *× 1

More C examples at level 3

← C typing practice