The C++ language provides a “pow()” function that aids in finding the power of any number. We utilize this function when we want to find the power of the number in C++. This function takes two arguments: the first argument is the “base” or the number whose power we want to find and the next argument is the exponent in this function. This “pow()” function is defined inside the “math.h” or “cmath” header file in C++ programming. Let’s do some codes and check how this method computes the power of different numbers in C++.
Example 1:
The header files are included first: “iostream” and “cmath”. The “iostream” is included as input/output and the other functions are defined in it. The “cmath” is included as we have to find the power of a number with the aid of the “pow()” function which is defined in this header file. Then, we must add the “std” namespace so we don’t need to add it with the functions separately.
Underneath this, we invoke the “main()” method and then print some text using the ”cout” as it aids in printing in C++. Then, we utilize the “pow()” function in which we place “5” as the first parameter which is the “base” here. Then, we place “3” as the second parameter which is the “exponent” of that number. Now, this “pow()” function finds the power of the number “5” which is raised to the power of “3” and displays the power result as we put this “pow()” function inside the “cout”.
Code 1:
#include <cmath>
using namespace std;
int main() {
cout << "We are computing the power of the number here!" << endl;
cout << pow(5, 3);
return 0;
}
Output:
The answer of “5” raised to the power “3” is “125” which is also rendered in the following. We get this result with the aid of the “pow()” function.
Example 2:
The “iostream” as well as the “cmath” header files are included here. Then, the “namespace std” is placed. Underneath this, the “main()” is called. Then, we declare three variables which are “value”, “exponent”, and “outcome” as the “int” data type. Now, we assign “6” to the “value” variable and “5” to the “exponent” variable.
After this, we utilize the “pow()” function. Then, we pass both variables to this “pow()” function and assign its result to the “outcome” variable. After this, we utilize the “cout” and print the statement here first. Then, in the next “cout”, we display the “value”, “exponent”, as well as the “outcome”.
Code 2:
#include <cmath>
using namespace std;
int main () {
int value, exponent, outcome;
value = 6;
exponent = 5;
outcome = pow(value, exponent);
cout << "We utilize the pow function here!" << endl;
cout << value << " ^ " << exponent << " = " << outcome;
return 0;
}
Output:
The “pow()” function helps us arrive at the answer of “6” raised to the power of “5” which is “7776” as presented in the following:
Example 3:
In this code, we will find the power of the float number where the exponent is also the float data type. Here, “namespace std” is inserted after the “iostream” and “cmath” header files are included. The “main()” function is then called, and three variables named “n_value”, “e_value”, and “p_result” are declared as the “float” data type. We now set the “n_value” variable to “8.2” and the “e_value” variable to “3.2”.
Then, we use the “pow()” function, passing both variables to it and assigning the function’s output to the “p_result” variable. Next, we use the “cout” function to print the statement. In the following “cout”, we will show the “n_value”, “e_value”, and “p_result” as follows:
Code 3:
#include <cmath>
using namespace std;
int main () {
float n_value, e_value, p_result ;
n_value = 8.2;
e_value =3.2;
p_result = pow(n_value, e_value);
cout << "We utilize the pow function here!" << endl;
cout << n_value << " ^ " << e_value << " = " << p_result;
return 0;
}
Output:
Here is the outcome in which we find the power of the float number whose exponent is also the float number with the assistance of the “pow()” function.
Example 4:
This code calculates the power of the double number with the exponent as the double data type. In this case, the “iostream” and “cmath” header files are included before the “namespace std” is included. The following code calls the “main()” function and declares three variables with the “double” data type and these are “d_Num”, “d_Expo”, and “d_PowRes”. The “d_num” and “d_expo” variables are now initialized with “2.25” and “5.21”, respectively.
Next, we assign the “pow()” function’s output to the “d_PowRes ” variable and use the “pow()” function, passing both variables to it. Next, we print the sentence here using the “cout” function. The “d_Num”, “d_Expo”, and “d_PowRes” are displayed in the ensuing “cout”.
Code 4:
#include <cmath>
using namespace std;
int main () {
double d_Num, d_Expo , d_PowRes ;
d_Num = 2.25;
d_Expo = 5.21;
d_PowRes = pow(d_Num, d_Expo);
cout << "We utilize the pow function here!" << endl;
cout << "The number is " << d_Num << " Its exponent is " << d_Expo << endl;
cout << d_Num << " ^ " << d_Expo << " = " << d_PowRes;
return 0;
}
Output:
This results from using the “pow()” function to get the power of a double number whose exponent is likewise a double number.
Example 5:
In this last code, we will find the power of a number whose exponent is the negative number. The “bits/stdc++.h” and the “math.h” header files are included here along with the “iostream” header file in this code as the “math.h” header file contains the “pow()” function’s definition.
After this, we add the “std” namespace. Then, the “main()” is now called. The “b” variable is initialized here as the “float” data type and the value “4.87” is assigned to this variable. Underneath this, the “int” variable “e” is initialized with the negative value which is “-2”. Then, the “float result” is also declared here. Below this, we initialize this “result” variable and assign the “pow()” function to this variable in which both variables “b” and “e” are placed as the parameters.
Here, we insert the base of the “float” data type. The exponent is the negative integer value. Now, the outcome that we get after applying this function is saved in the “result” variable which is displayed in the following using the “cout” underneath this.
Code 5:
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
int main () {
float b = 4.87;
int e = -2;
float result;
result = pow(b, e);
cout << "The exponent is negative here" << endl;
cout << b << " ^ " << e << " = " << result;
return 0;
}
Output:
This renders the outcome where we insert the negative exponent to the float base number in our code. We get this power result with the “pow()” function.
Conclusion
The “pow()” function in C++ is explored here in this guide. We defined it as computing the power of any number where we utilize this “pow()” function. We also illustrated several examples of applying this “pow()” function on the numbers of different data types in C++ programming. We also computed the power of the number where the exponent is the negative value and rendered the outputs of all code in this guide.