Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Third International Conference Achids 2011 Daegu Korea April 2011 Proceedings Part 2 Lnai 6592

Authors: Ngoc Thanh Nguyen ,Chong-Gun Kim ,Adam Janiak

2011th Edition

3642200419, 978-3642200410

More Books

Students also viewed these Databases questions

Question

3. Identify the methods used within each of the three approaches.

Answered: 1 week ago