Skip to content

Instantly share code, notes, and snippets.

@thmain
Created June 9, 2018 20:34
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/6d3bfd19b332b13ba4eee5210184ac0c to your computer and use it in GitHub Desktop.
Save thmain/6d3bfd19b332b13ba4eee5210184ac0c to your computer and use it in GitHub Desktop.
public class SumOfDigitsTillSingleDigitTricky {
static int findSum(int N){
if(N==0)
return N;
int remainder = N%9;
if(remainder==0)
return 9;
else
return remainder;
}
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);
N = 111;
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