965. 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.

Now Your Turn!

Pick the correct output for the given input

Input : num = 38

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 0 <= num <= 231 - 1
0
class Solution{
public:
int addDigits(int num){
//your code goes here
}
};
Test Case

Input:

Num