|
| 1 | +/** Author : Suraj Kumar Modi |
| 2 | + * https://github.com/skmodi649 |
| 3 | + */ |
| 4 | + |
| 5 | + |
| 6 | +/** You are given a number n. You need to find the digital root of n. |
| 7 | + * DigitalRoot of a number is the recursive sum of its digits until we get a single digit number. |
| 8 | + * |
| 9 | + * Test Case 1: |
| 10 | + * Input: |
| 11 | + * n = 1 |
| 12 | + * Output: 1 |
| 13 | + * Explanation: Digital root of 1 is 1 |
| 14 | + * |
| 15 | + * Test Case 2: |
| 16 | + * Input: |
| 17 | + * n = 99999 |
| 18 | + * Output: 9 |
| 19 | + * Explanation: Sum of digits of 99999 is 45 |
| 20 | + * which is not a single digit number, hence |
| 21 | + * sum of digit of 45 is 9 which is a single |
| 22 | + * digit number. |
| 23 | + */ |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +/** Algorithm : |
| 28 | + * Step 1 : Define a method digitalRoot(int n) |
| 29 | + * Step 2 : Define another method single(int n) |
| 30 | + * Step 3 : digitalRoot(int n) method takes output of single(int n) as input |
| 31 | + if(single(int n) <= 9) |
| 32 | + return single(n) |
| 33 | + else |
| 34 | + return digitalRoot(single(n)) |
| 35 | + * Step 4 : single(int n) calculates the sum of digits of number n recursively |
| 36 | + if(n<=9) |
| 37 | + return n; |
| 38 | + else |
| 39 | + return (n%10) + (n/10) |
| 40 | + * Step 5 : In main method simply take n as input and then call digitalRoot(int n) function and print the result |
| 41 | + */ |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +import java.util.*; |
| 50 | +import java.io.*; |
| 51 | +import java.lang.*; |
| 52 | + |
| 53 | +class DigitalRoot |
| 54 | +{ |
| 55 | + |
| 56 | + public static int digitalRoot(int n) |
| 57 | + { |
| 58 | + if(single(n) <= 9) // If n is already single digit than simply call single method and return the value |
| 59 | + return single(n); |
| 60 | + else |
| 61 | + return digitalRoot(single(n)); |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + // This function is used for finding the sum of digits of number |
| 67 | + public static int single(int n) |
| 68 | + { |
| 69 | + if(n<=9) // if n becomes less than 10 than return n |
| 70 | + return n; |
| 71 | + else |
| 72 | + return (n % 10) + single(n / 10); // n % 10 for extracting digits one by one |
| 73 | + } // n / 10 is the number obtainded after removing the digit one by one |
| 74 | + // Sum of digits is stored in the Stack memory and then finally returned |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + public static void main(String[] args) |
| 79 | + { |
| 80 | + Scanner sc = new Scanner(System.in); |
| 81 | + System.out.println("Enter the number : "); |
| 82 | + int n = sc.nextInt(); // Taking a number as input from the user |
| 83 | + System.out.println("Digital Root : "+digitalRoot(n)); // Printing the value returned by digitalRoot() method |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +/** |
| 89 | + * Time Complexity : O((Number of Digits)^2) |
| 90 | + * Auxiliary Space Complexity : O(Number of Digits) |
| 91 | + * Constraints : 1 <= n <= 10^7 |
| 92 | + */ |
0 commit comments