Have you ever wondered how mathematical power functions are implemented in programming languages like Java? In Java, the Math.pow() function is a powerful tool used to raise a number to a power. Let's understand more!

The** Power Function** is used to raise a number to the power of another number. It is a part of the Math class and can be used using **Math.pow(double a, double b) **where a is the base and b is the exponent. The method returns the result as a double.

*Also, read Power Function in C++*

**Table of Content**

## Syntax

double result = Math.pow(double base, double exponent);

*Where,*

**base**is the number to be raised.**exponent**is the power to which the base number is raised.

This method is part of the Math class in **Java** and is used to perform exponentiation.

**Example** **of** **Power Function in Java**

public class Main { public static void main(String[] args) { double base = 3.0; double exponent = 4.0; double result = Math.pow(base, exponent); System.out.println("Result: " + result); }}

**Output**

Result: 81.0

In the above example, **3.0 is raised to the power of 4.0, and the result 81.0** is printed to the console. Don’t forget to import the java.lang package to use the Math class, although in most cases, it’s imported by default since it’s a part of the core Java package.

## Let’s See a Few More Examples to Understand the Concept

### Basic Level

**Example 1**

**Calculating the square of a number**

public class Main { public static void main(String[] args) { double result = Math.pow(5.0, 2.0); System.out.println("Result: " + result); }}

**Output**

Result: 25.0

**Example 2**

**Calculating the cube of a number**

public class Main { public static void main(String[] args) { double result = Math.pow(21.0, 3.0); System.out.println("Result: " + result); }}

**Output**

Result: 9261.0

### Intermediate Level

**Example 3**

**Calculating the square root using** **Math.pow**

public class Main { public static void main(String[] args) { double result = Math.pow(64.0, 0.5); System.out.println("Result: " + result); }}

**Output**

Result: 8.0

**Example 4**

**Compound Interest Calculation**

public class Main { public static void main(String[] args) { double principal = 1000.0; double rate = 7.0; double time = 4.0;
double compoundInterest = principal * Math.pow((1 + rate/100), time); System.out.println("Compound Interest: " + compoundInterest); }}

**Output**

Compound Interest: 1310.7960100000003

*This output indicates that the compound interest accrued over 4 years with a 7% interest rate on a principal of 1000 units of currency is 310.7961 units of currency (with the total amount being 1310.7961 units of currency).*

### Miscellaneous Level

**Example 5**

**Population Growth Calculation (Exponential Growth)**

In this example, we will calculate the projected population of a city after a certain number of years, given an annual growth rate. The formula for exponential growth is:

*P = P*_{0}.e^{(r.t)}

*P = P*

_{0}.e^{(r.t)}**Where:**

**P**= final population**P**_{0}_{}= initial population**r**= annual growth rate (as a decimal)**t**= time in years**e**= base of the natural logarithm (approximately 2.71828)

public class Main { public static void main(String[] args) { double initialPopulation = 500000.0; double growthRate = 0.02; double timeInYears = 10.0;
double finalPopulation = initialPopulation * Math.pow(Math.E, growthRate * timeInYears); System.out.println("Projected Population: " + finalPopulation); }}

**Output**

Projected Population: 610701.379080085

This example showcases a real-world application of the Math.pow() method, where it’s used to calculate exponential growth, a concept frequently seen in biology (population growth), finance (compound interest), and many other fields.

**Conclusion**

The **Power Function in Java, Math.pow() **allows developers to compute exponential values with ease. Throughout this blog, we’ve demonstrated its utility in various scenarios, from simple numeric operations to complex calculations involving compound interest and exponential growth.

Download this article as PDF to read offline

Download as PDF**About the Author**

I'm Esha Gupta, a B.Tech graduate in Computer Science & Engineering, specializing in Front End Web Dev. I've interned at GeeksforGeeks & Coding Minutes, fueling my passion for crafting appealing and functional digit... Read Full Bio

## Comments