Created
June 9, 2018 20:32
-
-
Save thmain/7df55e736aaf4308d2cc4f935ec2568c 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 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