C++ pow()

C++ pow(base, exponent) returns the value of a base raised to the power of another number exponent.

pow(base, exponent) = base^exponent

Syntax

The syntax of C++ pow() is

pow(base, exp)

where

ParameterDescription
baseBase number. A double, float, long double, or integral type value.
expExponent or power. A double, float, long double, or integral type value.

Returns

The return value depends on the type of value passed for parameter x.

The return value of pow(base, exp) is

  • double if base is double.
  • float if base is float.
  • long double if base is long double.
  • Promoted if base and exp are of different types. Promoted is long double if any of the argument is long double, else the Promoted is double.

The synopsis of pow() function is

double pow(double x, double y);
float pow(float x, float y);
long double pow(long double x, long double y);
Promoted pow(Type1 x, Type2 y); // for combinations of integral types

pow() is a function of cmath library. Include cmath library at the start of program, if using pow() function.

Example

In this example, we read two values from user into variables x and y, and compute the value of x to the power of y using pow() function.

C++ Program

#include <iostream>
#include<cmath>
using namespace std;

int main() {
    double x, y;
    cout << "Enter base : ";
    cin >> x;
    cout << "Enter exponent : ";
    cin >> y;
    
    double result = pow(x, y);
    cout << "pow(" << x << ", " << y << ") : " << result << endl;
}

Output

Enter base : 2
Enter exponent : 5
pow(2, 5) : 32
Program ended with exit code: 0
Enter base : 25
Enter exponent : 0
pow(25, 0) : 1
Program ended with exit code: 0
Enter base : 4
Enter exponent : -1
pow(4, -1) : 0.25
Program ended with exit code: 0

Conclusion

In this C++ Tutorial, we learned the syntax of C++ pow(), and how to use this function to find the value of a number raised to a power, with the help of examples.