C bootcamp · Lab 34

Integer power

easyMath10 minLesson: C Math

Read the question, write C on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read integers a and b (b ≥ 0) and print a raised to the power b.

Anything to the power 0 is 1. A loop that multiplies is enough — you do not need pow from math.h (that returns a double).

Input. Two integers a and b.

Output. One integer: a^b.

Constraints

  • 0 ≤ b ≤ 12
  • Results fit in a 32-bit int.

Examples

Example 1
Input
2 10
Output
1024
Example 2
Input
5 0
Output
1
Hint
  1. long result = 1; for (int i = 0; i < b; i++) result *= a;
Show correct code

Peek only after you have tried. You can still Check your own version.

#include <stdio.h>

int main(void) {
  int a, b;
  if (scanf("%d %d", &a, &b) != 2) return 1;
  long result = 1;
  for (int i = 0; i < b; i++) result *= a;
  printf("%ld\n", result);
  return 0;
}
main.cC17 · gcc · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.