2. Generate the Collatz sequence starting at n, using a while loop to apply the following rules until you reach the number 1: a. If the number is even, divide it by 2. b. If the number is odd, multiply it by 3 and add 1.
3.Print the sequence.
Right Answer:
n = int(input("Enter a number: "))
sequence = [n]
while n != 1:
if n % 2 == 0:
n = n // 2
else:
n = 3 * n + 1
sequence.append(n)
print("Collatz sequence:", sequence)