3. (D) Write a JavaScript program to accept a number from the user and display the sum of its digits

3. (D) Write a JavaScript program to accept a number from the user and display the sum of its digits.
<html>
<body>
<form>
Enter The No. To Find Sum of Digits:<input type="number" id="no"><br><br>
<input type="submit" value="Check" onclick="myfun()">
</form>
<div id="abc">
<textarea id="xyz"></textarea>
</div>
<script>
function myfun()
{
var n;
n=document.getElementById("no").value;
var R,S=0;
while(n!=0)
{
R=n%10;
S=S+R;
n=Math.floor(n/10);
}
alert("THE SUM IS : " + S);
}
</script>
</body>
</html>
3. (D) Write a JavaScript program to accept a number from the user and display the sum of its digits 3. (D) Write a JavaScript program to accept a number from the user and display the sum of its digits Reviewed by admin on December 24, 2019 Rating: 5

No comments:

Powered by Blogger.