Print Number series without using any loop
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.
C++
// C++ program to Print Number // series without using loop #include <iostream> using namespace std; // function print series // using recursion void PrintNumber(int N, int Original, int K, bool flag) { // print the number cout << 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; } } // driver program int main() { int N = 20, K = 6; PrintNumber(N, N, K, true); return 0; } |
Java
// 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 |
Python3
# Python program to Print Number # series without using loop def PrintNumber(N, Original, K, flag): #print the number print(N, end = " ") # change flag if number # become negative if (N <= 0): if(flag==0): flag = 1 else: flag = 0 # base condition for # second_case (Adding K) if (N == Original and (not(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 (not(flag)): PrintNumber(N + K, Original, K, flag); return N = 20K = 6PrintNumber(N, N, K, True) # This code is contributed by Mohit Gupta_OMG |
C#
// C# program to Print Number // series without using loop using System; public class GFG { // function print series // using recursion static void PrintNumber(int N, int Original, int K, bool flag) { // print the number Console.Write(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; } } // driver program static public void Main () { int N = 20, K = 6; PrintNumber(N, N, K, true); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to Print Number // series without using loop // function print series // using recursion function PrintNumber($N, $Original, $K, $flag) { // print the number echo($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; } } // Driver Code $N = 20; $K = 6; PrintNumber($N, $N, $K, true); // This code is contributed by Ajit. ?> |
Output :
20 14 8 2 -4 2 8 14 20
Recommended Posts:
- 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 contribute@geeksforgeeks.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.



