
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Power Using Math.pow in Java
In order to get the power of a number in Java, we use the java.lang.Math.pow() method. The Math.pow(double a, double b) method accepts two arguments of the double data type and returns a value of the first parameter raised to the power of the second argument.
Declaration - The java.lang.Math.pow() method is declared as follows −
public static double pow(double a, double b)
where a is the base and b is the power to which the base is raised.
Let us see a program where we find the power of a number using Math.pow() method
Example
import java.lang.Math; public class Example { public static void main(String[] args) { // declaring and initializing some double values double x = 5.0; double y = 3.0; // computing the powers System.out.println( x + " raised to the power of " + y + " is " + Math.pow(x,y)); System.out.println( y + " raised to the power of " + x + " is " + Math.pow(y,x)); } }
Output
5.0 raised to the power of 3.0 is 125.0 3.0 raised to the power of 5.0 is 243.0
Advertisements