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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Program to print triangular number series till n
- Node.js REPL (READ, EVAL, PRINT, LOOP)
- Program to print the Sum of series -1 + 2 + 11 + 26 + 47 +.....
- Print the first N terms of the series 6, 28, 66, 120, 190, 276, ...
- Program to print the series 2, 1, 4, 3, 6, 5, .... up to N terms
- Program to print Arithmetic Progression series
- 3 Different ways to print Fibonacci series in Java
- Program to print binomial expansion series
- Program to print the series 1, 9, 17, 33, 49, 73, 97... till N terms
- Program to print the series 2, 15, 41, 80, 132, 197… till N terms
- Program to print the series 1, 3, 4, 8, 15, 27, 50… till N terms
- Print first N terms of series (0.25, 0.5, 0.75, ...) in fraction representation
- C++ Program to print Fibonacci Series using Class template
- Print a number strictly less than a given number such that all its digits are distinct.
- Find Nth number of the series 1, 6, 15, 28, 45, .....
- n'th multiple of a number in Fibonacci Series
- Find sum of product of number in given series
- Program to find the Nth number of the series 2, 10, 24, 44, 70.....
- Number of terms in Geometric Series with given conditions
- Print Bracket Number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.