Please code in Java and please implement constarints Digital Root and Iterations Given a non-negative integer, print out its digital root and the number of iterations required to reach it. The digital root is the single digit number obtained by an iterative process of finding the sum of digits. In the next iteration, the sum of the digits in the previous iteration is computed, and the process repeated until a single digit value is obtained. Input Format The first line of input consists of an integer t denoting the number of test cases. Then t lines follow each consisting of an integer n. Output Format For each test case, output the digital root and the number of iterations separated by a space. Constraints 1 <= t <= 10^4 0 <= n <= 10^12 Sample Input 4 1234567890 199 100 8 Sample Output 9 2 9 3 1 1 8 0 Explanation For 1234567890 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 = 45 4 + 5 = 9 It took two iterations. Answer is 9 2. Please code in java and use bigInteger
source code:
import
java.math.BigInteger;
import java.util.Scanner;
public class BIg {
public static void main(String...args)
{
Scanner in= new
Scanner(System.in);
System.out.print("Enter input
format : "); //user input
String s = in.nextLine();
String[] s1=s.split(" ");
//creating array of string using user input (split function)
int
t=Integer.parseInt(s1[0]);
if(t>=1 &&t<=10000)
// checking test case t value
{
for(int
i=1;i<=t;i++)
{ int count=0;
//number of times sum perform
int m=Integer.parseInt(s1[i]);
if(m>=0 &&
m<=1000000000000.0)
{
while(s1[i].length()>1) //while execute until string length
==1
{
s1[i]=sum(s1[i]);
count++;
}
System.out.print(s1[i]+" "+ count+" ");
//printing values
}
}
}
}
private static
String sum(String s) { //digit sum method
BigInteger n = new BigInteger("0"); //using big
integers
BigInteger n1 = new BigInteger(s);
BigInteger ten = new BigInteger("10");
int s1=s.length();
while(s1>0)
{
BigInteger rem = n1.remainder(ten);
//remainder
n=n.add(rem); //adding
remainder
n1=n1.divide(ten);
s1--;
}
return n.toString();
}
}
if you have any doubts ask me...
Get Answers For Free
Most questions answered within 1 hours.