-
Notifications
You must be signed in to change notification settings - Fork 5
/
AddDigits.java
39 lines (34 loc) · 928 Bytes
/
AddDigits.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
Problem Statement:
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
Problem Link:
Add Digits: https://leetcode.com/problems/add-digits/
Solution:
https://github.com/sunnypatel165/leetcode-again/blob/master/solutions/AddDigits.java
Author:
Sunny Patel
https://github.com/sunnypatel165
https://www.linkedin.com/in/sunnypatel165/
*/
class Solution {
public int addDigits(int num) {
if(num==0) return 0;
return (num%9 ==0 ? 9 : num%9);
//return addAllDigits(num);
}
public int addAllDigits(int num){
while(num != num%10){
num = sumOfDigits(num);
}
return num;
}
public int sumOfDigits(int num){
int sum=0;
while(num > 0){
sum+=num%10;
num/=10;
}
return sum;
}
}