Print a pattern without using any loop - GeeksforGeeks

11 Sep, 2021
Given a number n, print following a pattern without using any loop.
Examples : 
Input: n = 16
Output: 16, 11, 6, 1, -4, 1, 6, 11, 16

Input: n = 10
Output: 10, 5, 0, 5, 10
We basically first reduce 5 one by one until we reach a negative or 0. After we reach 0 or negative, we one add 5 until we reach n.Source: Microsoft Interview Question.
The idea is to use recursion. It is an interesting question to try on your own. Below is the Code. The code uses a flag variable to indicate whether we are moving toward 0 or we are moving toward the back to n. 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to print pattern that first reduces 5 one// by one, then adds 5. Without any loop#include <iostream>using namespace std; // Recursive function to print the pattern.// n indicates input value// m indicates current value to be printed// flag indicates whether we need to add 5 or// subtract 5. Initially flag is true.void printPattern(int n, int m, bool flag){    // Print m.    cout << m << " ";         // If we are moving back toward the n and    // we have reached there, then we are done    if (flag == false && n ==m)        return;         // If we are moving toward 0 or negative.    if (flag)    {    // If m is greater, then 5, recur with true flag    if (m-5 > 0)        printPattern(n, m-5, true);    else // recur with false flag        printPattern(n, m-5, false);    }    else // If flag is false.        printPattern(n, m+5, false);} // Recursive function to print the pattern// variance where m is the input int32 valuevoid PrintPattern(int m){    if (m > 0)    {        cout << m << '\n';        PrintPattern(m - 5);    }     cout << m << '\n';} // Driver Programint main(){    int n = 16;    //printPattern(n, n, true);    PrintPattern(n);    return 0;}
// Java program to print pattern that first reduces 5 one// by one, then adds 5. Without any loopimport java.io.*; class GFG {         // Recursive function to print the pattern.    // n indicates input value    // m indicates current value to be printed    // flag indicates whether we need to add 5 or    // subtract 5. Initially flag is true.    static void printPattern(int n, int m, boolean flag)    {                 // Print m.        System.out.print(m + " ");         // If we are moving back toward the n and        // we have reached there, then we are done        if (flag == false && n == m)            return;         // If we are moving toward 0 or negative.        if (flag) {             // If m is greater, then 5, recur with            // true flag            if (m - 5 > 0)                printPattern(n, m - 5, true);             else // recur with false flag                printPattern(n, m - 5, false);        }         else // If flag is false.            printPattern(n, m + 5, false);    }     // Driver Program    public static void main(String[] args)    {        int n = 16;        printPattern(n, n, true);    }}// This code is contributed by vt_m
# Python program to print pattern# that first reduces 5 one by one,# then adds 5. Without any loop. # Recursive function to print# the pattern.n indicates# input value m indicates# current value to be printed# flag indicates whether we# need to add 5 or subtract 5.# Initially flag is True.def printPattern(n, m, flag):         # Print m.    print(m)         # If we are moving back    # toward the n and we    # have reached there,    # then we are done    if flag == False and n == m:        return    # If we are moving    # toward 0 or negative.    if flag:    # If m is greater, then 5,    # recur with true flag        if m - 5 > 0:            printPattern(n, m - 5, True)        else: # recur with false flag            printPattern(n, m - 5, False)    else: # If flag is false.        printPattern(n, m + 5, False) # Driver Coden = 16printPattern(n, n, True) # This code is contributed# by HrushikeshChoudhary
// C# program to print pattern that first reduces 5 one// by one, then adds 5. Without any loopusing System; class GFG {         // Recursive function to print the pattern.    // n indicates input value    // m indicates current value to be printed    // flag indicates whether we need to add 5 or    // subtract 5. Initially flag is true.    static void printPattern(int n, int m, bool flag)    {                 // Print m.        Console.Write(m + " ");         // If we are moving back toward the n and        // we have reached there, then we are done        if (flag == false && n == m)            return;         // If we are moving toward 0 or negative.        if (flag) {             // If m is greater, then 5, recur with            // true flag            if (m - 5 > 0)                printPattern(n, m - 5, true);             else // recur with false flag                printPattern(n, m - 5, false);        }         else // If flag is false.            printPattern(n, m + 5, false);    }     // Driver Program    public static void Main()    {        int n = 16;        printPattern(n, n, true);    }}// This code is contributed by vt_m
<?php// PHP program to print pattern// that first reduces 5 one by one,// then adds 5. Without any loop // Recursive function to print// the pattern. n indicates input// value m indicates current value// to be printed flag indicates whether// we need to add 5 or subtract 5.// Initially flag is true.function printPattern($n, $m, $flag){    // Print m.    echo $m ," ";         // If we are moving back    // toward the n and we    // have reached there,    // then we are done    if ($flag == false && $n == $m)        return;         // If we are moving    // toward 0 or negative.    if ($flag)    {    // If m is greater, then 5,    // recur with true flag    if ($m - 5 > 0)        printPattern($n, $m - 5, true);         // recur with false flag    else        printPattern($n, $m - 5, false);    }         // If flag is false.    else        printPattern($n, $m + 5, false);} // Driver Code$n = 16;printPattern($n, $n, true); // This code is contributed by m_kit?>
<script> // Javascript program to print pattern that// first reduces 5 one by one, then adds 5.// Without any loop // Recursive function to print the pattern.// n indicates input value m indicates current// value to be printed flag indicates whether// we need to add 5 or subtract 5. Initially// flag is true.function printPattern(n, m, flag){         // Print m.    document.write(m + " ");     // If we are moving back toward the n and    // we have reached there, then we are done    if (flag == false && n == m)        return;     // If we are moving toward 0 or negative.    if (flag)    {                 // If m is greater, then 5, recur with        // true flag        if (m - 5 > 0)            printPattern(n, m - 5, true);                     // Recur with false flag        else            printPattern(n, m - 5, false);    }         // If flag is false.    else        printPattern(n, m + 5, false);} // Driver codelet n = 16;printPattern(n, n, true); // This code is contributed by divyeshrabadiya07 </script>
Output : 
16, 11, 6, 1, -4, 1, 6, 11, 16
How to print above pattern without any extra variable and loop? The above program works fine and prints the desired out but uses extra variables. We can use two print statements. First one before the recursive call that prints all decreasing sequence. The second one after the recursive call to print increasing sequence.
Below is the implementation of the idea.
C++
Java
Python3
C#
PHP
Javascript
// C++ program to print pattern that first reduces 5 one// by one, then adds 5. Without any loop an extra variable.#include <iostream>using namespace std; // Recursive function to print the pattern without any extra// variablevoid printPattern(int n){    // Base case (When n becomes 0 or negative)    if (n ==0 || n<0)    {        cout << n << " ";        return;    }         // First print decreasing order    cout << n << " ";    printPattern(n-5);     // Then print increasing order    cout << n << " ";} // Driver Programint main(){    int n = 16;    printPattern(n);    return 0;}
// Java program to print pattern that first// reduces 5 one by one, then adds 5.// Without any loop an extra variable. import java.io.*; class GFG {         // Recursive function to print the    // pattern without any extra variable    static void printPattern(int n)    {                 // Base case (When n becomes 0 or        // negative)        if (n == 0 || n < 0) {                         System.out.print(n + " ");                         return;        }         // First print decreasing order        System.out.print(n + " ");                 printPattern(n - 5);         // Then print increasing order        System.out.print(n + " ");    }     // Driver Program    public static void main(String[] args)    {                 int n = 16;                 printPattern(n);    }} // This code is contributed by vt_m
# Python3 program to print pattern that# first reduces 5 one by one, then adds 5.# Without any loop an extra variable. # Recursive function to print the pattern# without any extra variabledef printPattern(n):     # Base case (When n becomes 0 or negative)    if (n == 0 or n < 0):        print(n, end = ", ")        return         # First print decreasing order    print(n, end = ", ")    printPattern(n - 5)     # Then print increasing order    print(n, end = ", ") # Driver Coden = 16printPattern(n) # This code is contributed by# Mohit kumar 29
// C# program to print pattern that first// reduces 5 one by one, then adds 5.// Without any loop an extra variable. using System; class GFG {         // Recursive function to print the    // pattern without any extra variable    static void printPattern(int n)    {                 // Base case (When n becomes 0 or        // negative)        if (n == 0 || n < 0) {                         Console.Write(n + " ");                         return;        }         // First print decreasing order        Console.Write(n + " ");                 printPattern(n - 5);         // Then print increasing order        Console.Write(n + " ");    }     // Driver Program    public static void Main()    {                 int n = 16;                 printPattern(n);    }} // This code is contributed by vt_m
<?php// PHP program to print pattern// that first reduces 5 one// by one, then adds 5. Without// any loop an extra variable. // Recursive function to print the// pattern without any extra variablefunction printPattern( $n){         // Base case (When n becomes    // 0 or negative)    if ($n == 0 or $n < 0)    {        echo $n , " ";        return;    }         // First print decreasing order    echo $n , " ";    printPattern($n-5);     // Then print increasing order    echo $n , " ";}     // Driver Code    $n = 16;    printPattern($n); // This code is contributed by anuj_67.?>
<script>    // Javascript program to print pattern that first reduces 5 one    // by one, then adds 5. Without any loop an extra variable.         // Recursive function to print the pattern without any extra    // variable    function printPattern(n)    {             // Base case (When n becomes 0 or negative)        if (n == 0 || n < 0)        {            document.write(n + ", ");            return;        }         // First print decreasing order        document.write(n + ", ");        printPattern(n - 5);         // Then print increasing order        document.write(n + ", ");    }         let n = 16;    printPattern(n);         // This code is contributed by suresh07.</script>
Output: 
16, 11, 6, 1, -4, 1, 6, 11, 16
Thanks to AKSHAY RATHORE for suggesting the above solution.This article is contributed by Gautham. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above 
jit_t
vt_m
HrushikeshChoudhary
mohit kumar 29
AbhijeetHaldar
Enigma341
divyeshrabadiya07
suresh07
surinderdawra388
Microsoft
pattern-printing
Misc
Recursion
Microsoft
Misc
pattern-printing
Recursion
Misc
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to write Regular Expressions?
Minimax Algorithm in Game Theory | Set 3 (Tic-Tac-Toe AI - Finding optimal move)
fgets() and gets() in C language
Association Rule
Recursive Functions
Write a program to print all permutations of a given string
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Recursion
Program for Tower of Hanoi
Backtracking | Introduction