Java Program to Calculate Generic Root of a Number

In the previous article, we have seen Java Program to Count Total Digits in a Number

In this article we will see how we can find generic root of a number by using Java programming language.

Java Program to Calculate Generic Root of a Number

Before jumping into the program directly, let’s know about generic root first.

Generic Root of A Number:

Generic root of a number represents finding sum of all the digits of a number till we get a single digit sum value.

Example:

Let the number is 65372

95979 = 9+5+9+7+9 = 39 = 3+9 =12 = 1+2 =

Let’s see different ways to find generic root of a number.

Method-1: Java Program to Calculate Generic Root of a Number By Using Static Input Value

Approach:

  • Declare a number.
  • Then take a while loop and continue it till the number is a one digit number.
  • Inside first while loop take another while loop and find the sum of each digit of a number.
  • Then out side inner while loop, check if the sum value is more than one digit number(means greater than equal to 10), then again that sum value becomes the number value and first while loop executes again.
  • If sum value is a single digit number then print that sum value which is generic root value of original number.

Program:

import java.util.*;

class Main
{
    public static void main(String arg[])    
    {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        // a number declared
        int num=98765;

        //continue while loop if 'num' is 2 digit value
        while(num >= 10)
        {
            int rem;
            int sum=0;
            
            //continue while loop till 'num' value becomes 0
            //find sum of each digit of a number
            while(num != 0)
            {
                rem = num % 10;
                sum=sum + rem; 
                num=num/10;
            }
            
            //if 'sum' value is more than two digit 
            if(sum >= 10)
            {
                //then assign 'sum' value to 'num'
                num = sum;
            }
            
            //else print 'sum' value 
            else
                System.out.println("Generic Root of the Given Number is: "+ sum);
        }
    }
}
Output:

Generic Root of the Given Number is: 8

Method-2: Java Program to Calculate Generic Root of a Number By Using User Input Value

Approach:

  • Take a number input.
  • Then take a while loop and continue it till the number is a one digit number.
  • Inside first while loop take another while loop and find the sum of each digit of a number.
  • Then out side inner while loop, check if the sum value is more than one digit number(means greater than equal to 10), then again that sum value becomes the number value and first while loop executes again.
  • If sum value is a single digit number then print that sum value which is generic root value of original number.

Program:

import java.util.*;

class Main
{
    public static void main(String arg[])    
    {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //integer variable 'num', 'sum' and 'rem' declared
        int num, rem;
        int sum;
        //taking a number input from user
        System.out.print("Enter a number: ");
        num=sc.nextInt();
        
        //continue while loop if 'num' is 2 digit value
        while(num >= 10)
        {
            sum=0;
            
            //continue while loop till 'num' value becomes 0
            //find sum of each digit of a number
            while(num != 0)
            {
                rem = num % 10;
                sum=sum + rem; 
                num=num/10;
            }
            
            //if 'sum' value is more than two digit 
            if(sum >= 10)
            {
                //then assign 'sum' value to 'num'
                num = sum;
            }
            
            //else print 'sum' value 
            else
                System.out.println("Generic Root of the Given Number is: "+ sum);
        }
    }
}
Output:

Enter a number: 965979
Generic Root of the Given Number is: 3

Method-3: Java Program to Calculate Generic Root of a Number By Using User Defined Method

Approach:

  • Take a number input.
  • Then call a user defined method by passing that number as parameter.
  • Inside the method take a while loop and continue it till the number is a one digit number.
  • Inside first while loop take another while loop and find the sum of each digit of a number.
  • Then out side inner while loop, check if the sum value is more than one digit number(means greater than equal to 10), then again that sum value becomes the number value and first while loop executes again.
  • If sum value is a single digit number then print that sum value which is generic root value of original number.

Program:

import java.util.*;

class Main
{
    public static void main(String arg[])    
    {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //taking a number input from user
        System.out.print("Enter a number: ");
        int num=sc.nextInt();

        //call user defined method findGenericRoot()
        findGenericRoot(num);
    }
    
    public static void findGenericRoot(int num)
    {
        //continue while loop if 'num' is 2 digit value
        while(num >= 10)
        {
            int rem;
            int sum=0;
            
            //continue while loop till 'num' value becomes 0
            //find sum of each digit of a number
            while(num != 0)
            {
                rem = num % 10;
                sum=sum + rem; 
                num=num/10;
            }
            
            //if 'sum' value is more than two digit 
            if(sum >= 10)
            {
                //then assign 'sum' value to 'num'
                num = sum;
            }
            
            //else print 'sum' value 
            else
                System.out.println("Generic Root of the Given Number is: "+ sum);
        }
    }
}
Output:

Enter a number: 123
Generic Root of the Given Number is: 6

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Related Java Programs: