Power Function in Java

This is the Java Program to Implement the pow() Function.

Problem Description

Given a value say x, and an exponent n, write a program to calculate x raised to the power n.

Problem Solution

The value x raised to the power n can be calculated, using a simple recursive algorithm. If the exponent is 1 return the variable x. Otherwise, recursively call the function on half of the exponent. If the exponent is even, multiply the result obtained from the recursive call by itself and in case if the exponent is odd also multiply x after multiplying the result.

Program/Source Code

Here is the source code of the Java Program to Implement the pow() Function. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

  2. // Java Program to Implement the pow() Function
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  7. public class Power {
  8.     // Function to calculate power of x raised to n
  9.     static double pow(double x, int n){
  10.         if(n<0){
  11.             return pow(1/x,-n);
  12.         }
  13.         if(n==1)
  14.             return x;
  15.         else if(n%2 == 0){
  16.             double y = pow(x,n/2);
  17.             return y*y;
  18.         }
  19.         double y = pow(x,n/2);
  20.         return y*y*x;
  21.     }
  22.     // Function to read user input
  23.     public static void main(String[] args) {
  24.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  25.         double x;
  26.         int n;
  27.         try {
  28.             System.out.println("Enter the number");
  29.             x = Double.parseDouble(br.readLine());
  30.             System.out.println("Enter the exponent (in integer)");
  31.             n = Integer.parseInt(br.readLine());
  32.         } catch (Exception e) {
  33.             System.out.println("An error occurred");
  34.             return;
  35.         }
  36.         double result = pow(x,n);
  37.         System.out.printf(x + " raised to " + n + " is %f", result);
  38.     }
  39. }
Program Explanation

1. In function pow(), firstly the exponent is checked. If it is negative, then the recursive call is made on the reciprocal of x, with negative of the exponent.
2. If the exponent is 1, then simply x is returned.
3. If the exponent is even, then a recursive call with half of the exponent is made and finally the result is squared.
4. If the exponent is odd, then a recursive call with half of the exponent is made and finally the result is squared and multiplied with x.


Time Complexity: O(log(n)) where n is the exponent.

Runtime Test Cases
Case 1 (Simple Test Case):
Enter the number
Enter the exponent (in integer)
3.5 raised to 5 is 525.218750
Case 2 (Simple Test Case - another example):
Enter the number
Enter the exponent (in integer)
2.0 raised to -2 is 0.250000

Sanfoundry Global Education & Learning Series – Java Programs.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.