Solution:
<html>
<body>
<script>
function sum() {
var sum = 0, i, str,num1;
num1 = number(document.getelementbyid("n").value);
str = num1.tostring();
sum = 0;
for (i = 0; i < str.length; i++) {
sum += parseint(str.charat(i), 10);
}
window.alert(sum);
}
</script>
</head>
<body>
enter a number : <input type="text" id="n"><br/>
<button onclick="sum()"> sum is </button>
<body>
</html>
0 Comments