mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
digitSumAdded (#315)
Co-authored-by: Aayushadh <ayushharwani2011@gmail.com>
This commit is contained in:
18
Maths/digitSum.js
Normal file
18
Maths/digitSum.js
Normal file
@ -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))
|
Reference in New Issue
Block a user