Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using java language, in one class, write a program that accepts a positive integ

ID: 3623572 • Letter: U

Question

Using java language, in one class, write a program that accepts a positive integer input from the keyboard. This is the starting value for your sequence. Then your code should generate and print out, in a column, hailstone numbers until the value 1 is reached. After 1 has been reached, your program should print out: 1) the largest value appearing in the sequence; and 2) the length of the sequence you generated.

For example, here is the output for the sequence that begins with the seed value 21:

Welcome to DrJava.
> java Hail
Enter a positive hailstone starting value
21

21
64
32
16
8
4
2
1
start: 21
term count: 8
biggest: 64

Requirements: your program must be commented and clear.

Thank you.

Explanation / Answer

please rate - thanks

import java.util.*;
public class Hailstone
{
public static void main(String[] args)
{int num,length=1,max=0,start;
Scanner in=new Scanner(System.in);
System.out.print("Enter a positive hailstone starting value: ");
num=in.nextInt();
max=num;
start=num;
System.out.println();
do
{length++;
System.out.println(num);
if(num%2==0)
    num/=2;
else
    num=num*3+1;
if(num>max)
    max=num;
}while(num!=1);
System.out.println(num);
System.out.println("start: "+start);
System.out.println("term count: "+length);
System.out.println("biggest: "+max);

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote