7.20 zyLab: Hailstone Sequence Given a positive integer n, the following rules w
ID: 3909571 • Letter: 7
Question
7.20 zyLab: Hailstone Sequence Given a positive integer n, the following rules will always create a sequence that ends in 1 but mathematicians have not been able to prove it. if n is even, divide it by two if n is odd, multiply it by 3 and add 1 (e.g. 3n +1) continue until n is 1 Complete the following method to print the Hailstone Sequence. Duplicate the sample output with ten numbers per line and each separated by a tab (t). System.out.print val + "t" Hailstone Sequence 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 421 AC7.20.1: zyLab: Hailstone Sequence LAB ACTIVITY 0/2 Main.java Load default template.. 1 import java.util.* 2 public class Main t 4 public void hailstone) 6 Type your code here. 10 public static void main(String args) Main test new MainO test.hailstoneO; 12 13 14 15Explanation / Answer
Main.java
public class Main {
public void hailstone() {
int val = 25;
int i = 0;
while(val!=1) {
i++;
System.out.print(val+" ");
if(val % 2 == 0){
val = val/2;
} else {
val = 3 * val + 1;
}
if(i%10==0){
System.out.println();
}
}
System.out.print(val+" ");
}
public static void main(String[] args) {
Main test = new Main();
test.hailstone();
}
}
Output:
25 76 38 19 58 29 88 44 22 11
34 17 52 26 13 40 20 10 5 16
8 4 2 1
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.