Calculate power of x to y recursively

Saurabh Staff asked 2 years ago

How to calculate power of x to y recursively ?

1 Answers
Best Answer
Saurabh Staff answered 2 years ago

Below code snippet achieves the same. It’s written in javascript.

 
window.onload = init;

function init() {
    var x = 2,
        y = 5;
    alert("power of " + x + "^" + y + " is : " + pow(x, y));
}


function pow(x, y) {
    if (y === 0) {
        return 1;
    } else {
        return x * pow(x, y - 1);
    }
}