27. JavaScript Program to find the Sum and Product of individual digits of a given number.
How does this program work?
- This program is used to find the sum and product of individual digits of a given number using JavaScript.
- We can followed by the below code we can get the Output easily.
Here is the code
<html>
<head>
<title>JavaScript program to find the Sum and Product of individual digits of given number</title>
</head>
<body>
<table>
<tr>
<td> <input type="text" name="a" id="first" placeholder="Enter a number"> </td>
</tr>
<tr>
<td> <button onclick="sum_product ()" >Submit</button> </td>
</tr>
</table>
<div id="num"></div>
<div id="num1"></div>
</body>
<script type="text/javascript">
function sum_product()
{
var r,n,sum = 0,product = 1;
n = parseInt(document.getElementById ("first").value);
while(n > 0)
{
r = n %10;
sum = sum+r;
product = product*r;
n = parseInt(n/10);
}
document.getElementById ("num").innerHTML= sum + " is a sum of digit";
document.getElementById ("num1").innerHTML= product + " is a product of digit";
}
</script>
</html>