In this post, we will learn **how to calculate the power of a number using C++** Programming language.

An expression that represents repeated multiplication of the same factor is called a **power**. For example: the power of a **base** ‘**5**‘ of **exponent** ‘**2**‘ is **5 ^{2}** =

**5 x 5**=

**25**.

We will calculate the power of a number using the following approaches:

- Using pow() function
- Using While Loop

So, without further ado, let’s begin this tutorial.

**C++ Program to Calculate the Power of a Number**

// C++ Program to Calculate the Power of a Number #include <iostream> #include <cmath> using namespace std; int main(){ float base, exp, power; // Asking for input cout << "Enter the base: "; cin >> base; cout << "Enter the exponent: "; cin >> exp; // Calculating power using pow() function power = pow(base, exp); // Displaying output cout << base << "^" << exp << " = " << power << endl; return 0; }

**Output**

```
Enter the base: 2
Enter the exponent: 5
2^5 = 32
```

**How Does This Program Work ?**

float base, exp, power;

In this program, we have declared **three floating data type** variables named **base**, **exp** and **power**.

// Asking for input cout << "Enter the base: "; cin >> base; cout << "Enter the exponent: "; cin >> exp;

Now, the user is asked to enter the **base** and **exponent**.

// Calculating power using pow() function power = pow(base, exp);

The power of the entered number is calculated using the** pow()** function. The **pow()** function computes a base number raised to the power of an exponent. This function is located in the **cmath** header file.

The power of the number gets stored in the **power** named variable.

// Displaying output cout << base << "^" << exp << " = " << power << endl;

The power of the entered number is displayed on the screen using the **cout** statement.

**C++ Program to Calculate the Power of a Number Using For Loop**

// C++ Program to Calculate the Power of a Number Using For Loop #include <iostream> using namespace std; int main(){ float base, exp, power = 1; // Asking for input cout << "Enter the base: "; cin >> base; cout << "Enter the exponent: "; cin >> exp; // Calculating power for (int i = 0; i < exp; i++){ power = power * base; } // Displaying output cout << base << "^" << exp << " = " << power << endl; return 0; }

**Output**

```
Enter the base: 3
Enter the exponent: 3
3^3 = 27
```

**Conclusion**

I hope after going through this post, you understand **how to calculate the power of a number using C++** Programming language.

If you have any doubt regarding the post, then contact us in the comment section. We will be delighted to assist you.

**Also Read:**