Kotlinlearncs.online LogoJava
Return to List

Solve: Sum of Digits

Created By: Justin Maier
/ Version: 2024.8.0

Given a nonnegative int variable n, calculate and print out the sum of the digits of n. For example, if n has the value 1234, since 1 + 2 + 3 + 4 = 10, your program should print 10.

Hint This one is tricky! You can use n % 10 to extract the digit in the ones place of n and use integer division / to remove the last digit from a number.

Related Lessons

Stuck? You may find these lessons helpful: