[Type C]Q3: Write a function for the hailstone sequence as directed.

What is Recursion Function?

recursive function is a function defined in terms of itself via self-referential expressions.

This means that the function will continue to call itself and repeat its behavior until some condition is met.

Problem: The hailstone sequence starting at positive integer n is generated by following two simple rules. If n is even, the next number in the sequence is n/2. If n is odd, the next number in the sequence is 3*n+1. Repeating this process, the hailstone sequence is generated. Write a recursive function, Hailstone(n) which prints the hailstone sequence beginning at n. Stop when the sequence reaches the number 1(Since otherwise, we would loop forever).

Program Code:

Output:

[10, 5, 16, 8, 4, 2, 1]
[7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
[8, 4, 2, 1]

The above code demonstrates recursion in python.

To view all the lessons in chapter 6: http://computertutor.in/class-12-computer-science/chapter-6/

To view entire class 12 computer science study material: http://computertutor.in/resources/

You cannot copy content of this page