Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/7df55e736aaf4308d2cc4f935ec2568c to your computer and use it in GitHub Desktop.
Save thmain/7df55e736aaf4308d2cc4f935ec2568c to your computer and use it in GitHub Desktop.
public class SumOfDigitsTillSingleDigitRecursion {
static int findSum(int N){
if(N<10)
return N;
int sum = 0;
while(N>0){
sum += N%10;
N = N/10;
}
return findSum(sum);
}
public static void main(String[] args) {
int N = 12345;
int result = findSum(N);
System.out.println("Sum of digits in a number " + N + " till it become a single digit: " + result);
N = 999;
result = findSum(N);
System.out.println("Sum of digits in a number " + N + " till it become a single digit: " + result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment