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...
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...
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 [(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...
This is in java and you are not allowed to use Java API classes for queues,...
This is in java and you are not allowed to use Java API classes for queues, stacks, arrays, arraylists and linkedlists. You have to write your own implementations for them. You should construct a BST by inserting node values starting with a null tree. You can re-use the code for the insert method given in the sample code from the textbook. -insert method is provided below Your code should have a menu driven user interface at the command line with...
in java A way to measure the amount of energy spent during an exercise is to...
in java A way to measure the amount of energy spent during an exercise is to use metabolic equivalents (MET). The formula to use is: METs x 3.5 x (weight in kg ) / 200 = calories burned per minute . For example, say you weigh 160 pounds (approximately 73 kg) and you play singles tennis, which has a MET value of 8. The formula would work as follows: 8 x 3.5 x 73 / 200 = 10.2 calories per...
APPLIED STATISTICS 2 USE R CODE ! SHOW R CODE! Write a function to calculate the...
APPLIED STATISTICS 2 USE R CODE ! SHOW R CODE! Write a function to calculate the sum of cubes from 1 to n, but skip the multiple of 5. Say, if n=10, the result is 1^3+2^3+3^3+4^3+6^3+7^3+8^3+9^3. The input is the value of n, the output is the summation. (you need if statement to check whether a number is a multiple of 5.In R, a%%b is the remainder for a divided by b. So, we have 10%%5=0) APPLIED STATISTICS 2 USE...
(Use Java ) please write comment on every line I need to understand the code Problem...
(Use Java ) please write comment on every line I need to understand the code Problem Description: Write a program that prompts the user to enter a point (x, y) and checks whether the point is within the rectangle centered at (0, 0) with width 10 and height 5. For example, (2, 2) is inside the rectangle and (6, 4) is outside the rectangle, as shown in the Figure. (Hint: A point is in the rectangle if its horizontal distance...
write a code in python Write the following functions below based on their comments. Note Pass...
write a code in python Write the following functions below based on their comments. Note Pass is a key word you can use to have a function the does not do anything. You are only allowed to use what was discussed in the lectures, labs and assignments, and there is no need to import any libraries. #!/usr/bin/python3 #(1 Mark) This function will take in a string of digits and check to see if all the digits in the string are...