Javascript program to enter any number and calculate its square root
ADVERTISEMENTS
Javascript program to enter any number and calculate its square root. There are you will learn how to find the square root of any number in the javascript language.
Formula:
r = n^(1/2)
where:
r = root
n = number
There are two ways to implement these formulae:
- By using the Math.sqrt() function
- Second, by using Math.pow() function
1. By using the Math.sqrt() function
/* Javascript program to calculate the square root of a number */
<script type="text/javascript">
var n, r;
// n = number
// r = root
/* Calculate square root of number */
n = 25;
r = Math.sqrt(n);
document.write("Square root of " + n + " = " + r);
</script>
Output Screen:
Square root of 25 = 5
2. By using Math.pow() function
/* Javascript program to calculate the square root of a number */
<script type="text/javascript">
var n, r;
// n = number
// r = root
/* Calculate square root of number */
n = 25;
r = Math.pow(n, 0.5);
document.write("Square root of " + n + " = " + r);
</script>
Output Screen:
Square root of 25 = 5