Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit dd4a4ec

Browse files
committed
Have implemented the program to add the digits to the to digit.
1 parent 5cacebc commit dd4a4ec

File tree

3 files changed

+60
-11
lines changed

3 files changed

+60
-11
lines changed

.idea/workspace.xml

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/com/raj/AddDigits.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Add Digits
3+
Link: https://leetcode.com/problems/add-digits/
4+
5+
Given an integer num, repeatedly add all its digits until the result has only one digit, and return it.
6+
7+
Example 1:
8+
Input: num = 38
9+
Output: 2
10+
Explanation: The process is
11+
38 --> 3 + 8 --> 11
12+
11 --> 1 + 1 --> 2
13+
Since 2 has only one digit, return it.
14+
15+
Example 2:
16+
Input: num = 0
17+
Output: 0
18+
19+
Constraints:
20+
0 <= num <= 231 - 1
21+
22+
Follow up: Could you do it without any loop/recursion in O(1) runtime?
23+
*/
24+
package com.raj;
25+
26+
public class AddDigits {
27+
public static void main(String[] args) {
28+
// Initialization.
29+
int num = 111;
30+
int sum = 0;
31+
32+
// Logic.
33+
sum = provideTotalToOneDigit(num);
34+
35+
// Display the result.
36+
System.out.println("The sum of the provided digit to the one digit is: " + sum);
37+
}
38+
39+
private static int provideTotalToOneDigit(int num) {
40+
int ans = 0;
41+
if (num < 10) {
42+
return num;
43+
}
44+
while (num > 0) {
45+
ans += num % 10;
46+
num /= 10;
47+
}
48+
return provideTotalToOneDigit(ans);
49+
}
50+
}

src/com/raj/SplitAStringInBalancedStrings.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public static void main(String[] args) {
4040
int LCounter = 0;
4141
int ans = 0;
4242

43-
4443
// Logic.
4544
for (int i = 0; i < s.length(); i++) {
4645
if (s.charAt(i) == 'R') {

0 commit comments

Comments
 (0)