12. JavaScript Program to Check Middle digit of given 3 digit number is Equal to the Sum of Other Two digits or Not.
How does this program work?
- In this program you are going to learn about how to Check Middle digit of given 3 digit number is Equal to the Sum of Other Two digits or Not using JavaScript.
- Here First we claculate Last digit, First digit and their Sum.
- After that By using Mathematical Calculations like logarithm then compare Sum and middle digit using if condition and display result.
Here is the code
<html>
<head>
<title>JavaScript Program to Check middle digit is Equal to the Sum of Other Two digits or Not</title>
</head>
<body>
<table>
<tr>
<td> <input type="text" name="a" id="first" placeholder="Enter a number"/> </td>
</tr>
<tr>
<td> <button onclick="middle ()">Submit</button> </td>
</tr>
</table>
<div id ="num"></div>
</body>
<script type="text/javascript">
function middle()
{
var n,middle,last,sum,first;
n = parseInt(document.getElementById ("first").value);
last = n%10;
m = parseInt(n/10);
middle = m%10;
first = parseInt(n/100);
sum = last + first;
if(sum==middle)
{
document.getElementById("num").innerHTML= "Middle digit is equal to the sum of other two digits";
}
else
{
document.getElementById("num").innerHTML="Middle digit is not equal to the sum of other two digits";
}
}
</script>
</html>