PROBLEM 90
Hard

Sum of Four Digits

Programming Basics · JavaScript

</>
STATUS Not solved

TASK

Problem

#90

Given a positive four-digit integer, print the sum of its four digits.

EXAMPLE

Example

Input
2037
Output
12

LIMITS

Constraints

1000 ≤ n ≤ 9999
📖
LEARN Theory for this problem
+

### Sum of Four Digits

Integer division separates higher decimal/time units, while `%` returns the remainder. These two operations let a program isolate digits or split a total number of seconds/minutes into components.

💡
NEED HELP? Hints
+

['Decide which part comes from division and which part comes from the remainder.', 'Verify the accumulator initial value and both loop boundaries; this is where off-by-one errors most often appear.']

ANSWER Solution
+
const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trimEnd();
let n = Number(input);
let a = Math.floor((n) / (1000));
let b = (((Math.floor((n) / (100))) % (10)) + (10)) % (10);
let c = (((Math.floor((n) / (10))) % (10)) + (10)) % (10);
let d = (((n) % (10)) + (10)) % (10);
console.log(((a + b) + c) + d);