Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Make a java class that determines the count of times it takes for a number to reach 1 using the following logic. Accepts a number

Make a java class that determines the count of times it takes for a number to reach 1 using the following logic.

Accepts a number from the user. Validate that number is in the range 100 to 999

if the number is outside this range, display an appropriate message and terminate the program.

If the number is valid:

  • If the number is even, reset the number to number/2
  • If the number is odd, reset the number to 3 * number + 1

Repeat as long as the number remains greater than 1. Keep a count of the number of iterations until the value of number is 1

Display the sentence

It takes iterations for the number to reach 1 .

Ask the user for another number.

Execute the above until the user inputs -1. When the input is -1, terminate the program by displaying "Goodbye!"

Example:

Input: 123

It takes 46 iterations for the number 123 to reach 1.

Input: 378

It takes 107 iterations for the number 378 to reach 1.

Input: -1

Invalid number...Goodbye!

File to be submitted: .java file

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Heres a Java class implementing the described logic import javautilScanner public class NumberIt... 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

Step: 3

blur-text-image

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

Java An Introduction To Problem Solving And Programming

Authors: Walter Savitch

8th Edition

0134462033, 978-0134462035

More Books

Students also viewed these Operating System questions

Question

6. What is a Bernoulli trial?

Answered: 1 week ago

Question

5. What is a cumulative probability distribution?

Answered: 1 week ago