Recursive Function example
Some recursive functions example are given below:
Power of a Number
The first one recursive function example is Power of a number.Power of a number is the multiplying a number by itself .Power is expressed with a Base number and an Exponent. The exponent is a small number written above and to the right of the base number. It depicts how many times the base number is multiplied.
recursive function example |
<html>
<body>
<h2>JavaScript Math</h2>
<p>Math.pow(x,y) returns the value of x to the power of y:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.pow(4, 3);
</script>
</body>
</html>
JavaScript Math
Math.pow(x,y) returns the value of x to the power of y:
64
Read also for more examples of recursion: Click Here
Least Common Multiple(LCM) of 2 Numbers
The second one recursive function example is to find the LCM of two numbers.
The least common multiple(LCM) of a number is the smallest number that is the product of two or more numbers.
recursive function example |
// program to find the LCM of two integers
// take input
const num1 = prompt('Enter a first positive integer: ');
const num2 = prompt('Enter a second positive integer: ');
// higher number among number1 and number2 is stored in min
let min = (num1 > num2) ? num1 : num2;
// while loop
while (true) {
if (min % num1 == 0 && min % num2 == 0) {
console.log(`The LCM of ${num1} and ${num2} is ${min}`);
break;
}
min++;
}
Output:
Enter a first positive integer: 6 Enter a second positive integer: 8 The LCM of 6 and 8 is 24
The least common multiple(LCM) of a number is the smallest number that is the product of two or more numbers.
No comments:
Post a Comment