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

COMP163 Flowchart Programs 2. Write a Java application to display the \"hailston

ID: 3871301 • Letter: C

Question

COMP163 Flowchart Programs 2. Write a Java application to display the "hailstone" values for an integer. The Collatz conjecture is: This process will eventually reach the number 1, regardless of which positive integer is chosen initially. [Extra credit: Prove mathematically that the Collatz conjecture is true Begin Input:bird bird != 1 'rue False bird % 2:0 bird bird , 2 bird = 3.bird + 1 Output: bird End Example outputs: Enter a number 10 16 Enter a number >9 28 14 34 17 52 26 13 40 20 10 16

Explanation / Answer

Given below is the java code to implement the flowchart given. Please rate if it helped. Thank you.


import java.util.Scanner;
public class Collatz {
public static void main(String[] args) {
Scanner keybd = new Scanner(System.in);
System.out.print("Enter a number: ");
int bird = keybd.nextInt();
while(bird != 1)
{
if(bird % 2 == 0)
bird = bird / 2;
else
bird = 3 * bird + 1;
System.out.println(bird);
}
}
}

output

Enter a number: 9
28
14
7
22
11
34
17
52
26
13
40
20
10
5
16
8
4
2
1

Enter a number: 10
5
16
8
4
2
1

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