(example: 214= 2+1+4 = 7)
JavaScript Code:
<HTML><body>
<script>
function find()
{
var sum=0;
var no=parseInt(frm.txt1.value);
while(no>0)
{
sum=sum+no%10;
no=Math.floor(no/10);
}
alert("Sum of digits "+sum);
}
</script>
<form name="frm">
Enter a Number:<input name="txt1" type="text" />
<input name="b1" onclick="find();" type="button" value="display" /></form>
</BODY>
</HTML>
This comment has been removed by the author.
ReplyDeleteprovide program for Sum of Digits provide single digit only ex:123456=>1+2+3+4+5+6 gives 21 but also add 2+1=>3 so final result we have to produce as sinle digit 3
ReplyDeleteprovide program for Sum of Digits provide single digit only ex:123456=>1+2+3+4+5+6 gives 21 but also add 2+1=>3 so final result we have to produce as sinle digit 3
ReplyDeleteWhy don't you add two while() in series.......Its a small program
DeleteSo use output first as input in second.
Instead of floor you can use parseInt
ReplyDeleteNope bro you have to use the Math.floor function because JavaScript only understands integers eg: 543 but it doesn't recognize decimal numbers such as 1.56 and it will give an error if u will not use the Math.floor function because Math.floor ignores the number after the decimal place for eg if you get an output 4.789 and when you'll use the Math.floor function then it will ignore 789 and it will only display 4. I hope I am helpful to anyone out there.
Deletewhile(n>=1)
ReplyDelete{
s=s+(n%10);
n=parseInt(n/10);
}
alert(s);
thanks brother
DeleteThis comment has been removed by the author.
ReplyDeleteWhy modules of 10 is taken
ReplyDeleteprovide program for Sum of Digits of a given number
ReplyDeleteEx : given number is 1234
i want output like 12 + 34 = 46