Question: 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
Heres a Java class implementing the described logic import javautilScanner public class NumberIt... View full answer
Get step-by-step solutions from verified subject matter experts
