In C++ programming, this function is mainly used to find the power of a given number. Basically, the pow() function is responsible for calculating the power of any integer or variable. It is used to return the result of the first argument with respect to the power of the second argument. This function is defined in the cmath header file of C++. Hence, we need to include the  #include<math.h> in c/c++ to use that pow() function in our program, and then two numbers are passed. It accepts a double integer as input and also outputs a double integer as output. If you give an integer to the power function, the function converts it into a double data type for the execution of the program. It is a really useful and popular function in C++ programming that can make the evaluation of a power much easier and faster.

Learn From The Best Mentors in the Industry!

Automation Testing Masters ProgramExplore Program
Learn From The Best Mentors in the Industry!

For example: Given two numbers base and the exponent, pow() function evaluates x with respect to the power of y i.e. x^y. Example – pow(4 , 2); Then we will get the result as 4^2, which is 16. 

Example:

Given Input: 3.0, 4.0

Output: 81

Explanation: 

pow(3.0, 4.0) executes 3.0 raised to

the power 4.0, which equals 81.

Input: 7.0, 3.0

Output: 343

Explanation: 

pow(7.0, 3.0) executes 7.0 raised to

the power 3.0, which equals 343.

Syntax of C++ Power

Here, we are considering b = base and e = exponent.

double pow(double b, double e);  

float pow(float b, float e);  

long double pow(long double b, long double e);  

promoted pow(type1  b, type2 e);  

We should also note here that if any argument is of long double type, then the return type is also promoted to long double. If it does not happen, the return type is promoted to double.

Parameter of C++ Power

The pow() function has basically two parameters:

b - base - It is the base, whose power is to be calculated.

e - exponent - It is the exponent to the base.

Return of C++ Power

When the pow() function is used then it returns the following as its output:

  •  the base 'b' raised to the power of exponent 'e'.
  • 1.0 if the exponent is zero
  • 0.0 if the base is zero

Example - 1:

Now we are going to see a simple example when both base and exponent are of integer type in C++.

#include <iostream>  

#include<cmath>  

using namespace std;  

int main()  

{  

int base=5;  

  int exponent=3;  

  int power=pow(base,exponent);  

  std::cout << "Power of the given number is :" <<power;  

  return 0;  

}  

Output:

Power of the given number is : 125

Prepare Yourself to Answer All Questions!

Automation Testing Masters ProgramExplore Program
Prepare Yourself to Answer All Questions!

Example - 2:

In this example, we will see an example when the base is of float type and the exponent is of integer type.

#include <iostream>  

#include<cmath>  

using namespace std;  

int main()  

{  

   int base = 2.5;  

  int exponent = 3;  

  int power=pow(base,exponent);  

  std::cout << "Power of the given number is :" <<power;  

  return 0;  

}  

Output:

Power of the given number is : 27

Example - 3:

This is the last example of pow() function. In this example, we are going to see a simple example when both base and exponent are of float type.

#include <iostream>  

#include<cmath>  

using namespace std;  

int main()  

{  

   int base=4.5;  

  int exponent=3.5;  

  int power=pow(base,exponent);  

  std::cout << "Power of the given number is :" <<power;  

  return 0;  

}  

Output:

Power of the given number is : 64

Learn 15+ In-Demand Tools and Skills!

Automation Testing Masters ProgramExplore Program
Learn 15+ In-Demand Tools and Skills!

Working of the pow() Function With Integers in C++

In C++, the pow() function is responsible for taking ‘double’ as the arguments of the program and returns a ‘double’ value as the output of the program. This function does not continuously work for integers. For instance, the power of (4,2) i.e. pow(4, 2). When we assign an integer, it outputs 16 on some of the compilers and also works fine with some other compilers. But pow(4, 2) without any assignment to an integer outputs 16 and works very accurately. 

  • This is because 16 could also be stored as 15.9999999 or 16.0000000001 because the return type is double. When it is assigned to int, 16.0000000001 will become 16, but 15.9999999 will give output 15.
  • To overcome this problem and give output the accurate answer in integer format, we can add 1e-9 or 0.000000001 to the result and typecast it to int. For example: (int)(pow(4, 2)+1e-9) will give the correct answer(16, in the above given example), irrespective of the compiler of your particular device.

Example:

In this last example of the pow() function, we are going to see how the pow() function work with integers in C++:

// CPP program to illustrate the working with integers in the  power function

#include <bits/stdc++.h>

using namespace std;

int main()

{

    int a;

    // Using typecasting for

    // integer result in pow() fuction

    a = (int)(pow(4, 2) + 0.5);

    cout << a;

    return 0;

}

Output:

16

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Conclusion

In this article on  C++ power, we have illustrated almost all the ways by which we can implement the power function, i.e., pov() in C++, with proper examples. We have provided a brief overview of syntax, parameters and programs in C++ programming language. After the overview, we have also illustrated the Working of pow() function with integers in C++ with an example for a better understanding of the topic. We hope that this article gave you a thorough knowledge of  C++ power and how we can use it in our software development projects.

To know more about the C++ power, you can enroll in the Post-Graduate Program In Full-Stack Web Development offered by Simplilearn in collaboration with Caltech CTME. This Web Development course is a descriptive online bootcamp that includes 25 projects, a capstone project, and interactive online classes. In addition to the  C++ power and other related concepts, the course also details everything you need to become a full-stack technologist and accelerate your career as a software developer.

Simplilearn also offers free online skill-up courses in several domains, from data science and business analytics to software development, AI, and machine learning. You can take up any of these free courses to upgrade your skills and advance your career.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 16 Dec, 2024

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 18 Dec, 2024

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 18 Dec, 2024

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449