Java program to find generic root.



Generic RootIt sum of digits of a number unit we don't get a single digit.
Example:
Generic root of 4563: 4+5+6+3 = 18 since 18 is two digit numbers so 1 + 8 = 9
So, generic root of 4563 = 9


package demo;
import java.util.Scanner;
public class GenericRoot
{
 public static void main(String[] args)
 {
  Scanner sc = new Scanner(System.in);
  long num, sum = 0, r;
  System.out.print("Enter a number:-");
  num = sc.nextLong();
  while (num > 10) 
  {
   sum = 0;
   while (num != 0) 
   {
    r = num % 10;
    num = num / 10;
    sum += r;
   }
   if (sum > 10) 
   {
    num = sum;
   } else
   {
    break;
   }
  }
  System.out.println("Sum of the digits in single digit is: " + sum);
 }
}

Output:
Enter a number:-5555
Sum of the digits in single digit is: 2
BUILD SUCCESSFUL (total time: 6 seconds)


No comments:

Post a Comment