Explore 100's Of Free Video Tutorials

Keep Learning, Keep Coding & Keep Sharing

Find sum of digits in a given number

Notes:

Compute the Sum of Digits in a given Integer:
Program to find sum of digits of a number using while loop:

var num=12;
var lastDigit=0;
var sumOfAllDigits=0;
while(num!=0)
{
lastDigit= num % 10;
sumOfAllDigits = sumOfAllDigits + lastDigit;
num = parseInt(num/10);
}
document.write("Sum of all digits : ", sumOfAllDigits);

Interview Questions: