Java Program to Calculate Power of a Number Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a number N and a power P, the task is to find the exponent of this number raised to the given power, i.e. NP. Examples: Input: N = 5, P = 2 Output: 25 Input: N = 2, P = 5 Output: 32 Below are the various ways to find NP: Method 1: Using Recursion Java // Java program to find the power of a number // using Recursion class GFG { // Function to calculate N raised to the power P static int power(int N, int P) { if (P == 0) return 1; else return N * power(N, P - 1); } // Driver code public static void main(String[] args) { int N = 2; int P = 3; System.out.println(power(N, P)); } } Output8 Method 2: With the help of Loop Java // Java program to find the power of a number // with the help of loop class GFG { // Function to calculate N raised to the power P static int power(int N, int P) { int pow = 1; for (int i = 1; i <= P; i++) pow *= N; return pow; } // Driver code public static void main(String[] args) { int N = 2; int P = 3; System.out.println(power(N, P)); } } Method 3: Using Math.pow() method Java // Java program to find the power of a number // using Math.pow() method import java.lang.Math; class GFG { // Function to calculate N raised to the power P static double power(int N, int P) { return Math.pow(N, P); } // Driver code public static void main(String[] args) { int N = 2; int P = 3; System.out.println(power(N, P)); } } Output8.0 Method 4 : Efficient Approach In this approach I am going to calculate power of given base using bit manipulation with logarithmic time complexity Java /*java program to calculate of power of given base number */ public class GFG { public static void main(String[] args) { int base = 2; int power = 3; int ans = 1; while (power > 0){ if ((power&1)==1){ // if == 0 there is no point to calculate ans ans = ans * base; } base = base * base; // keep dividing power by 2 using right shift power = power >> 1; } System.out.println(ans); } } Output8 Time complexity : O(logb) Java Program to Calculate the Power of a Number Comment More infoAdvertise with us Next Article StrictMath pow() Method in Java C code_r Follow Improve Article Tags : Java Practice Tags : Java Similar Reads Java Program to find whether a no is power of two Given a positive integer, write a function to find if it is a power of two or not. Examples : Input : n = 4 Output : Yes 22 = 4 Input : n = 7 Output : No Input : n = 32 Output : Yes 25 = 32 1. A simple method for this is to simply take the log of the number on base 2 and if you get an integer then n 3 min read Java Guava | isPowerOfTwo() method IntMath Class The isPowerOfTwo() method of Guava's IntMath class is used to check if a number is power of two or not. It accepts the number to be checked as a parameter and return boolean value true or false based on whether the number is a power of 2 or not. Syntax : public static boolean isPowerOfTwo(int x) Par 2 min read Java Guava | isPowerOfTwo() method IntMath Class The isPowerOfTwo() method of Guava's IntMath class is used to check if a number is power of two or not. It accepts the number to be checked as a parameter and return boolean value true or false based on whether the number is a power of 2 or not. Syntax : public static boolean isPowerOfTwo(int x) Par 2 min read StrictMath pow() Method in Java The java.lang.StrictMath.pow() is an inbuilt method of StrictMath class used to find the power value i.e., the value of one argument raised to the power of another argument. Mathematically it refers to a^b . It gives rise to three special results: The method returns NaN when one argument is NaN and 2 min read Java Guava | pow(long b, int k) of LongMath Class with Examples The method pow(long b, int k) of Guava's LongMath Class returns b to the kth power. Even if the result overflows, it will be equal to BigInteger.valueOf(b).pow(k).longValue(). This implementation runs in O(log k) time. Syntax: public static long pow(long b, int k) Parameters: The method accepts two 2 min read Java Guava | pow(long b, int k) of LongMath Class with Examples The method pow(long b, int k) of Guava's LongMath Class returns b to the kth power. Even if the result overflows, it will be equal to BigInteger.valueOf(b).pow(k).longValue(). This implementation runs in O(log k) time. Syntax: public static long pow(long b, int k) Parameters: The method accepts two 2 min read Like