Java program to find power of any number using Math.pow()

Quick links

Write a Java program to find power of any number using Math.pow(). In this article, we will learn about Math.pow() function. And see how to find power of any number in Java.

Example
Input

Enter base: 5
Enter exponent: 2

Output

5 ^ 2 = 25

Required knowledge

Arithmetic operators, Data types, Basic Input/Output, java.lang.Math

Program to find power of any number

/**
 * Java program to find power of any number.
 */
import java.util.Scanner;

public class Power {

   public static void main (String[] args) {

        Scanner in = new Scanner(System.in);

        /* Read input from user */
        System.out.print("Enter base: ");
        float base = in.nextFloat();
        
        System.out.print("Enter exponent: ");
        float exp  = in.nextFloat();
        
        /* Find power using Math.pow() */
        float pow = (float) Math.pow(base, exp);
    

        /* Print result */
        System.out.println(base + " ^ " + exp + " = " + pow);
	}
}
Enter base: 5
Enter exponent: 2
5.0 ^ 2.0 = 25.0

Happy coding 😉