Assistant — Three-digit Digit Sum
Go Programming Basics · Go
TASK
Problem
Input provides `n` as integers. Compute the formula `n/100 + n/10%10 + n%10` and print the result.
EXAMPLE
Example
123
6
LIMITS
Constraints
- Unless the statement says otherwise, integer inputs have absolute value at most 1,000,000. - Every divisor used in the task is non-zero. - Floating-point test values are chosen so the required arithmetic fits `float64`.
LEARN
Theory for this problem
+
`fmt.Scan`/`fmt.Fscan` parses whitespace-separated input into typed variables. The variable type controls how later arithmetic and comparisons behave.
Apply these rules directly to the task requirement: input provides `n` as integers. Compute the formula `n/100 + n/10%10 + n%10` and print the result.
NEED HELP?
Hints
+
['Read values in exactly the order stated by the condition.', 'Check the solution on a minimal, typical, and boundary input.', 'Use the sample input `123` to verify parsing and confirm that the program prints exactly `6` with no extra text.']
ANSWER
Solution
+
package main
import (
"fmt"
)
func main() {
var n int
fmt.Scan(&n)
hundreds := n / 100
tens := n / 10 % 10
ones := n % 10
result := hundreds + tens + ones
fmt.Println(result)
}