-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Repetitive addition of digits
- Loading branch information
1 parent
45e946a
commit db888ec
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//Repetitive addition of digits | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
class GFG { | ||
public static void main(String args[]) throws IOException { | ||
Scanner sc = new Scanner(System.in); | ||
int t = sc.nextInt(); | ||
|
||
while(t-- > 0) { | ||
int N = sc.nextInt(); | ||
Solution ob = new Solution(); | ||
int ans = ob.singleDigit(N); | ||
System.out.println(ans); | ||
System.out.println("~"); | ||
} | ||
} | ||
} | ||
|
||
class Solution { | ||
public int singleDigit(int n) { | ||
int sum = 0; | ||
|
||
while(true) { | ||
sum += n % 10; | ||
n /= 10; | ||
|
||
if((n == 0) && (sum >= 1 && sum <= 9)) { | ||
return sum; | ||
} | ||
else if(n == 0 && sum > 9) { | ||
n = sum; | ||
sum = 0; | ||
} | ||
} | ||
} | ||
} |