Recursive function for X to the power Y


public class Power
{
    public static double power(double base, double basePow)
    {
        
        if(basePow==0)
        {
            return 1;
        }
        else if(basePow==1)
        {
            return base;
        }
        else if(basePow>1)
        {
            return base*power(base,basePow-1);
        }
        else
        {

              return 1/power(base, -1 * basePow);
        }
       
       
    }
    
}

//main class

import java.util.Scanner;

public class Main {

    public static void main(String[] args)
    {
      Scanner input = new Scanner(System.in);

      System.out.print("Enter the base number: ");
      double base = input.nextInt();

      System.out.print("Enter the base power: ");
      double basePow = input.nextInt();

      
      Power access = new Power();
      
      System.out.print(base + " to the power of " + basePow + " is: " + access.power(base, basePow));

    }

}

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|