Print the given pattern recursively

07 Dec, 2021
Given a positive integer n. Print the inverted triangular pattern (as described in the examples below) using the recursive approach.
Examples: 
Input : n = 5
Output : 
* * * * *
* * * *
* * *
* *
*

Input : n = 7
Output :
* * * * * * *
* * * * * *
* * * * * 
* * * *
* * *
* *
*
Method 1 (Using two recursive functions): One recursive function is used to get the row number and the other recursive function is used to print the stars of that particular row.
Algorithm:  
printPatternRowRecur(n)
    if n < 1
        return
        
    print "* "
    printPatternRowRecur(n-1)

printPatternRecur(n)
    if n < 1
        return
    
    printPatternRowRecur(n)
    print "\n"
    printPatternRecur(n-1)
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation to print the given// pattern recursively#include <bits/stdc++.h>using namespace std; // function to print the 'n-th' row of the// pattern recursivelyvoid printPatternRowRecur(int n){    // base condition    if (n < 1)        return;             // print the remaining stars of the n-th row    // recursively       cout << "* ";    printPatternRowRecur(n-1);} void printPatternRecur(int n){    // base condition    if (n < 1)        return;         // print the stars of the n-th row       printPatternRowRecur(n);            // move to next line    cout << endl;         // print stars of the remaining rows recursively    printPatternRecur(n-1);     } // Driver program to test aboveint main(){    int n = 5;    printPatternRecur(n);    return 0;}
// java implementation to print the given// pattern recursivelyimport java.io.*; class GFG{    // function to print the 'n-th' row    // of the pattern recursively    static void printPatternRowRecur(int n)    {        // base condition        if (n < 1)            return;                     // print the remaining stars        // of the n-th row recursively        System.out.print( "* ");        printPatternRowRecur(n - 1);    }         static void printPatternRecur(int n)    {        // base condition        if (n < 1)            return;                 // print the stars of the n-th row        printPatternRowRecur(n);                 // move to next line        System.out.println ();                 // print stars of the        // remaining rows recursively        printPatternRecur(n - 1);             }     // Driver program to test above    public static void main (String[] args)    {        int n = 5;        printPatternRecur(n);             }}//This code is contributed by vt_m
# Python 3 implementation# to print the given# pattern recursively # function to print the# 'n-th' row of the# pattern recursivelydef printPatternRowRecur(n):     # base condition    if (n < 1):        return             # print the remaining    # stars of the n-th row    # recursively    print("*", end = " ")    printPatternRowRecur(n - 1) def printPatternRecur(n):     # base condition    if (n < 1):        return         # print the stars of    # the n-th row    printPatternRowRecur(n)         # move to next line    print("")         # print stars of the    # remaining rows recursively    printPatternRecur(n - 1)     # Driver Coden = 5printPatternRecur(n) # This code is contributed# by Smitha
// C# implementation to print the given// pattern recursivelyusing System;class GFG{    // function to print the 'n-th' row    // of the pattern recursively    static void printPatternRowRecur(int n)    {        // base condition        if (n < 1)            return;                     // print the remaining stars        // of the n-th row recursively        Console.Write( "* ");        printPatternRowRecur(n - 1);    }         static void printPatternRecur(int n)    {        // base condition        if (n < 1)            return;                 // print the stars of the n-th row        printPatternRowRecur(n);                 // move to next line         Console.WriteLine();                 // print stars of the        // remaining rows recursively        printPatternRecur(n - 1);             }     // Driver program to test above    public static void Main()    {        int n = 5;        printPatternRecur(n);             }}//This code is contributed by vt_m
<?php// php implementation to print the given// pattern recursively // function to print the 'n-th' row// of the pattern recursivelyfunction printPatternRowRecur($n){         // base condition    if ($n < 1)        return;             // print the remaining stars of    // the n-th row recursively    echo "* ";    printPatternRowRecur($n-1);} function printPatternRecur($n){    // base condition    if ($n < 1)        return;         // print the stars of the n-th row    printPatternRowRecur($n);         // move to next line    echo "\n";         // print stars of the remaining    // rows recursively    printPatternRecur($n-1);     }     // Driver code    $n = 5;    printPatternRecur($n); // This code is contributed by mits?>
<script> // JavaScript implementation to print the given// pattern recursively     // function to print the 'n-th' row    // of the pattern recursivelyfunction printPatternRowRecur(n){    // base condition    if (n < 1)        return;             // print the remaining stars    // of the n-th row recursively    document.write( "* ");    printPatternRowRecur(n - 1);} function printPatternRecur(n){    // base condition    if (n < 1)        return;         // print the stars of the n-th row    printPatternRowRecur(n);         // move to next line    document.write("<br>");         // print stars of the    // remaining rows recursively    printPatternRecur(n - 1);     } // Driver program to test abovevar n = 5;printPatternRecur(n); // This code is contributed by Amit Katiyar </script>
Output:
* * * * *
* * * *
* * *
* *
*
Method 2 (Using single recursive function): This approach uses a single recursive function to print the entire pattern.
Algorithm:  
printPatternRecur(n, i)
    if n < 1
        return
    
    if i <= n
        print "* "
        printPatternRecur(n, i+1)
        
    else
        print "\n"
        printPatternRecur(n-1, 1)
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation to print the given pattern recursively#include <bits/stdc++.h> using namespace std; // function to print the given pattern recursivelyvoid printPatternRecur(int n, int i){    // base condition    if (n < 1)        return;         // to print the stars of a particular row    if (i <= n)    {        cout << "* ";                 // recursively print rest of the stars        // of the row        printPatternRecur(n, i + 1);    }            else    {        // change line        cout << endl;                 // print stars of the remaining rows recursively        printPatternRecur(n-1, 1);    }} // Driver program to test aboveint main(){    int n = 5;    printPatternRecur(n, 1);    return 0;   }
// java implementation to// print the given pattern recursivelyimport java.io.*; class GFG {         // function to print the    // given pattern recursively    static void printPatternRecur(int n, int i)    {        // base condition        if (n < 1)            return;                 // to print the stars of        // a particular row        if (i <= n)        {            System.out.print ( "* ");                         // recursively print rest             // of the stars of the row            printPatternRecur(n, i + 1);        }                 else        {            // change line            System.out.println( );                         // print stars of the            // remaining rows recursively            printPatternRecur(n - 1, 1);        }    }         // Driver program    public static void main (String[] args)    {        int n = 5;        printPatternRecur(n, 1);             }} // This code is contributed by vt_m
# Python3 implementation to print the# given pattern recursively # function to print the given pattern# recursivelydef printPatternRecur(n, i):     # base condition    if (n < 1):        return         # to print the stars of a    # particular row    if (i <= n):             print("* ", end = "")                 # recursively print rest of        # the stars of the row        printPatternRecur(n, i + 1)         else:             # change line        print("")                 # print stars of the remaining        # rows recursively        printPatternRecur(n-1, 1) # Driver program to test aboven = 5printPatternRecur(n, 1) # This code is contributed by Smitha
// C# implementation to// print the given pattern recursivelyusing System;class GFG {         // function to print the    // given pattern recursively    static void printPatternRecur(int n, int i)    {        // base condition        if (n < 1)            return;                 // to print the stars of        // a particular row        if (i <= n)        {            Console.Write ( "* ");                         // recursively print rest            // of the stars of the row            printPatternRecur(n, i + 1);        }                 else        {            // change line            Console.WriteLine( );                         // print stars of the            // remaining rows recursively            printPatternRecur(n - 1, 1);        }    }         // Driver program    public static void Main ()    {        int n = 5;        printPatternRecur(n, 1);             }} // This code is contributed by vt_m
<?php// php implementation to print// the given pattern recursively // function to print the given// pattern recursivelyfunction printPatternRecur($n, $i){         // base condition    if ($n < 1)        return;         // to print the stars of    // a particular row    if ($i <= $n)    {        echo "* ";                 // recursively print rest of        // the stars  of the row        printPatternRecur($n, $i + 1);    }         else    {        // change line        echo "\n";                 // print stars of the remaining        // rows recursively        printPatternRecur($n - 1, 1);    }}     // Driver code    $n = 5;    printPatternRecur($n, 1); // This code is contributed by mits?>
<script> // JavaScript implementation to print// the given pattern recursively // Function to print the given// pattern recursivelyfunction printPatternRecur(n, i){         // Base condition    if (n < 1)        return;         // To print the stars of    // a particular row    if (i <= n)    {        document.write("*" + "  ");                 // Recursively print rest of the stars        // of the row        printPatternRecur(n, i + 1);    }    else    {                 // Change line        document.write("<br>");                 // Print stars of the remaining        // rows recursively        printPatternRecur(n - 1, 1);    }} // Driver codevar n = 5;printPatternRecur(n, 1); // This code is contributed by rdtank </script>
Output: 
* * * * *
* * * *
* * *
* *
*
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Mithun Kumar
Smitha Dinesh Semwal
rdtank
amit143katiyar
khushboogoyal499
simranarora5sos
pattern-printing
School Programming
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.