Sum Of Digits In A Given Number

Think before you code.

Repeatedly add a number's digits until a single digit remains, using recursion.

easy

Sum Of Digits In A Given Number

Given an integer num, repeatedly add all its digits until the result has only one digit, and return it.

Example 1

  • Input : num = 529
  • Output : 7
  • Explanation : In first iteration the digits sum will be = 5 + 2 + 9 => 16. In second iteration the digits sum will be 1 + 6 => 7. Now single digit is remaining , so we return it.

Example 2

  • Input : num = 101
  • Output : 2
  • Explanation : In first iteration the digits sum will be = 1 + 0 + 1 => 2. Now single digit is remaining , so we return it.

Example 3

  • Input : num = 38
  • Output : 2

Constraints

  • 0 <= num <= 2³¹ - 1