Thursday, July 17, 2014

Recursive Java

package Recursive_lab;

import java.util.Scanner;
public class Power
{
    public static void main(String[] args)
    {
        int base;
        int exp;
        int answer;
        Scanner scan = new Scanner(System.in);
        System.out.print("Base number ");
        base = scan.nextInt();
        System.out.print("Raised to the power of? ");
        exp = scan.nextInt();
        answer = pow (base,exp);
        System.out.println(base + " raised to the power of " + exp + " is " + answer);
    }

    public static int pow(int base, int exp)
    {
        int pow;
        if (exp == 0)
            return 1;
        return base * pow(base, exp-1);
}
}

No comments:

Post a Comment