diff --git a/Maths/digitSum.js b/Maths/digitSum.js new file mode 100644 index 000000000..3f08cf807 --- /dev/null +++ b/Maths/digitSum.js @@ -0,0 +1,18 @@ +// program to find sum of digits of a number + +// function which would calculate sum and return it +function digitSum (num) { + // sum will store sum of digits of a number + let sum = 0 + // while will run untill num become 0 + while (num) { + sum += (num % 10) + num = parseInt(num / 10) + } + + return sum +} + +// assigning number +const num = 12345 +console.log(digitSum(num))