Pattern Printing

Practice this coding question on Coding Gurukul.

Problem statement

A training simulator lowers an integer counter in fixed steps until it reaches or crosses zero. It then retraces the same readings to return to its starting value. Your task Start at n, repeatedly subtract 5 until the value is nonpositive, then add 5 repeatedly until n is reached again. Print the complete sequence. Important details Print the turning value only once. If n is already nonpositive, print only n. Use recursion to model the descent and return; do not use a loop to generate the sequence. Values may become negative before turning. This exercise practices recursive calls and the work performed as those calls return. Worked example For n = 12, the descending values are 12, 7, 2, -3. Retracing gives 2, 7, 12, so print 12 7 2 -3 2 7 12.

Input format

One integer n.

Output format

Print the sequence separated by spaces.

Constraints

-100 <= n <= 1000.

Sample input

12

Sample output

12 7 2 -3 2 7 12

Sign in to run or submit a solution in C++, Java or Python.

Loading Coding Gurukul…