C Program to Print Number series without using any loop Last Updated : 24 Oct, 2023 Comments Improve Suggest changes Like Article Like Report 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 = 6Output : 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 <stdio.h> // 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; } Output20 14 8 2 -4 2 8 14 20 Time Complexity: O(N), where N is the value of the N variableAuxiliary Space: O(N) Please refer complete article on Print Number series without using any loop for more details! Comment More infoAdvertise with us Next Article C Program to Print Number series without using any loop K kartik Follow Improve Article Tags : C Language Similar Reads C Program to print numbers from 1 to N without using semicolon? How to print numbers from 1 to N without using any semicolon in C. C #include<stdio.h> #define N 100 // Add your code here to print numbers from 1 // to N without using any semicolon What code to add in above snippet such that it doesn't contain semicolon and prints numbers from 1 to N?We stro 2 min read How to print a number 100 times without using loop and recursion in C? It is possible to solve this problem using loop or a recursion method but what if both are not allowed? A simple solution is to write the number 100 times in cout statement. A better solution is to use #define directive (Macro expansion) CPP // CPP program to print "1" 100 times. // Prints 1 min read C program to print characters without using format specifiers As we know that there are various format specifiers in C like %d, %f, %c etc, to help us print characters or other data types. We normally use these specifiers along with the printf() function to print any variables. But there is also a way to print characters specifically without the use of %c form 1 min read Write a C program to print "Geeks for Geeks" without using a semicolon First of all we have to understand how printf() function works. Prototype of printf() function is: int printf( const char *format , ...) Parameter format: This is a string that contains a text to be written to stdout.Additional arguments: ... (Three dots are called ellipses) which indicates the vari 2 min read How will you print numbers from 1 to 100 without using a loop? If we take a look at this problem carefully, we can see that the idea of "loop" is to track some counter value, e.g., "i = 0" till "i <= 100". So, if we aren't allowed to use loops, how can we track something in the C language?Well, one possibility is the use of 'recursion', provided we use the t 12 min read Like