Java Program to Calculate Power of a Number - GeeksforGeeks

31 Oct, 2019
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 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));    }}Output:8

// 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));    }}
8

Method 2: With the help of Loop// 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));    }}Output:8

// 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));    }}
8

Method 3: Using Math.pow() method// 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));    }}Output:8.0

// 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));    }}
8.0

Java
Mathematical
Recursion
Mathematical
Recursion
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
How to iterate any Map in Java
Interfaces in Java
Initialize an ArrayList in Java
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7