Programs to print Triangle and Diamond patterns using recursion

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

  1. Pattern 1:

    Example:

    Input: 5
    Output: 
    * * * * *   * * * * * 
    * * * *       * * * * 
    * * *           * * * 
    * *               * * 
    *                   *
    

    Implementaition:

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Program to print the given pattern
      
    #include <iostream>
    using namespace std;
    void print_asterisk(int asterisk)
    {
        if (asterisk == 0)
            return;
        cout << "* ";
        print_asterisk(asterisk - 1);
    }
    void print_space(int space)
    {
        if (space == 0)
            return;
        cout << " "
             << " ";
        print_space(space - 1);
    }
      
    // function to print the pattern
    void pattern(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_asterisk(n);
        print_space(2 * (num - n) + 1);
        print_asterisk(n);
        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

    // Program to print the given pattern
    import java.util.*;
    class GFG
    {
    static void print_asterisk(int asterisk)
    {
        if (asterisk == 0)
            return;
        System.out.print("* ");
        print_asterisk(asterisk - 1);
    }
    static void print_space(int space)
    {
        if (space == 0)
            return;
        System.out.print(" ");
        System.out.print(" ");
        print_space(space - 1);
    }
      
    // function to print the pattern
    static void pattern(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_asterisk(n);
        print_space(2 * (num - n) + 1);
        print_asterisk(n);
        System.out.print("\n");
      
        // 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 PrinciRaj1992 

    chevron_right

    
    

    Python3

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Program to print the given pattern
    def print_asterisk(asterisk):
        if (asterisk == 0):
            return;
        print("* ", end = "");
        print_asterisk(asterisk - 1);
      
    def print_space(space):
        if (space == 0):
            return;
        print(" ", end = "");
        print(" ", end = "");
        print_space(space - 1);
      
    # function to print the pattern
    def pattern(n, num):
          
        # base case
        if (n == 0):
            return;
        print_asterisk(n);
        print_space(2 * (num - n) + 1);
        print_asterisk(n);
        print("");
      
        # recursively calling pattern()
        pattern(n - 1, num);
      
    # Driver Code
    if __name__ == '__main__':
        n = 5;
        pattern(n, n);
      
    # This code is contributed by Princi Singh

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# Program to print the given pattern
    using System;
          
    class GFG
    {
          
    static void print_asterisk(int asterisk)
    {
        if (asterisk == 0)
            return;
        Console.Write("* ");
        print_asterisk(asterisk - 1);
    }
      
    static void print_space(int space)
    {
        if (space == 0)
            return;
        Console.Write(" ");
        Console.Write(" ");
        print_space(space - 1);
    }
      
    // function to print the pattern
    static void pattern(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_asterisk(n);
        print_space(2 * (num - n) + 1);
        print_asterisk(n);
        Console.Write("\n");
      
        // 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 PrinciRaj1992

    chevron_right

    
    

    Output:

    * * * * *   * * * * * 
    * * * *       * * * * 
    * * *           * * * 
    * *               * * 
    *                   * 
    
  2. Pattern 2:

    Example:

    Input: 5
    Output: 
    *                   * 
    * *               * * 
    * * *           * * * 
    * * * *       * * * * 
    * * * * *   * * * * *
    

    Implementaition:

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Program to print the given pattern
      
    #include <iostream>
    using namespace std;
    void print_asterisk(int asterisk)
    {
        if (asterisk == 0)
            return;
        cout << "* ";
        print_asterisk(asterisk - 1);
    }
    void print_space(int space)
    {
        if (space == 0)
            return;
        cout << " "
             << " ";
        print_space(space - 1);
    }
      
    // function to print the pattern
    void pattern(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_asterisk(num - n + 1);
        print_space(2 * 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  program to print the given pattern
    class GFG 
    {
    static void print_asterisk(int asterisk)
    {
        if (asterisk == 0)
            return;
        System.out.print("* ");
        print_asterisk(asterisk - 1);
    }
    static void print_space(int space)
    {
        if (space == 0)
            return;
        System.out.print(" ");
        System.out.print(" ");
        print_space(space - 1);
    }
      
    // function to print the pattern
    static void pattern(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_asterisk(num - n + 1);
        print_space(2 * 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 Princi Singh

    chevron_right

    
    

    Python3

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Program to print the given pattern
    def print_asterisk(asterisk):
        if (asterisk == 0):
            return;
        print("*", end = " ");
        print_asterisk(asterisk - 1);
      
    def print_space(space):
        if (space == 0):
            return;
        print(" ", end = "");
        print(" ", end = "");
        print_space(space - 1);
      
    # function to print the pattern
    def pattern(n, num):
      
        # base case
        if (n == 0):
            return;
        print_asterisk(num - n + 1);
        print_space(2 * n - 1);
        print_asterisk(num - n + 1);
        print();
      
        # recursively calling pattern()
        pattern(n - 1, num);
      
    # Driver Code
    if __name__ == '__main__':
        n = 5;
        pattern(n, n);
          
    # This code is contributed by Princi Singh

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program to print the given pattern
    using System;
          
    class GFG 
    {
    static void print_asterisk(int asterisk)
    {
        if (asterisk == 0)
            return;
        Console.Write("* ");
        print_asterisk(asterisk - 1);
    }
      
    static void print_space(int space)
    {
        if (space == 0)
            return;
        Console.Write(" ");
        Console.Write(" ");
        print_space(space - 1);
    }
      
    // function to print the pattern
    static void pattern(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_asterisk(num - n + 1);
        print_space(2 * n - 1);
        print_asterisk(num - n + 1);
        Console.WriteLine();
      
        // 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

    
    

    Output:

    *                   * 
    * *               * * 
    * * *           * * * 
    * * * *       * * * * 
    * * * * *   * * * * * 
    
  3. Pattern 3:

    Example:

    Input: 5
    Output: 
        * * * * *
        * * * *  
        * * *    
        * *      
        *        
        *        
        * *      
        * * *    
        * * * *  
        * * * * *
    

    Implementaition:

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Program to print the given pattern
      
    #include <iostream>
    using namespace std;
    void print_asterisk(int asterisk)
    {
        if (asterisk == 0)
            return;
        cout << "* ";
        print_asterisk(asterisk - 1);
    }
      
    // function to print the upper-half of the pattern
    void pattern_upper(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_asterisk(n);
        cout << endl;
      
        // recursively calling pattern_upper()
        pattern_upper(n - 1, num);
    }
      
    // function to print the lower-half of the pattern
    void pattern_lower(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_asterisk(num - n + 1);
        cout << endl;
      
        // recursively calling pattern_lower()
        pattern_lower(n - 1, num);
    }
      
    // function to print the pattern
    void pattern(int n, int num)
    {
        pattern_upper(n, num);
        pattern_lower(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

    // Program to print the given pattern
    class GFG 
    {
    static void print_asterisk(int asterisk)
    {
        if (asterisk == 0)
            return;
        System.out.print("* ");
        print_asterisk(asterisk - 1);
    }
      
    // function to print the upper-half of the pattern
    static void pattern_upper(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_asterisk(n);
        System.out.println();
      
        // recursively calling pattern_upper()
        pattern_upper(n - 1, num);
    }
      
    // function to print the lower-half of the pattern
    static void pattern_lower(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_asterisk(num - n + 1);
        System.out.println();
      
        // recursively calling pattern_lower()
        pattern_lower(n - 1, num);
    }
      
    // function to print the pattern
    static void pattern(int n, int num)
    {
        pattern_upper(n, num);
        pattern_lower(n - 1, num);
    }
      
    // Driver Code
    public static void main(String[] args) 
    {
        int n = 5;
        pattern(n, n);
    }
      
    // This code is contributed by 29AjayKumar

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Program to print the given pattern
    using System;
          
    class GFG 
    {
    static void print_asterisk(int asterisk)
    {
        if (asterisk == 0)
            return;
        Console.Write("* ");
        print_asterisk(asterisk - 1);
    }
      
    // function to print the upper-half of the pattern
    static void pattern_upper(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_asterisk(n);
        Console.WriteLine();
      
        // recursively calling pattern_upper()
        pattern_upper(n - 1, num);
    }
      
    // function to print the lower-half of the pattern
    static void pattern_lower(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_asterisk(num - n + 1);
        Console.WriteLine();
      
        // recursively calling pattern_lower()
        pattern_lower(n - 1, num);
    }
      
    // function to print the pattern
    static void pattern(int n, int num)
    {
        pattern_upper(n, num);
        pattern_lower(n - 1, num);
    }
      
    // Driver Code
    public static void Main(String[] args) 
    {
        int n = 5;
        pattern(n, n);
    }
    }
      
    // This code is contributed by 29AjayKumar

    chevron_right

    
    

    Output:

        * * * * *
        * * * *  
        * * *    
        * *      
        *        
        *        
        * *      
        * * *    
        * * * *  
        * * * * *
    
  4. Pattern 4:

    Example:

    Input: 5
    Output: 
    * * * * *   * * * * * 
    * * * *       * * * * 
    * * *           * * * 
    * *               * * 
    *                   * 
    * *               * * 
    * * *           * * * 
    * * * *       * * * * 
    * * * * *   * * * * * 
    

    Implementaition:

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Program to print the given pattern
      
    #include <iostream>
    using namespace std;
    void print_asterisk(int asterisk)
    {
        if (asterisk == 0)
            return;
        cout << "* ";
        print_asterisk(asterisk - 1);
    }
    void print_space(int space)
    {
        if (space == 0)
            return;
        cout << " "
             << " ";
        print_space(space - 1);
    }
      
    // function to print the upper-half of the pattern
    void pattern_upper(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_asterisk(n);
        print_space(2 * (num - n) + 1);
        print_asterisk(n);
        cout << endl;
      
        // recursively calling pattern_upper()
        pattern_upper(n - 1, num);
    }
      
    // function to print the lower-half of the pattern
    void pattern_lower(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_asterisk(num - n + 1);
        print_space(2 * n - 1);
        print_asterisk(num - n + 1);
        cout << endl;
      
        // recursively calling pattern_lower()
        pattern_lower(n - 1, num);
    }
      
    // function to print the pattern
    void pattern(int n, int num)
    {
        pattern_upper(n, num);
        pattern_lower(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 Program to print the given pattern
    import java.util.*;
      
    class GFG 
    static void print_asterisk(int asterisk)
    {
        if (asterisk == 0)
            return;
        System.out.print("* ");
        print_asterisk(asterisk - 1);
    }
      
    static void print_space(int space)
    {
        if (space == 0)
            return;
        System.out.print(" ");
        System.out.print(" ");
        print_space(space - 1);
    }
      
    // function to print the upper-half of the pattern
    static void pattern_upper(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_asterisk(n);
        print_space(2 * (num - n) + 1);
        print_asterisk(n);
        System.out.print("\n");
      
        // recursively calling pattern_upper()
        pattern_upper(n - 1, num);
    }
      
    // function to print the lower-half of the pattern
    static void pattern_lower(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_asterisk(num - n + 1);
        print_space(2 * n - 1);
        print_asterisk(num - n + 1);
        System.out.print("\n");
      
        // recursively calling pattern_lower()
        pattern_lower(n - 1, num);
    }
      
    // function to print the pattern
    static void pattern(int n, int num)
    {
        pattern_upper(n, num);
        pattern_lower(n - 1, num);
    }
      
    // driver function
    public static void main(String[] args)
    {
        int n = 5;
        pattern(n, n);    
    }
    }
      
    // This code is contributed by 29AjayKumar

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# Program to print the given pattern
    using System;
      
    class GFG 
    static void print_asterisk(int asterisk)
    {
        if (asterisk == 0)
            return;
        Console.Write("* ");
        print_asterisk(asterisk - 1);
    }
      
    static void print_space(int space)
    {
        if (space == 0)
            return;
        Console.Write(" ");
        Console.Write(" ");
        print_space(space - 1);
    }
      
    // function to print the upper-half of the pattern
    static void pattern_upper(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_asterisk(n);
        print_space(2 * (num - n) + 1);
        print_asterisk(n);
        Console.Write("\n");
      
        // recursively calling pattern_upper()
        pattern_upper(n - 1, num);
    }
      
    // function to print the lower-half of the pattern
    static void pattern_lower(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_asterisk(num - n + 1);
        print_space(2 * n - 1);
        print_asterisk(num - n + 1);
        Console.Write("\n");
      
        // recursively calling pattern_lower()
        pattern_lower(n - 1, num);
    }
      
    // function to print the pattern
    static void pattern(int n, int num)
    {
        pattern_upper(n, num);
        pattern_lower(n - 1, num);
    }
      
    // Driver Code
    public static void Main(String[] args)
    {
        int n = 5;
        pattern(n, n); 
    }
    }
      
    // This code is contributed by 29AjayKumar

    chevron_right

    
    

    Output:

    * * * * *   * * * * * 
    * * * *       * * * * 
    * * *           * * * 
    * *               * * 
    *                   * 
    * *               * * 
    * * *           * * * 
    * * * *       * * * * 
    * * * * *   * * * * * 
    
  5. Pattern 5:

    Example:

    Input: 5
    Output: 
    *                   * 
    * *               * * 
    * * *           * * * 
    * * * *       * * * * 
    * * * * *   * * * * * 
    * * * *       * * * * 
    * * *           * * * 
    * *               * * 
    *                   * 
    

    Implementaition:

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Program to print the given pattern
      
    #include <iostream>
    using namespace std;
    void print_asterisk(int asterisk)
    {
        // base case
        if (asterisk == 0)
            return;
        cout << "* ";
        print_asterisk(asterisk - 1);
    }
    void print_space(int space)
    {
        // base case
        if (space == 0)
            return;
        cout << " "
             << " ";
        print_space(space - 1);
    }
      
    // function to print the upper-half of the pattern
    void pattern_upper(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_asterisk(num - n + 1);
        print_space(2 * n - 1);
        print_asterisk(num - n + 1);
        cout << endl;
      
        // recursively calling pattern_upper()
        pattern_upper(n - 1, num);
    }
      
    // function to print the lower-half of the pattern
    void pattern_lower(int n, int num)
    {
        if (n == 0)
            return;
        print_asterisk(n);
        print_space(2 * (num - n) + 1);
        print_asterisk(n);
        cout << endl;
      
        // recursively calling pattern_lower(
        pattern_lower(n - 1, num);
    }
      
    // function to print the pattern
    void pattern(int n, int num)
    {
        pattern_upper(n, num);
        pattern_lower(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 program to print the given pattern
    class GFG 
    {
      
    static void print_asterisk(int asterisk)
    {
        // base case
        if (asterisk == 0)
            return;
        System.out.print("* ");
        print_asterisk(asterisk - 1);
    }
      
    static void print_space(int space)
    {
        // base case
        if (space == 0)
            return;
        System.out.print(" ");
        System.out.print(" ");
        print_space(space - 1);
    }
      
    // function to print the upper-half of the pattern
    static void pattern_upper(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_asterisk(num - n + 1);
        print_space(2 * n - 1);
        print_asterisk(num - n + 1);
        System.out.println();
      
        // recursively calling pattern_upper()
        pattern_upper(n - 1, num);
    }
      
    // function to print the lower-half of the pattern
    static void pattern_lower(int n, int num)
    {
        if (n == 0)
            return;
        print_asterisk(n);
        print_space(2 * (num - n) + 1);
        print_asterisk(n);
        System.out.println();
      
        // recursively calling pattern_lower(
        pattern_lower(n - 1, num);
    }
      
    // function to print the pattern
    static void pattern(int n, int num)
    {
        pattern_upper(n, num);
        pattern_lower(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# program to print the given pattern
    using System;
          
    class GFG 
    {
    static void print_asterisk(int asterisk)
    {
        // base case
        if (asterisk == 0)
            return;
        Console.Write("* ");
        print_asterisk(asterisk - 1);
    }
      
    static void print_space(int space)
    {
        // base case
        if (space == 0)
            return;
        Console.Write(" ");
        Console.Write(" ");
        print_space(space - 1);
    }
      
    // function to print 
    // the upper-half of the pattern
    static void pattern_upper(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        print_asterisk(num - n + 1);
        print_space(2 * n - 1);
        print_asterisk(num - n + 1);
        Console.WriteLine();
      
        // recursively calling pattern_upper()
        pattern_upper(n - 1, num);
    }
      
    // function to print the lower-half of the pattern
    static void pattern_lower(int n, int num)
    {
        if (n == 0)
            return;
        print_asterisk(n);
        print_space(2 * (num - n) + 1);
        print_asterisk(n);
        Console.WriteLine();
      
        // recursively calling pattern_lower(
        pattern_lower(n - 1, num);
    }
      
    // function to print the pattern
    static void pattern(int n, int num)
    {
        pattern_upper(n, num);
        pattern_lower(n - 1, num);
    }
      
    // Driver Code
    public static void Main(String[] args)
    {
        int n = 5;
        pattern(n, n);
    }
    }
      
    // This code is contributed by Princi Singh

    chevron_right

    
    

    Output:

    *                   * 
    * *               * * 
    * * *           * * * 
    * * * *       * * * * 
    * * * * *   * * * * * 
    * * * *       * * * * 
    * * *           * * * 
    * *               * * 
    *                   * 
    


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.