Raise an integer to a power
Calculate a base raised to a non-negative integer exponent without using Math.Pow.
Course progress
Problem 55 of 100
Example → approach → runnable solution
Worked example
See the problem in action
Trace a concrete input through to the verified result.
Example input
base = 3; exponent = 4
Expected output
81
What is happening?
Multiply four copies of the base: 3 x 3 x 3 x 3 = 81.
Solution strategy
Build the right mental model
Start with the multiplicative identity 1 and multiply by the base once for each exponent step.
Core concepts
loops · arithmetic
Complexity
Time O(exponent), space O(1).
Runnable solution
Learn from working code
Compare languages, copy the code, or open it in the compiler.
Complete C# solution for integer-power-loop
using System;
class Program
{
static long Power(int baseValue, int exponent)
{
if (exponent < 0) throw new ArgumentOutOfRangeException("exponent");
long result = 1;
for (int i = 0; i < exponent; i++) result *= baseValue;
return result;
}
static void Main()
{
Console.WriteLine(Power(3, 4));
}
}Knowledge check
Questions students often ask
What is the main idea behind raise an integer to a power?
Start with the multiplicative identity 1 and multiply by the base once for each exponent step.
What is the time and space complexity of this solution?
Time O(exponent), space O(1).
Do all language tabs solve the same problem?
Yes. Every program uses the same example and produces the verified output shown on this page. The syntax and a few language-specific data structures differ.