Popular Tutorials
Start Learning C++Certification Courses
Created with over a decade of experience and thousands of feedback.
C++ pow()
In this tutorial, we will learn about the C++ pow() function with the help of examples.
The pow() function returns the result of the first argument raised to the power of the second argument. This function is defined in the cmath header file.
In C++, pow(a, b) = ab.
Example
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// computes 5 raised to the power 3
cout << pow(5, 3);
return 0;
}
// Output: 125
pow() Syntax
The syntax of the pow() function is:
pow(double base, double exponent);
pow() Parameters
The pow() function takes two parameters:
- base - the base value
- exponent - exponent of the base
pow() Return Value
The pow() function returns:
- the result of
baseexponent - 1.0 if
exponentis zero - 0.0 if
baseis zero
pow() Prototypes
The prototypes of pow() as defined in the cmath header file are:
double pow(double base, double exponent);
float pow(float base, float exponent);
long double pow(long double base, long double exponent);
// for other argument types
Promoted pow(Type1 base, Type2 exponent);
Since C++ 11,
- if any argument passed to
pow()is long double, the return typePromotedislong double - else, the return type
Promotedisdouble
Example 1: C++ pow()
#include <iostream>
#include <cmath>
using namespace std;
int main () {
double base, exponent, result;
base = 3.4;
exponent = 4.4;
result = pow(base, exponent);
cout << base << " ^ " << exponent << " = " << result;
return 0;
}
Output
3.4 ^ 4.4 = 218.025
Example 2: pow() With Different Arguments
#include <iostream>
#include <cmath>
using namespace std;
int main () {
long double base = 4.4, result;
int exponent = -3;
result = pow(base, exponent);
cout << base << " ^ " << exponent << " = " << result << endl;
// initialize int arguments
int int_base = -4, int_exponent = 6;
double answer;
// pow() returns double in this case
answer = pow(int_base, int_exponent);
cout << int_base << " ^ " << int_exponent << " = " << answer;
return 0;
}
Output
4.4 ^ -3 = 0.0117393 -4 ^ 6 = 4096
Also Read:
Did you find this article helpful?