Open In App

Print a pattern without using any loop

Improve
Improve
Like Article
Like
Save
Share
Report

Given a number n, print the following pattern without using any loop.

n, n-5, n-10, …, 0, 5, 10, …, n-5, n

Examples : 

Input: n = 16
Output: 16, 11, 6, 1, -4, 1, 6, 11, 16

Input: n = 10
Output: 10, 5, 0, 5, 10

We strongly recommend that you click here and practice it, before moving on to the solution.

Print a pattern without using any loop (using recursion):

Follow the given steps to solve the problem:

  • Create a recursive function with parameters as n and m and flag variable set as true
  • Print m and if the flag is false and the value of m is equal to n then return from the function
  • If the flag is true then check
    • If m-5 is greater than zero then recur for m-5
    • Else recur for m-5 and set the flag to false, as now we will be moving backward
  • Else recur for m+5

Below is the implementation of the above approach:

C++




// C++ program to print pattern that first reduces 5 one
// by one, then adds 5. Without any loop
#include <bits/stdc++.h>
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, than 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 value
void PrintPattern(int m)
{
    if (m > 0) {
        cout << m << '\n';
        PrintPattern(m - 5);
    }
  
    cout << m << '\n';
}
  
// Driver code
int main()
{
    int n = 16;
      
      // Function call
    PrintPattern(n);
    return 0;
}


Java




// Java program to print pattern that first reduces 5 one
// by one, then adds 5. Without any loop
import 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, than 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 code
    public static void main(String[] args)
    {
        int n = 16;
        
          // Function call
        printPattern(n, n, true);
    }
}
// This code is contributed by vt_m


Python3




# 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, than 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 Code
if __name__ == "__main__":
  n = 16
  
  # Function call
  printPattern(n, n, True)
  
# This code is contributed
# by HrushikeshChoudhary


C#




// C# program to print pattern that first reduces 5 one
// by one, then adds 5. Without any loop
using 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 code
    public static void Main()
    {
        int n = 16;
            
          // Function call
        printPattern(n, n, true);
    }
}
// This code is contributed by vt_m


PHP




<?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, than 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;
  
// Function call
printPattern($n, $n, true); 
  
// This code is contributed by m_kit 
?>


Javascript




<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, than 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 
let n = 16;
printPattern(n, n, true);
  
// This code is contributed by divyeshrabadiya07
  
</script>


Output

16
11
6
1
-4
1
6
11
16

Time Complexity: O(N)
Auxiliary Space: O(N), stack space for the recursion

Print a pattern without using any loop and extra variable:

To solve the problem follow the below idea:

The above program works fine and prints the desired out but uses extra variables. We can use two print statements. The first one before the recursive call prints all decreasing sequences. The second one after the recursive call to print the increasing sequence

Below is the implementation of the above approach:

C++




// 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
// variable
void 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 code
int main()
{
    int n = 16;
    
      // Function call
    printPattern(n);
    return 0;
}


Java




// 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 code
    public static void main(String[] args)
    {
  
        int n = 16;
  
          // Function call
        printPattern(n);
    }
}
  
// This code is contributed by vt_m


Python3




# 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 variable
  
  
def 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 Code
if __name__ == "__main__":
  n = 16
  
  # Function call
  printPattern(n)
  
# This code is contributed by
# Mohit kumar 29


C#




// 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 code
    public static void Main()
    {
  
        int n = 16;
  
          // Function call
        printPattern(n);
    }
}
  
// This code is contributed by vt_m


PHP




<?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 variable 
function 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; 
  
    // Function call
    printPattern($n); 
  
// This code is contributed by anuj_67. 
?>


Javascript




<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 

Time Complexity: O(N)
Auxiliary Space: O(N), stack space for the recursion

Thanks to AKSHAY RATHORE for suggesting the above solution.

 



Last Updated : 20 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads