JavaScript Program to find the sum of digits of a number

In this program, You will learn how to find the sum of digits of a number in JavaScript.


while (Condition) {
  Statement
  Increment/Decrement
}

JavaScript Program to find the sum of digits of a number

Example: How to find the sum of digits of a number in JavaScript

let num = parseInt(prompt("Enter a number:"));
let sum = 0
while (num > 0) {
  r = num % 10;
  sum = sum + r;
  num = parseInt(num / 10);
}

console.log("Sum of digits of a number:" + sum);

Output:

Enter a number:> 3452
Sum of digits of a number:14