Java Program to find Generic Root of a Number

Write a Java Program to find Generic Root of a Number with example. The formula behind this Java generic root calculation is nothing but the calculating sum of all digits in a number until we get a single-digit output (less than 10). For example, Generic Root of 98765 = 9 + 8 + 7 + 6 + 5 = 35 = 8.

Java Program to find Generic Root of a Number Example 1

This program allows the user to enter any positive integer value. Then this Java program calculates the Generic Root of that given number using While Loop, For Loop, and If Else Statement.

// Java Program to find Generic Root of a Number
import java.util.Scanner;

public class GenericRoot1 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Number, Sum, Reminder;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Integer Value : ");
		Number = sc.nextInt();	
		
		while(Number >= 10)
		{
			for (Sum=0; Number > 0; Number= Number/10)
		    {
				Reminder = Number % 10;
				Sum=Sum + Reminder;  
		    }
		    if(Sum >= 10)
		    {
		    	Number = Sum;
		    }
		    else
		    {
		    	System.out.println("\n The Generic Root of a Given Number = " + Sum);
		    }
		}
	}
}
Java Program to find Generic Root of a Number 1

User entered value for this Java Program to find Generic Root of a Number : Number  = 236

While Loop – First Iteration: while (Number >= 10)
Condition is True. So, it enter into the For Loop

For Loop First Iteration: for(Sum=0; Number > 0; Number= Number/10)
=> (Sum=0; 234 > 0; Number = 234/10) – Condition is True.
Reminder = Number % 10
Reminder = 234 % 10 = 4
Sum = 0 + 4 = 4

Next, the Number updated by For Loop.
Number = Number/10 => 234/10
Number = 23

Second Iteration: for(Sum=0; 23 > 0; Number = 23/10)
Condition is True.
Reminder = 23 % 10 = 3
Sum = 4 + 3 = 7

Number = 23/10
Number = 2

Third Iteration: for(Sum=0; 2 > 0; Number = 2/10)
Condition is True.
Reminder = 2 % 10 = 2
Sum = 7 + 2 = 9

Number = 2/10
Number = 0.2

Fourth Iteration: for(Sum=0; 0.2 > 0; Number = 0.2/10)
Condition is False.

Next, the Java compiler enters into If Else Statement

if(Sum >= 10) – Condition is False. So, it executes the statement inside the Else block.

Java Program to find Generic Root of a Number Example 2

This Java program to calculate the generic root of a number is the same as the above example. But we separated the logic and placed it in a separate method.

// Java Program to find Generic Root of a Number
import java.util.Scanner;

public class GenericRoot2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Number;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Integer Value : ");
		Number = sc.nextInt();	
		
		GenericRoot(Number);
	}
	
	public static void GenericRoot(int Number)
	{
		int Sum, Reminder;
		
		while(Number >= 10)
		{
			for (Sum=0; Number > 0; Number= Number/10)
		    {
				Reminder = Number % 10;
				Sum=Sum + Reminder;  
		    }
		    if(Sum >= 10)
		    {
		    	Number = Sum;
		    }
		    else
		    {
		    	System.out.println("\n The Generic Root of a Given Number = " + Sum);
		    }
		}
	}
}

Java generic roots output

 Please Enter any Integer Value : 987586

 The Generic Root of a Given Number = 7