Power raise using Recursion

//Author : S.Mahbub - Uz - Zaman 
//Math.pow using Recursion


import java.util.*;
public class MathPow {  
    //We konw 5^5=5*5^4 -> 5^4=5*5^3 -> 5^3=5*5^2 -> 5^2=5*5^1 -> 5^1=5*5^0 -> 5^0=1 
    //something to the power 0 means 1, So this our base case
    public static double Pow (double base,double pow) {
        if(pow == 0) // Base Case #Recursive function breaking condition#
            return 1;
        else
            return base * Pow(base, pow - 1);
    }
    
    public static void main (String args []) {
        Scanner sc = new Scanner (System.in);
        double base = sc.nextDouble();
        double pow = sc.nextDouble();
        System.out.println((int)Pow(base, pow));
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s