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
- 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 +.....
- Program to print the series 2, 1, 4, 3, 6, 5, .... up to N terms
- Program to print binomial expansion series
- Program to print Arithmetic Progression series
- 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, .....
- Find sum of product of number in given series
- n'th multiple of a number in Fibonacci 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 firstname.lastname@example.org. 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.