Write a Java Program to calculate the power using recursion

Java Program to calculate the power using recursion

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to calculate the power using recursion
Java Program to calculate the power using recursion

Write a Java Program to calculate the power using recursion

PROGRAM: Java Program to calculate the power using recursion

/*Java Program to calculate the power using recursion*/

class Power {
public static void main(String[] args) {

int base = 3, powerRaised = 4;
int result = power(base, powerRaised);

System.out.println(base + “^” + powerRaised + “=” + result);
}

public static int power(int base, int powerRaised) {
if (powerRaised != 0) {

// recursive call to power()
return (base * power(base, powerRaised – 1));
}
else {
return 1;
}
}
}

Leave a Reply

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