Python Program for Print Number Series Without Using any Loop (original) (raw)

Last Updated : 30 Oct, 2025

Given two numbers N and K, the task is to print a number series where each term is obtained by repeatedly subtracting K from N until the number becomes zero or negative and once it becomes zero or negative, we start adding K back until the number reaches the original value N again. You must do this without using any loop.

**For Example:

**Input: N = 15, K = 5
**Output: 15 10 5 0 -5 0 5 10 15

**Input: N = 20, K = 6
**Output: 20 14 8 2 -4 2 8 14 20

Let's explore different methods to print number series without using loop in Python.

Recursive Mathematical Approach

This method uses recursion to repeatedly subtract and add the number K. The same function calls itself until number becomes zero or negative and then reverses back to the original value, forming the full pattern automatically.

Python `

def print_series(N, K): if N <= 0: print(N, end=" ") return print(N, end=" ") print_series(N - K, K) print(N, end=" ")

N = 20 K = 6 print_series(N, K)

`

Output

20 14 8 2 -4 2 8 14 20

**Explanation:

Recursive Toggle Approach Using a Boolean Flag

This method introduces a flag variable to control whether the program should subtract or add K. Once the number becomes zero or negative, the flag flips, and the recursion direction reverses automatically.

Python `

def print_number(N, original, K, flag): print(N, end=" ")

if N <= 0:
    flag = not flag

if N == original and not flag:
    return

if flag:
    print_number(N - K, original, K, flag)
else:
    print_number(N + K, original, K, flag)

N = 20 K = 6 print_number(N, N, K, True)

`

Output

20 14 8 2 -4 2 8 14 20

**Explanation:

Recursive One-Liner (Lambda Expression)

This method uses a lambda function (anonymous function) and Python’s recursive nature in a single line. It’s compact and demonstrates recursion elegantly.

Python `

f = lambda n, k: print(n, end=" ") or (f(n - k, k) or print(n, end=" ")) if n > 0 else print(n, end=" ") f(20, 6)

`

Output

20 14 8 2 -4 2 8 14 20

**Explanation:

Using Recursion with List Storage and Unpacking

This method stores the entire recursive series inside a list and then prints it using the * unpacking operator for clean output.

Python `

def series(n, k): return [n] if n <= 0 else [n] + series(n - k, k) + [n]

N, K = 20, 6 print(*series(N, K))

`

Output

20 14 8 2 -4 2 8 14 20

**Explanation:

Please refer complete article on Print Number series without using any loop for more details!