Programs for printing pyramid patterns using recursion

This article is aimed at giving a recursive implementation for pattern printing.

  1. Simple triangle pattern:

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ code to demonstrate star pattern
    #include <iostream>
    using namespace std;
      
    // function to print a row
    void printn(int num)
    {
        // base case
        if (num == 0)
            return;
        cout << "* ";
      
        // recursively calling printn()
        printn(num - 1);
    }
      
    // function to print the pattern
    void pattern(int n, int i)
    {
        // base case
        if (n == 0)
            return;
        printn(i);
        cout << endl;
      
        // recursively calling pattern()
        pattern(n - 1, i + 1);
    }
      
    // driver function
    int main()
    {
        int n = 5;
        pattern(n, 1);
        return 0;
    }

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code to demonstrate star pattern
    import java.io.*;
      
    class GFG 
    {
          
    // function to print a row
    static void printn(int num)
    {
        // base case
        if (num == 0)
            return;
        System.out.print ("* ");
      
        // recursively calling printn()
        printn(num - 1);
    }
      
    // function to print the pattern
    static void pattern(int n, int i)
    {
        // base case
        if (n == 0)
            return;
        printn(i);
        System.out.println();
      
        // recursively calling pattern()
        pattern(n - 1, i + 1);
    }
      
    // Driver code
    public static void main (String[] args) 
    {
      
        int n = 5;
        pattern(n, 1);
    }
    }
      
    // This code is contributed by ajit.

    chevron_right

    
    

    Python3

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Python 3 code to demonstrate star pattern
      
    # function to print a row
    def printn(num):
          
        # base case
        if (num == 0):
            return
        print("*", end = " ")
      
        # recursively calling printn()
        printn(num - 1)
      
    # function to print the pattern
    def pattern(n, i):
          
        # base case
        if (n == 0):
            return
        printn(i)
        print("\n", end = "")
          
        # recursively calling pattern()
        pattern(n - 1, i + 1)
      
    # Driver Code
    if __name__ == '__main__':
        n = 5
        pattern(n, 1)
      
    # This code is contributed by
    # Surendra_Gangwar

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# code to demonstrate star pattern
    using System;
      
    class GFG
    {
              
    // function to print a row
    static void printn(int num)
    {
        // base case
        if (num == 0)
            return;
        Console.Write("* ");
      
        // recursively calling printn()
        printn(num - 1);
    }
      
    // function to print the pattern
    static void pattern(int n, int i)
    {
        // base case
        if (n == 0)
            return;
        printn(i);
        Console.WriteLine();
      
        // recursively calling pattern()
        pattern(n - 1, i + 1);
    }
      
    // Driver code
    static public void Main ()
    {
        int n = 5;
        pattern(n, 1);
    }
    }
      
    // This code is contributed by akt_mit.

    chevron_right

    
    

    PHP

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    // PHP code to demonstrate star pattern
      
    // Function to print a row
    function printn($num)
    {
        // base case
        if ($num == 0)
            return;
        echo "* ";
      
        // recursively calling printn()
        printn($num - 1);
    }
      
    // function to print the pattern
    function pattern($n, $i)
    {
        // base case
        if ($n == 0)
            return;
        printn($i);
        echo "\n";
      
        // recursively calling pattern()
        pattern($n - 1, $i + 1);
    }
      
    // Driver Code
    $n = 5;
    pattern($n, 1);
      
    // This code is contributed by @Tushil..
    ?>

    chevron_right

    
    

    Output:

    * 
    * * 
    * * * 
    * * * * 
    * * * * *
    
  2. After 180 degree rotation:

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ code to demonstrate star pattern
      
    #include <iostream>
    using namespace std;
      
    // function to print spaces
    void print_space(int space)
    {
        // base case
        if (space == 0)
            return;
        cout << " "
             << " ";
      
        // recursively calling print_space()
        print_space(space - 1);
    }
      
    // function to print asterisks
    void print_asterisk(int asterisk)
    {
        // base case
        if (asterisk == 0)
            return;
        cout << "* ";
      
        // recursively calling print_asterisk()
        print_asterisk(asterisk - 1);
    }
      
    // function to print the pattern
    void pattern(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_space(n - 1);
        print_asterisk(num - n + 1);
        cout << endl;
      
        // recursively calling pattern()
        pattern(n - 1, num);
    }
      
    // driver function
    int main()
    {
        int n = 5;
        pattern(n, n);
        return 0;
    }

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code to demonstrate star pattern 
    import java.util.*;
      
    class GFG
    {
      
        // function to print spaces 
        static void print_space(int space)
        {
            // base case 
            if (space == 0
            {
                return;
            }
            System.out.print(" " + " ");
      
            // recursively calling print_space() 
            print_space(space - 1);
        }
      
        // function to print asterisks 
        static void print_asterisk(int asterisk)
        {
            // base case 
            if (asterisk == 0)
            {
                return;
            }
            System.out.print("* ");
      
            // recursively calling print_asterisk() 
            print_asterisk(asterisk - 1);
        }
      
        // function to print the pattern 
        static void pattern(int n, int num)
        {
            // base case 
            if (n == 0)
            {
                return;
            }
            print_space(n - 1);
            print_asterisk(num - n + 1);
            System.out.println();
      
            // recursively calling pattern() 
            pattern(n - 1, num);
        }
      
        // Driver code 
        public static void main(String[] args) 
        {
            int n = 5;
            pattern(n, n);
        }
      
    // This code is contributed by Rajput-Ji

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# code to demonstrate star pattern
    using System;
      
    class GFG
    {
          
    // function to print spaces
    static void print_space(int space)
    {
        // base case
        if (space == 0)
            return;
        Console.Write(" " + " ");
      
        // recursively calling print_space()
        print_space(space - 1);
    }
      
    // function to print asterisks
    static void print_asterisk(int asterisk)
    {
        // base case
        if (asterisk == 0)
            return;
        Console.Write("* ");
      
        // recursively calling print_asterisk()
        print_asterisk(asterisk - 1);
    }
      
    // function to print the pattern
    static void pattern(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_space(n - 1);
        print_asterisk(num - n + 1);
        Console.WriteLine();
      
        // recursively calling pattern()
        pattern(n - 1, num);
    }
      
    // Driver code
    public static void Main()
    {
        int n = 5;
        pattern(n, n);
    }
    }
      
    // This code is contributed by Akanksha Rai

    chevron_right

    
    

    PHP

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    // PHP code to demonstrate star pattern
    // function to print spaces
    function print_space($space)
    {
        // base case
        if ($space == 0)
            return;
        echo " ",
                " ";
      
        // recursively calling print_space()
        print_space($space - 1);
    }
      
    // function to print asterisks
    function print_asterisk($asterisk)
    {
        // base case
        if ($asterisk == 0)
            return;
        echo "* ";
      
        // recursively calling print_asterisk()
        print_asterisk($asterisk - 1);
    }
      
    // function to print the pattern
    function pattern($n, $num)
    {
        // base case
        if ($n == 0)
            return;
        print_space($n - 1);
        print_asterisk(($num - $n) + 1);
        echo "\n";
      
        // recursively calling pattern()
        pattern($n - 1, $num);
    }
      
    // Driver Code
    $n = 5;
    pattern($n, $n);
      
    // This code is contributed by @Tushil.
    ?>

    chevron_right

    
    

    Output:

            * 
          * * 
        * * * 
      * * * * 
    * * * * *
    
  3. Printing Pyramid:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ code to demonstrate star pattern
      
    #include <iostream>
    using namespace std;
      
    // function to print spaces
    void print_space(int space)
    {
        // base case
        if (space == 0)
            return;
        cout << " ";
      
        // recursively calling print_space()
        print_space(space - 1);
    }
      
    // function to print asterisks
    void print_asterisk(int asterisk)
    {
        // base case
        if (asterisk == 0)
            return;
        cout << "* ";
      
        // recursively calling asterisk()
        print_asterisk(asterisk - 1);
    }
      
    // function to print the pattern
    void pattern(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_space(n - 1);
        print_asterisk(num - n + 1);
        cout << endl;
      
        // recursively calling pattern()
        pattern(n - 1, num);
    }
      
    // driver function
    int main()
    {
        int n = 5;
        pattern(n, n);
        return 0;
    }

    chevron_right

    
    

    Output:



        * 
       * * 
      * * * 
     * * * * 
    * * * * *
    
  4. Number Pattern

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ code to demonstrate printing pattern of numbers
      
    #include <iostream>
    using namespace std;
      
    // function to print a row
    void print_row(int no, int val)
    {
        // base case
        if (no == 0)
            return;
        cout << val << " ";
      
        // recursively calling print_row()
        print_row(no - 1, val);
    }
      
    // function to print the pattern
    void pattern(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_row(num - n + 1, num - n + 1);
        cout << endl;
      
        // recursively calling pattern()
        pattern(n - 1, num);
    }
    int main()
    {
        int n = 5;
        pattern(n, n);
    }

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code to demonstrate printing 
    // pattern of numbers
    class GFG
    {
          
    // function to print a row
    static void print_row(int no, int val)
    {
        // base case
        if (no == 0)
            return;
        System.out.print(val + " ");
      
        // recursively calling print_row()
        print_row(no - 1, val);
    }
      
    // function to print the pattern
    static void pattern(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_row(num - n + 1, num - n + 1);
        System.out.println();
      
        // recursively calling pattern()
        pattern(n - 1, num);
    }
      
    // Driver Code
    public static void main(String[] args)
    {
        int n = 5;
        pattern(n, n);
    }
    }
      
    // This code is contributed by Code_Mech.

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# code to demonstrate printing 
    // pattern of numbers
    using System;
      
    class GFG
    {
          
    // function to print a row
    static void print_row(int no, int val)
    {
        // base case
        if (no == 0)
            return;
        Console.Write(val + " ");
      
        // recursively calling print_row()
        print_row(no - 1, val);
    }
      
    // function to print the pattern
    static void pattern(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_row(num - n + 1, num - n + 1);
        Console.WriteLine();
      
        // recursively calling pattern()
        pattern(n - 1, num);
    }
      
    // Driver Code
    public static void Main()
    {
        int n = 5;
        pattern(n, n);
    }
    }
      
    // This code is contributed by Akanksha Rai

    chevron_right

    
    

    PHP

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    // PHP code to demonstrate printing
    // pattern of numbers
      
    // function to print a row
    function print_row($no, $val)
    {
        // base case
        if ($no == 0)
            return;
        echo $val . " ";
      
        // recursively calling print_row()
        print_row($no - 1, $val);
    }
      
    // function to print the pattern
    function pattern($n, $num)
    {
        // base case
        if ($n == 0)
            return;
        print_row($num - $n + 1, 
                  $num - $n + 1);
        echo "\n";
      
        // recursively calling pattern()
        pattern($n - 1, $num);
    }
      
    // Driver Code
    $n = 5;
    pattern($n, $n);
      
    // This code is contributed
    // by Akanksha Rai
    ?>

    chevron_right

    
    

    Output:

    1 
    2 2 
    3 3 3 
    4 4 4 4 
    5 5 5 5 5
    
  5. Numbers without re assigning:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ code to demonstrate printing pattern of numbers
      
    #include <iostream>
    using namespace std;
      
    // function to print a row
    int print_row(int ct, int num)
    {
        // base case
        if (num == 0)
            return ct;
        cout << ct << "\t";
      
        // recursively calling print_row()
        print_row(ct + 1, num - 1);
    }
      
    // function to print the pattern
    void pattern(int n, int count, int num)
    {
        // base case
        if (n == 0)
            return;
        count = print_row(count, num);
        cout << endl;
      
        // recursively calling pattern()
        pattern(n - 1, count, num + 1);
    }
      
    // driver function
    int main()
    {
        int n = 5;
        pattern(n, 1, 1);
    }

    chevron_right

    
    

    Output:

    1    
    2    3    
    4    5    6    
    7    8    9    10    
    11    12    13    14    15
    
  6. strong>Character Pattern:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ code to demonstrate printing pattern of alphabets
      
    #include <iostream>
    using namespace std;
      
    // function to print a row
    void print_row(int no, int val)
    {
        // base case
        if (no == 0)
            return;
        cout << (char)(val + 64) << " ";
      
        // recursively calling print_row()
        print_row(no - 1, val);
    }
      
    // function to print the pattern
    void pattern(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_row(num - n + 1, num - n + 1);
        cout << endl;
      
        // recursively calling pattern()
        pattern(n - 1, num);
    }
    int main()
    {
        int n = 5;
        pattern(n, n);
    }

    chevron_right

    
    

    Output:

    A 
    B B 
    C C C 
    D D D D 
    E E E E E
    
  7. Continuous Character pattern:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
      
    // function to print a row
    int print_row(int ct, int num)
    {
        // base case
        if (num == 0)
            return ct;
        cout << (char)(ct + 64) << " ";
      
        // recursively calling print_row()
        print_row(ct + 1, num - 1);
    }
      
    // function to print the pattern
    void pattern(int n, int count, int num)
    {
        // base case
        if (n == 0)
            return;
        count = print_row(count, num);
        cout << endl;
      
        // recursively calling pattern()
        pattern(n - 1, count, num + 1);
    }
      
    // driver function
    int main()
    {
        int n = 5;
        pattern(n, 1, 1);
    }

    chevron_right

    
    

    Output:

    A 
    B C 
    D E F 
    G H I J 
    K L M N O
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.