Question
form a number chain in java language: Arranging its digits in descending order. Arranging its digits in ascending order. Subtracting the number obtained in (2)
form a number chain in java language:
Arranging its digits in descending order.
Arranging its digits in ascending order.
Subtracting the number obtained in (2) from the number obtained (1) to form a new number.
Repeating these steps until the new number matches the result from the previous calculation.
For example given an original number of 1234, the process would be: 4321 - 1234 = 3087 8730 - 378 = 8352 8532 - 2358 = 6174 7641 - 1467 = 6174 Chain length = 4
The number of distinct numbers in the chain is the length of the chain. You are to write a program that reads numbers and displays the length of the chain for each number read. Some numbers may not result in a new number that matches the result from the previous calculation.
For example, the number 94723 can go through more than 1000 iterations and still not reach the ending point.
In cases like this, stop after 1000 iterations and indicate the chain length is greater than 1000 as shown in the sample output below.
The input will consist of a text file named numberchain.txt. This file will contain a sequence of positive integer numbers each on its own line.
Although the sample file below has 5 numbers (lines) do NOT assume all test files will have 5 numbers (lines) of input. The files used by the judges may have more or fewer numbers.
Input (numbertest.txt) | Output |
---|---|
1234 | original number was 1234 and chain length = 4 |
123456789 | original number was 123456789 and chain length = 2 |
84723 | original number was 84723 and chain length > 1000 |
8080 | original number was 8080 and chain length = 7 |
9235 | original number was 9235 and chain length = 6 |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started