Java Program to Implement the pow() Function – Sanfoundry

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.

  1.  
  2. // Java Program to Implement the pow() Function
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  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
3.5
Enter the exponent (in integer)
5
3.5 raised to 5 is 525.218750
 
Case 2 (Simple Test Case - another example):
 
Enter the number
2
Enter the exponent (in integer)
-2
2.0 raised to -2 is 0.250000

Sanfoundry Global Education & Learning Series – Java Programs.

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!

Leave a Comment

Your email address will not be published. Required fields are marked *