Question

Please code in Java and please implement constarints Digital Root and Iterations Given a non-negative integer,...

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

Homework Answers

Answer #1

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...

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
How to write a C++ program. Additive persistence is a property of the sum of the...
How to write a C++ program. Additive persistence is a property of the sum of the digits of an integer. The sum of the digits is found, and then the summation of digits is performed creating a new sum. This process repeats until a single integer digit is reached. Consider the following example: 1. The beginning integer is 1234 2. Sum its digits is 1+2+3+4 = 10 3. The integer is now 10 4. The sum of its digits is...
What would the pseudocode be for this? The code for this has already been answered. I...
What would the pseudocode be for this? The code for this has already been answered. I need the pseudocode. Credit card numbers typically consist of 13, 15, or 16 digits. For example, 4690 3582 1375 4657 is a hypothetical credit card number. The first digit designates the system. In (3.1.1), the first digit, 4, shows that the card would be a Visa card. The following digits specify other information such as the account number and bank number. (The precise meaning...
Design a module that can perform a binary-coded decimal (BCD) addition. You have two 4-bit BCD...
Design a module that can perform a binary-coded decimal (BCD) addition. You have two 4-bit BCD (decimal digits 0 to 9) inputs “a” and “b” and an 8-bit output “x” which represents a two digit BCD number. X [7:4] represents the upper BCD digits X [3:0] represents the lower BCD digits In the Verilog file, please code a BCD adder. It should follow the following format. module bcd_adder( a,b,x ); input wire [3:0] a; input wire [3:0] b; output reg...
4) Write a Java program using Conditions: Write a program where it will ask user to...
4) Write a Java program using Conditions: Write a program where it will ask user to enter a number and after that it will give you answer how many digits that number has. Steps: 1) Create Scanner object and prompt user to enter the number and declare variable integer for input 2) Use if else condition with && condition where you will specify the digits of numbers by writing yourself the digit number that should display: Example(num<100 && num>=1), and...
This question is broken into 3 parts, each of which builds on the functions developed in...
This question is broken into 3 parts, each of which builds on the functions developed in the previous part. Note that you can (and should) still answer parts (b) and (c) even if you cannot answer the preceding parts - just assume that the functions work as they should, and continue. Please explain the code as well Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The...
Java please. Given a sequence of unsorted numbers, determine how badly out of order they are....
Java please. Given a sequence of unsorted numbers, determine how badly out of order they are. Write a program that, for any given sequence of unique natural numbers, will compute the 'distance' between that original ordering and the same set of numbers sorted in ascending order. The distance should be computed by calculating how far displaced each number is in the original ordering from its correct location in the sorted list and summing those results. For instance, given the list...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q be a non-empty queue,...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
IN JAVA PLEASE How many times does the while loop execute for the given input values...
IN JAVA PLEASE How many times does the while loop execute for the given input values of -1 4 0 9 userNum=3;while (userNum>0){ //Do something //Get usernum firm input }
write a C program to find whether the given number of three digits is an integer...
write a C program to find whether the given number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is since 3**3 + 7**3 + 1**3 = 371. Output: Enter a number, or -999 to quit : 432 4**3 + 3**3 + 2**3 is not = 432 Enter a number, or -999 to quit : 371 3**3 + 7**3 + 1**3 is = 371...