Skip to content

Commit

Permalink
Create Repetitive addition of digits
Browse files Browse the repository at this point in the history
  • Loading branch information
dishathakurata authored Nov 28, 2024
1 parent 45e946a commit db888ec
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Repetitive addition of digits
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;
}
}
}
}

0 comments on commit db888ec

Please sign in to comment.