Javascript program to find the power of any number x^y
ADVERTISEMENTS
Javascript program to find the power of any number x^y. There are you will learn how to find the power of any number x^y in javascript language.
Formula:
r = b ^ y
where:
r = result
b = base value
y = exponent value
There are two ways to implement these formulae:
- By using the default method
- Second, by using Math.pow() function
1. By using the default method
/* Javascript program to find the power of any number */
<script type="text/javascript">
var b, e, r = 1, i = 1;
// b = base
// e = exponent
// r = result
// i = looping incremental
b = 5;
e = 4;
// finding power of base value by equiping exponent value
while(i <= e)
{
r *= b;
i++;
}
document.write("Result:: " + b + "^" + e + " = " + r )
</script>
Output Screen:
Result:: 5^4 = 625
2. By using Math.pow() function
/* Javascript program to find the power of any number by using Math.pow() function */
<script type="text/javascript">
var b, e, r;
// b = base
// e = exponent
// r = result
b = 8;
e = 4;
// finding power of base value by equiping exponent value
r = Math.pow(b, e);
document.write("Result:: " + b +"^" + e + " = " + r )
</script>
Output Screen:
Result:: 8^4 = 4096