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
{- Alexa loves movies and maintains a list of negative and/or positive integer ratings for the...
{- Alexa loves movies and maintains a list of negative and/or positive integer ratings for the n movies in her collection. She's getting ready for a film festival and wants to choose some subsequence of movies from her collection to bring such that the following conditions are satisfied: The collective sum of their ratings is maximal. She must go through her list in order and cannot skip more than one movie in a row. In other words, she cannot skip...
Ask user to input any 5-digit positive integer value. Calculate the sum of digits of that...
Ask user to input any 5-digit positive integer value. Calculate the sum of digits of that number. Assume that the number is positive, integer, 5-digit. Example: 29107 should calculate 2+9+1+0+7 and get 19 Hint: Use the % operator to extract a digit from a number. Use loop(s) Must be in Java
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...
IN JAVA In this problem, we will implement an nth root finder. Recall that the nth...
IN JAVA In this problem, we will implement an nth root finder. Recall that the nth root of x is the number when raised to the power n gives x. In particular, please fill in the method findNthRoot(int number, int n, int precision) within the Main class. The method should return a string representing the nth root of number, rounded to the nearest precision decimal places. If your answer is exact, you should still fill in the answer with decimal...
In C Programming Language: Given an integer N, find whether the sum of the digits calculated...
In C Programming Language: Given an integer N, find whether the sum of the digits calculated repeatedly till a single digit is obtained results in the sum of 1 or not. Display 1 if the sum of the digits of N can equal 1 else display 0. Example: For 199, Sum of digits = 1+9+9=19, 1+9=10, 1+0=1 so it should display 1 For 57, sum of digits = 5+7=12, 1+2=3 so it should display 0
IN JAVA please Given a sorted array and a target value, return the index if the...
IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...
Write the Java(Java 7 or Java 8) program for this problem:- Thanos, in his mission to...
Write the Java(Java 7 or Java 8) program for this problem:- Thanos, in his mission to restore the ecological balance in the universe, has reached planet earth. He considers a planet ecologically balanced if more than half of the people on the planet have the same Consumption Capacity There are N people on planet earth, each having Consumption Capacity C1, C2, ...CN and Strength S1, S2... Sn . Thanos will make earth ecological balanced by killing some people(Possibly None). To...
Problem: Our Armstrong number Please write code for C language So far we have worked on...
Problem: Our Armstrong number Please write code for C language So far we have worked on obtaining individual digits from 4 digits or 5 digit numbers. Then added them to find the sum of digits in various examples and assignments. However, the process of extracting individual digits is actually can be solved using a loop as you were doing a repetitive task by using mod operation and division operation. Now, we know how loops work and we can remove the...
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...
Java: All Hail Modulus Agustus! The modulus operator is used all the time. Realize that if...
Java: All Hail Modulus Agustus! The modulus operator is used all the time. Realize that if you “mod” any number by a number “n”, you’ll get back a number between 0 and n-1. For example, “modding” any number by 20 will always give you a number between 0-19. Your job is to design implement (source code) a program to sum the total of all digits in an input integer number between 0 and 1000, inclusive. Notice that you need to...