Search⌘ K

Solution Review: Calculate the Power of a Number

Explore how to calculate the power of a number using a for loop in C++. This lesson helps you understand iterative multiplication by the exponent value, reinforcing your grasp of loops and basic arithmetic operations in C++ programming.

We'll cover the following...

Solution #

C++
#include <iostream>
using namespace std;
int main() {
// Initialize variable
int base = 2, exponent = 3, result = 1;
// for loop initialization
for (int counter = 1; counter <= exponent; counter++) {
// for loop body
result *= base;
}
// Exit for loop
cout << result;
return 0;
}

Explanation

...