**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.

**Examples :**

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**.

`// Java program to Print Number ` `// series without using loop ` ` ` `import` `java.io.*; ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` `public` `static` `void` `PrintNumber(` `int` `N, ` `int` `Original, ` `int` `K, ` `boolean` `flag) ` ` ` `{ ` ` ` ` ` `// print the number ` ` ` `System.out.print(N + ` `" "` `); ` ` ` ` ` `// change flag if number ` ` ` `// become negative ` ` ` `if` `(N <= ` `0` `) ` ` ` `flag = !flag; ` ` ` ` ` `// base condition for ` ` ` `// second_case (Adding K) ` ` ` `if` `(N == Original && !flag) ` ` ` ` ` `return` `; ` ` ` ` ` `// if flag is true ` ` ` `// we subtract value until ` ` ` `// number is greater then zero ` ` ` `if` `(flag == ` `true` `) { ` ` ` `PrintNumber(N - K, Original, K, flag); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// second case (Addition ) ` ` ` `if` `(!flag) { ` ` ` `PrintNumber(N + K, Original, K, flag); ` ` ` `return` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `N = ` `20` `, K = ` `6` `; ` ` ` `PrintNumber(N, N, K, ` `true` `); ` ` ` `} ` `} ` `// This code is contributed by Mohit Gupta_OMG ` |

*chevron_right*

*filter_none*

**Output:**

20 14 8 2 -4 2 8 14 20

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

## Recommended Posts:

- How to print an Array in Java without using Loop
- Java Program to Print any Statement without Using the Main Method
- Java Program to Reverse a Number and find the Sum of its Digits Using do-while Loop
- Java Program to Print a Semicolon Without Using Semicolon
- Java Program to Iterate Over Arrays Using for and foreach Loop
- Java Program to Find Sum of Natural Numbers Using While Loop
- Flatten a Stream of Lists in Java using forEach loop
- Flatten a Stream of Arrays in Java using forEach loop
- Java Program to Count number of binary strings without consecutive 1's
- Java Program for nth multiple of a number in Fibonacci Series
- 3 Different ways to print Fibonacci series in Java
- Java Program for Range sum queries without updates
- Java Program to Illustrate a Method Without Parameters and Return Type
- Java Program to Multiply two Matrices of any size
- Java Program to Interchange Any Two Rows in the Matrix
- Java Program to Interchange Any Two Columns in the Matrix
- Java Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Java Program to Find sum of Series with n-th term as n^2 - (n-1)^2
- Java Program for efficiently print all prime factors of a given number
- Program to find Nth term of the series 3, 6, 18, 24, ...