Introduction
While writing a program, we often encounter scenarios where we have to perform certain mathematical computations, which may require a lot of time. Therefore, many inbuilt functions are used in programming languages for performing mathematical calculations.
Do you know that math pow is one such inbuilt function? If not, then don't worry; in this article, we will discuss about math pow and its variations in different programming languages. We will also see some examples to understand the concept.
Also see, Swap Function in Java
About Math pow Function
The ‘math pow’ is an inbuilt function in programming languages that helps users calculate a number raised to a specific power.
For example, 2^3, 2 is a base, while 3 is the exponent.
Manually this can be calculated by multiplying 2 three times, but by using math pow, we can do this efficiently without performing complex computations.
Syntax
While different languages have the syntax of math pow function, a basic syntax can be referred to as:
pow_function(base, exponent)

In the above syntax, pow_function is the name of the function according to the specific programming language.

The ‘base’ is the number we want to raise to a specific power.
 The ‘Exponent’ is the power to which a base is to be raised.
Math pow Function in Javascript
Let's see the pow function in Javascript.
In javascript, the Math.pow() method returns the value of a variable ‘a’ raised to the power ‘b’ (a^b).
The time complexity of the pow() function is O(logN) or O(1), depending upon different browsers. Math.pow() is an ECMAScript (ES1) feature supported by all browsers.
Syntax
Math.pow(base, exponent)
See the below example for better understanding.
Code
let NinjaResult=Math.pow(2,3)
console.log("Result of 2 raised to power 3 is ",NinjaResult)
Output
In the example above, 2 is the base, and 3 is the exponent.
Math pow Function in Java
In Java, the 'Math. pow()' function is used to return the first parameter's value raised to the power of the second parameter. If the second parameter is not a number, we get NaN.
The time complexity of the Math.pow() function is O(logN).
Syntax
Math.pow(base,exponent)
See the below example for better understanding.
Code
class NinjaPowExample
{
public static void main(String[] args)
{
double NinjaVar1 = 3;
double NinjaVar2 = 2;
System.out.println("3 raised to power 2 is "+Math.pow(NinjaVar1 , NinjaVar2 ));
}
}
Output
Math pow Function in Python
In Python, the pow() function returns the first parameter's value raised to the power of the second parameter.
The time complexity of the Math.pow() function is O(logN).
Syntax
pow(a,b)
If the user provides the third variable in Python, the modulo operation is applied between the exponential result and the third variable.
For example,
pow(a,b,c) // results in pow(a,b)%c or a^b %c
The third parameter is optional. See the below example for better understanding.
Code
NinjaResult = pow(5, 3, 4)
print("5 raised to power 3 and modulated by 4 is:",NinjaResult)
Output
Math pow Function in C++
In CPP, the pow() function returns the first parameter's value raised to the power of the second parameter.
The time complexity of the pow() function is O(logN) or O(1), depending upon compilers or libraries.
For using the pow() function in cpp, we must include <cmath> header file in our program. This library also includes functions such as sqrt(), sin(), cos(), etc.
Syntax
pow(a,b)
In the syntax above, ‘a’ is the first parameter, and ‘b’ is the second parameter. See the below example for better understanding.
Code
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout <<"3 raised to power 4 is: "<<pow(3, 4);
return 0;
}