New Tutorials:
CLOSE
pow()  Guide  Library Functions
Technology    Programming

# Power function pow() in Different Languages with Code Examples

JUNE 19, 2019   by vineelpynam

Hi there! In this post we are going to see usage of `pow()` library function in 6 different languages namely C, C++, Java, Python, PHP and JavaScript. In mathematics you must have solved problems like "find the value of `5^3`?" for which the correct answer is 5x5x5 = 125. So easy, right.

In this article we will learn how to solve similar problems programatically using the `pow()` function. Before we jump onto examples, let's learn a few key points about the `pow()` function:

• The `pow()` function is used for finding power of a given number.

• `pow()` function always returns a double value.

• `pow()` function takes two arguments, for example, `pow(X, Y)` returns `X` raised to the power if `Y` (i.e `X^Y`)

X^Y (in mathematics) = pow(X, Y) (in programming)

The first argument is the base value and the second argument is the power value.

Lets take an example and see how it works:

Example 1: pow(2, 5) = 2^5 = 32 ( 2 x 2 x 2 x 2 x 2 )

Example 2: pow(5, 2) = 5^2 = 25 ( 5 x 5 )

## 1. `pow()` Library function in C Language

In C language `pow()` is a library function. The datatype of arguments that you pass to this function should be a `double`(datatype). If any other datatype is used like (`int`), you will get an error or null output.

To use `pow()` function in C language, we have to include `<math.h>` library. Below we have a simple code example:

``````#include <stdio.h>
#include <math.h>

int main () {
double base=2, power=5, output;

output = pow(base, power);

printf("Value %lf^%lf = %lf\n", base, power, output);

return(0);
}``````

Output:

```2^5 = 32
```

## 2. `pow()` Library function in C++ Language

In C++ language too `pow()` is a library function and accepts only `double `datatype values as arguments.

In C++ to use `pow()` function, we have to include `<cmath>` library. Below we have a simple code example:

``````#include <iostream>
#include <cmath>

using namespace std;```

int main ()
{
double base=5, power = 2, output;

output = pow(base, power);

cout << base << "^" << power << " = " << output;

return 0;
}```

Output:

```5 ^ 2 = 25
```

## 3. `pow()` Library function in Java Language

In Java `pow()` is an inbuilt function. Here, the `pow()` is used as ` Math.pow()`

``````public class StudyTonight{

public static void main(String[] args) {

double base=2.5,power=5.5, output;

output = Math.pow(base, power);

System.out.println( base + "^" + power + "=" + output );

}
}``````

Output:

`2.5 ^ 5.5 = 154.408088`

In the languages covered below i.e. Python, PHP and JavaScript, `pow()` is an inbuilt function and one can use` pow()` function directly without inluding a library like in C and C++.

And also you do not need to declare datatype for the variables.

## 4. `pow()` function in Python

Here is a simple code example,

``````base = 10
power = 3
# calculating power
output = pow(base, power)
print(base, "^", power, "=", output)``````

Output:

```10 ^ 3 = 1000
```

## 5. `pow()` function in PHP

Here is a simple code example,

``````<?php

\$base = 5;
\$power = -5;
\$output = pow(\$base, \$power);
echo (\$base . " ^ " . \$power . " = " . \$output);

?>``````

Output:

```5 ^ -5 = 0.00032
```

## 6. `pow()` function in Javascript

In Javascript `pow()` is used as `Math.pow()`

``````<script type="text/javascript">

var base = 20;
var power = -2;
var output = Math.pow(base, power);
document.write( base + "^" + power + "=" + output );

</script>``````

Ouput:

`20 ^ -2 = 0.0025`

Pheww! This is it. I hope that you enjoyed the post and learned about implementation of `pow()` library function in different languages. If you feel that this post is useful, please share it with your friends and colleagues.

Thanks for reading it till the end.