# C Program to Print Number series without using any loop

Write a C program for given 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

## C Program to Print Number series without using any loop using Recursion:

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

Below is the implementation of the above approach:

## C

 `// C Program for the above approach` `#include `   `// Function to print the number series using recursion` `void` `PrintNumber(``int` `N, ``int` `Original, ``int` `K, ``int` `flag)` `{` `    ``// Print the number` `    ``printf``(``"%d "``, N);`   `    ``// Change flag if the number becomes negative` `    ``if` `(N <= 0)` `        ``flag = !flag;`   `    ``// Base condition for the second case (Adding K)` `    ``if` `(N == Original && !flag)` `        ``return``;`   `    ``// If the flag is true, subtract the value until the` `    ``// number is greater than zero` `    ``if` `(flag) {` `        ``PrintNumber(N - K, Original, K, flag);` `        ``return``;` `    ``}`   `    ``// Second case (Addition)` `    ``if` `(!flag) {` `        ``PrintNumber(N + K, Original, K, flag);` `        ``return``;` `    ``}` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `N = 20, K = 6;`   `    ``PrintNumber(N, N, K, 1);`   `    ``return` `0;` `}`

Output

`20 14 8 2 -4 2 8 14 20 `

Time Complexity: O(N), where N is the value of the N variable
Auxiliary Space: O(N)

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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!