Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript Program for Sum of the digits of a given number

<script>
 
// Javascript program to compute sum of digits in
// number.
 
/* Function to get sum of digits */
function getSum(n)
{
    var sum = 0;
    while (n != 0) {
        sum = sum + n % 10;
        n = parseInt(n / 10);
    }
    return sum;
}
 
// Driver code
var n = 687;
document.write(getSum(n));
 
</script>
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #javascript #Program #Sum #digits #number
ADD COMMENT
Topic
Name
3+3 =