- Related Questions & Answers
- Add minimum number to an array so that the sum becomes even in C programming
- C Program for Tower of Hanoi
- C++ Program for Dijkstra’s shortest path algorithm?
- C/C++ Program for nth Catalan Number?
- C Program to Multiply two Floating Point Numbers?
- C program to find the length of a string?
- C Program to Check if all digits of a number divide it
- C Program to Check if a Given String is a Palindrome?
- Concatenate a string given number of times in C++ programming
- Write a program to Delete a Tree in C programming
- Write a C program to print “ Tutorials Point ” without using a semicolon
- Superperfect Number in C programming
- C Programming for sum of the series 0.6, 0.06, 0.006, 0.0006, …to n terms
- Sum of the nodes of a Singly Linked List in C Program
- Sum of the first N terms of the series 5,12, 23, 38…. in C Programming

In this article, we will learn about the solution to the problem statement given below −

**Problem statement** − Given Two number N and K, our problem is to subtract a number K from N until number(N) is greater than zero(0), once the N becomes negative or zero then we start adding K to it until that number become the original number(N).

**For example,**

N = 10 K = 4 Output will be: 10 6 2 -2 2 6 10

1. we call the function again and again until N is greater than zero (in every function call we subtract K from N ). 2. Once the number becomes negative or zero we start adding K in each function call until the number becomes the original number. 3. Here we used a single function for purpose of addition and subtraction but to switch between addition or subtraction function we used a Boolean type variable flag.

Now let’s observe the implementation in Python

def PrintNumber(N, Original, K, flag): #print the number print(N, end = " ") #if number become negative if (N <= 0): if(flag==0): flag = 1 else: flag = 0 if (N == Original and (not(flag))): return # if flag is true if (flag == True): PrintNumber(N - K, Original, K, flag) return if (not(flag)): PrintNumber(N + K, Original, K, flag); return N = 10 K = 4 PrintNumber(N, N, K, True)

10 6 2 -2 2 6 10

Here all variables are declared in global namespace as shown in the image below −

In this article, we learned about the terminology for printing a number series without using any kind of looping construct in Python 3.x. Or earlier.

Advertisements