Prerequisite: Recursion in Java
In the previous article, we have discussed about Java Program to Find First Uppercase Letter in a String by Using Recursion
In this program we are going to see how to find digital roots of a number by using Recursion in Java programming language.
Java Program to Find Digital Roots of a Number by Using Recursion
Let’s see an example to understand it clearly.
Assume there is a number say N = 1234 Then the sum of the digits of N = 1+2+3+4 = 10 If the sum of digits > 9 then again find the sum of digits. Hence sum of digits of 10 is 1+0 = 1 So, the digital root of 1234 is 1
Now let’s see different ways to find digital roots of a number by using recursion.
Method-1: Java Program to Find Digital Roots of a Number By Using Static Input and Recursion
Approach:
- Declare and initialize an integer variable say ‘
n
’ - Call a user defined method
findDigitalRoot()
method and pass ‘n
’ as parameter to find the digital root of the given number . - Inside the user defined method we will find the sum of the digits of the number using a do while loop, if the sum of digits of the number exceeds 9 then we will call
findDigitalRoot()
method recursively. And finally we will return the sum value to themain()
method. - Now the value of the user defined method
findDigitalRoot()
method is stored in an integer variable say ‘x
’. - Print the value of digital root.
Program:
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { // declare and initialize an integer variable ‘n’ int n = 7869; //call findDigitalRoot() method to find the digital root int x = findDigitalRoot(n); // print the result System.out.println("The digital root of the number "+n+" is: "+x); } //findDigitalRoot method static int findDigitalRoot(long n) { //storing the original integer value in a temporary variable long temp = n; long sum = 0; //to find sum of digits of a number do { long remainder = temp%10; sum += remainder; temp = temp/10; } while(temp!=0); //if the sum of digits of the number is greater than 9 then again find the sum of the digits using recursion if (sum > 9) return findDigitalRoot(sum); // if the sum of digits of the number is less than 10 then return the value return (int)sum; } }
Output: The digital root of the number 7869 is: 3
Method-2: Java Program to Find Digital Roots of a Number By Using User Input and Recursion
Approach:
- Create a scanner class.
- Declare an integer variable say ‘
n
’. - Prompt the user to enter a number.
- Call a user defined method
findDigitalRoot()
method and pass ‘n
’ as parameter to find the digital root of the given number . - Inside the user defined method we will find the sum of the digits of the number using a do while loop, if the sum of digits of the number exceeds 9 then we will call
findDigitalRoot()
method recursively. And finally we will return the sum value to themain()
method. - Now the value of the user defined method
findDigitalRoot()
method is stored in an integer variable say ‘x
’. - Print the value of digital root.
Program:
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { // create a scanner class Scanner s = new Scanner(System.in); System.out.println("Enter the number:"); int n = s.nextInt(); //call findDigitalRoot() method to find the digital root int x = findDigitalRoot(n); // print the result System.out.println("The digital root of the number "+n+" is: "+x); } //findDigitalRoot method static int findDigitalRoot(long n) { //storing the original integer value in a temporary variable long temp = n; long sum = 0; //to find sum of digits of a number do { long remainder = temp%10; sum += remainder; temp = temp/10; } while(temp!=0); //if the sum of digits of the number is greater than 9 then again find the sum of the digits using recursion if (sum > 9) return findDigitalRoot(sum); // if the sum of digits of the number is less than 10 then return the value return (int)sum; } }
Output: Enter the number: 1234 The digital root of the number 1234 is: 1
Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.
Related Java Programs: