Created
June 9, 2018 20:34
-
-
Save thmain/6d3bfd19b332b13ba4eee5210184ac0c to your computer and use it in GitHub Desktop.
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
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