Problem – Givens Two number N and K, our task is to subtract a number K from N until number(N) is greater than zero, once the N becomes negative or zero then we start adding K until that number become the original number(N).
Note : Not allow to use any loop.
Input : N = 15 K = 5 Output : 15 10 5 0 1 5 10 15 Input : N = 20 K = 6 Output : 20 14 8 2 -4 2 8 14 20
Explanation – We can do it using recursion idea is that we call the function again and again until N is greater than zero (in every function call we subtract N by K). Once the number becomes negative or zero we start adding K in every function call until the number becomes the original number. Here we use a single function for both addition and subtraction but to switch between addition or subtraction function we used a Boolean flag.
20 14 8 2 -4 2 8 14 20
Please refer complete article on Print Number series without using any loop for more details!
- Python Program to print a number diamond of any given size N in Rangoli Style
- Python program to print Calendar without calendar or datetime module
- Python Program for Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Python program to convert any base to decimal by using int() method
- Python Program for Efficient program to print all prime factors of a given number
- Python Program to Count number of binary strings without consecutive 1's
- How to print without newline in Python?
- Python | Ways to print list without quotes
- Python - Ways to print longest consecutive list without considering duplicates element
- Python Program to Convert any Positive Real Number to Binary string
- Python Program for n\'th multiple of a number in Fibonacci Series
- Python - Detect loop in Dictionaries
- Python Program for Program to Print Matrix in Z form
- Python Program for Range sum queries without updates
- Python Program for Find sum of Series with n-th term as n^2 - (n-1)^2
- Python | Find current weather of any city using openweathermap API
- Python - Check if string contains any number
- Program to check if a string contains any special character
- Python | Find fibonacci series upto n using lambda
- Program to print all the numbers divisible by 3 and 5 for a given number