In this article, you will learn to compute power to a number manually, and by using pow() function.

To understand this example, you should have the knowledge of following C++ programming topics:

This program takes two numbers from the user (a base number and an exponent) and calculates the power.

Power of a number = base^{exponent}

```
#include <iostream>
using namespace std;
int main()
{
int exponent;
float base, result = 1;
cout << "Enter base and exponent respectively: ";
cin >> base >> exponent;
cout << base << "^" << exponent << " = ";
while (exponent != 0) {
result *= base;
--exponent;
}
cout << result;
return 0;
}
```

**Output**

Enter base and exponent respectively: 3.4 5 3.4^5 = 454.354

The above technique works only if the exponent is a positive integer.

If you need to find the power of a number with any real number as an exponent, you can use `pow()`

function.

```
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float base, exponent, result;
cout << "Enter base and exponent respectively: ";
cin >> base >> exponent;
result = pow(base, exponent);
cout << base << "^" << exponent << " = " << result;
return 0;
}
```

**Output**

Enter base and exponent respectively: 2.3 4.5 2.3^4.5 = 42.44

It takes a lot of effort and cost to maintain Programiz. We would be grateful if you support us by either:

**Disabling AdBlock on Programiz. We do not use intrusive ads.**

or

Donate on Paypal
By using Programiz, you agree to use cookies as stated in our Privacy policy Continue