Skip to content
Exercise · Operators and Expressions · lesson 7 of 8

Exercise: digit sum of a three-digit number

Write a function int digit_sum(int n) that returns the sum of the digits of the three-digit number n.

  • digit_sum(123) → 6
  • digit_sum(507) → 12
  • digit_sum(100) → 1

Constraints: n is an integer from 100 to 999 inclusive.

digit_sum.cpp — cpp
cpp · 4 lines
Ctrl+Enter run · Esc leave editor