Program to print the pattern with two hollow Triangles

Given a number n, the task is to write a program to draw the following pattern using ‘$’.

         $
       $ $ $
     $ $ $ $ $
   $ $ $ $ $ $ $
   $           $
   $ $       $ $
   $ $ $   $ $ $
   $ $ $ $ $ $ $
   $ $ $   $ $ $
   $ $       $ $
   $           $
   $ $ $ $ $ $ $
     $ $ $ $ $
       $ $ $
         $


Approach:
The above pattern is printed by breaking it into smaller patterns:

  1. The above triangle
            $
          $ $ $
        $ $ $ $ $
      $ $ $ $ $ $ $
    

    This is done with the help of for loop for rows 0 to n-1. Firstly the space is printed, then the ‘$’ is printed.

  2. The middle body
       $           $
       $ $       $ $
       $ $ $   $ $ $
       $ $ $ $ $ $ $
       $ $ $   $ $ $
       $ $       $ $
       $           $
    

    This again is divided into 3 smaller patterns:

    • The upper middle body
         $           $
         $ $       $ $
         $ $ $   $ $ $
      

      This is done with the help of for loop for rows 0 to n-2. Firstly the left ‘$’ is printed, then the space is printed, then the right ‘$’ is printed.

    • The middle middle body
         $ $ $ $ $ $ $
      

      This is done with the help of for loop for row = n-1. Here only the ‘$’ is printed for i 0 to (2n -1).

    • The lower middle body
         $ $ $   $ $ $
         $ $       $ $
         $           $
      

      This is done with the help of for loop for rows n to 2n-1. Firstly the left ‘$’ is printed, then the space is printed, then the right ‘$’ is printed.

  3. The lower triangle
       $ $ $ $ $ $ $
         $ $ $ $ $
           $ $ $
             $
    

    This is done with the help of for loop for rows 0 to n-1. This is done similar to the upper triangle pattern.

Hence the complete pattern will be drawn.

Below program prints the pattern as shown above:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach 
  
#include <iostream>
  
using namespace std;
  
void printPattern(int n) 
    
    // for upper triangle 
  //     $
 //    $ $ $
 //  $ $ $ $ $
// $ $ $ $ $ $ $
  
        for (int r = 0; r < n; r++) { 
  
            // for space 
            for (int i = r; i < n - 1; i++) {
                cout << " "
                cout << " "
                         }
  
            // for $ 
            for (int i = 0; i < 2 * r + 1; i++) 
                cout << "$ "
  
            // new line 
                cout << endl ; 
        
  
        // for middle body 
        // $           $
        // $ $       $ $
        // $ $ $   $ $ $
        // $ $ $ $ $ $ $
        // $ $ $   $ $ $
        // $ $       $ $
        // $           $
        for (int r = 0; r < 2 * n - 1; r++) { 
            if (r < n - 1) { 
  
                // for body 
                // $           $
                // $ $       $ $
                // $ $ $   $ $ $
  
  
                // for $ 
                for (int i = 0; i <= r; i++) 
                    cout << "$ "
  
                // for space 
                for (int i = 0; i <= 2 * (n - r - 1)-2; i++) {
                   cout << " "
                    cout << " "
                      
                }
  
                // for $ 
                for (int i = 0; i <= r; i++) 
                    cout << "$ "
            
            else if (r == n - 1) { 
  
                // for body 
                // $ $ $ $ $ $ $ 
  
                // for $ 
                for (int i = 0; i < 2 * r + 1; i++) 
                    cout << "$ "
            
            else
  
                // for body 
                // $ $ $   $ $ $
                // $ $       $ $
                // $           $
  
                // for $ 
                for (int i = 2 * n - 1; i > r; i--) 
                    cout << "$ "
  
                // for space 
                for (int i = n; i <= r; i++) {
                    cout << " "
                    cout << " "
                      
                }
  
                // for space 
                for (int i = n; i < r; i++) {
                    cout << " "
                    cout << " "
                      
                }
  
                // for $ 
                for (int i = 2 * n - 1; i > r; i--) 
                    cout << "$ "
            
  
            // new line 
            cout << endl ; 
        
  
        // for lower triangle 
        // $ $ $ $ $ $ $
        //   $ $ $ $ $
        //     $ $ $
        //       $
  
        for (int r = 0; r < n; r++) { 
  
            // for space 
            for (int i = 0; i < r; i++){ 
                cout << " "
                cout << " " ;
                }
  
            // for $ 
            for (int i = 0; i < 2 * (n - r) - 1; i++) 
                cout << "$ "
  
            // new line 
                cout << endl ; 
        
    
  
  
int main()
{
      
    int n = 4; 
    printPattern(n); 
    return 0;
      
    // This code is contributed by ANKITRAI1
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of above approach 
public class GFG {
  
    public static void printPattern(int n)
    {
    // for upper triangle 
  //     $
 //    $ $ $
 //  $ $ $ $ $
// $ $ $ $ $ $ $
  
        for (int r = 0; r < n; r++) {
  
            // for space
            for (int i = r; i < n - 1; i++){
                System.out.print(" ");
                System.out.print(" ");
            }
  
            // for $
            for (int i = 0; i < 2 * r + 1; i++)
                System.out.print("$ ");
  
            // new line
            System.out.println();
        }
  
        // for middle body 
        // $           $
        // $ $       $ $
        // $ $ $   $ $ $
        // $ $ $ $ $ $ $
        // $ $ $   $ $ $
        // $ $       $ $
        // $           $
  
        for (int r = 0; r < 2 * n - 1; r++) {
            if (r < n - 1) {
  
                // for body 
                // $           $
                // $ $       $ $
                // $ $ $   $ $ $
  
                // for $
                for (int i = 0; i <= r; i++)
                    System.out.print("$ ");
  
                // for space
                for (int i = 0; i <= 2 * (n - r - 1) - 2; i++){
                    System.out.print(" ");
                    System.out.print(" ");
                }
  
                // for $
                for (int i = 0; i <= r; i++)
                    System.out.print("$ ");
            }
            else if (r == n - 1) {
  
                // for body
                // $ $ $ $ $ $ $
  
                // for $
                for (int i = 0; i < 2 * r + 1; i++)
                    System.out.print("$ ");
            }
            else {
  
                // for body 
                // $ $ $   $ $ $
                // $ $       $ $
                // $           $
  
                // for $
                for (int i = 2 * n - 1; i > r; i--)
                    System.out.print("$ ");
  
                // for space
                for (int i = n; i <= r; i++){
                    System.out.print(" ");
                    System.out.print(" ");
                }
  
                // for space
                for (int i = n; i < r; i++){
                    System.out.print(" ");
                    System.out.print(" ");
                }
  
                // for $
                for (int i = 2 * n - 1; i > r; i--)
                    System.out.print("$ ");
            }
  
            // new line
            System.out.println();
        }
  
        // for lower triangle 
        // $ $ $ $ $ $ $
        //   $ $ $ $ $
        //     $ $ $
        //       $
  
        for (int r = 0; r < n; r++) {
  
            // for space
            for (int i = 0; i < r; i++){
                System.out.print(" ");
                System.out.print(" ");
            }
  
            // for $
            for (int i = 0; i < 2 * (n - r) - 1; i++)
                System.out.print("$ ");
  
            // new line
            System.out.println();
        }
    }
  
    // Driver Code
    public static void main(String[] args)
    {
  
        int n = 4;
        printPattern(n);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of above approach 
  
def printPattern(n):
    # for upper triangle 
#    $
# $ $ $
# $ $ $ $ $
# $ $ $ $ $ $ $
  
        for r in range(0,n,1):
            # for space 
            for i in range(r,n - 1,1):
                print(" ",end = " ")
  
            # for $ 
            for i in range(0,2 * r + 1,1):
                print("$",end = " "
  
            # new line 
            print("\n"
          
        # for middle body 
        # $      $
        # $ $    $ $
        # $ $ $ $ $ $
        # $ $ $ $ $ $ $
        # $ $ $ $ $ $
        # $ $    $ $
        # $      $
        for r in range(0,2 * n - 1,1):
            if (r < n - 1):
                # for body 
                # $      $
                # $ $    $ $
                # $ $ $ $ $ $
  
  
                # for $ 
                for i in range(0,r+1,1):
                    print("$",end = " "
  
                # for space 
                for i in range(0,2 * (n - r - 1)-2+1,1):
                    print(" ",end = " ")
                              
                # for $ 
                for i in range(0,r+1,1):
                    print("$", end = " ")
            elif (r == n - 1):
                # for body 
                # $ $ $ $ $ $ $ 
  
                # for $ 
                for i in range(0,2 * r + 1,1):
                    print("$",end = " ")
              
            else:
                # for body 
                # $ $ $ $ $ $
                # $ $    $ $
                # $      $
  
                # for $ 
                i = 2 * n - 1
                while(i > r):
                    print("$",end = " ")
                    i -= 1
  
                # for space 
                for i in range(n,r+1,1):
                    print(" ",end = " ")             
                  
                # for space 
                for i in range(n,r,1):
                    print(" ",end = " ")
          
                  
                # for $ 
                i = 2 * n - 1
                while(i > r):
                    print("$",end = " ")
                    i -=1
  
            # new line 
            print("\n")
        # for lower triangle 
        # $ $ $ $ $ $ $
        # $ $ $ $ $
        #    $ $ $
        #    $
  
        for r in range(0,n,1):
            # for space 
            for i in range(0,r,1):
                print(" ",end = " ")
  
            # for $ 
            for i in range(0,2 * (n - r) - 1,1):
                print("$",end = " ")
  
            # new line 
            print("\n")
# driver code
if __name__ == '__main__':
    n = 4
    printPattern(n)
      
# This code is contributed by
# Surendra_Gangwar
     

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of above approach 
using System;
  
class GFG {
  
    public static void printPattern(int n) {
          
        // for upper triangle 
        //     $
        //    $ $ $
        //  $ $ $ $ $
        // $ $ $ $ $ $ $
        for (int r = 0; r < n; r++) {
  
            // for space
            for (int i = r; i < n - 1; i++) {
                Console.Write(" ");
                Console.Write(" ");
            }
  
            // for $
            for (int i = 0; i < 2 * r + 1; i++)
            Console.Write("$ ");
  
            // new line
            Console.Write("\n");
        }
  
        // for middle body 
        // $           $
        // $ $       $ $
        // $ $ $   $ $ $
        // $ $ $ $ $ $ $
        // $ $ $   $ $ $
        // $ $       $ $
        // $           $
  
        for (int r = 0; r < 2 * n - 1; r++) {
            if (r < n - 1) {
  
                // for body 
                // $           $
                // $ $       $ $
                // $ $ $   $ $ $
  
                // for $
                for (int i = 0; i <= r; i++)
                Console.Write("$ ");
  
                // for space
                for (int i = 0; i <= 2 * (n - r - 1) - 2; i++) {
                    Console.Write(" ");
                    Console.Write(" ");
                }
  
                // for $
                for (int i = 0; i <= r; i++)
                Console.Write("$ ");
            }
            else if (r == n - 1) {
  
                // for body
                // $ $ $ $ $ $ $
  
                // for $
                for (int i = 0; i < 2 * r + 1; i++)
                Console.Write("$ ");
            }
            else {
  
                // for body 
                // $ $ $   $ $ $
                // $ $       $ $
                // $           $
  
                // for $
                for (int i = 2 * n - 1; i > r; i--)
                Console.Write("$ ");
  
                // for space
                for (int i = n; i <= r; i++) {
                    Console.Write(" ");
                    Console.Write(" ");
                }
  
                // for space
                for (int i = n; i < r; i++) {
                    Console.Write(" ");
                    Console.Write(" ");
                }
  
                // for $
                for (int i = 2 * n - 1; i > r; i--)
                Console.Write("$ ");
            }
  
            // new line
            Console.Write("\n");
        }
  
        // for lower triangle 
        // $ $ $ $ $ $ $
        //   $ $ $ $ $
        //     $ $ $
        //       $
  
        for (int r = 0; r < n; r++) {
  
            // for space
            for (int i = 0; i < r; i++) {
                Console.Write(" ");
                Console.Write(" ");
            }
  
            // for $
            for (int i = 0; i < 2 * (n - r) - 1; i++)
            Console.Write("$ ");
  
            // new line
            Console.Write("\n");
        }
    }
  
    // Driver Code
    public static void Main() {
  
        int n = 4;
        printPattern(n);
    }
}
  
// This code is contributed by ChitraNayal

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of above approach 
  
function printPattern($n
    // for upper triangle 
    // $
    // $ $ $
    // $ $ $ $ $
    // $ $ $ $ $ $ $
    for ($r = 0; $r < $n; $r++) 
    
  
        // for space 
        for ($i = $r; $i < $n - 1; $i++)
        {
            echo (" ") ; 
            echo (" ") ; 
        }
  
        // for $ 
        for ($i = 0; $i < 2 * $r + 1; $i++) 
            echo ("$ ") ; 
  
        // new line 
        echo ("\n") ; 
    
  
    // for middle body 
    // $         $
    // $ $ $ $
    // $ $ $ $ $ $
    // $ $ $ $ $ $ $
    // $ $ $ $ $ $
    // $ $ $ $
    // $         $
    for ($r = 0; $r < 2 * $n - 1; $r++)
    
        if ($r < $n - 1)
        
  
            // for body 
            // $         $
            // $ $ $ $
            // $ $ $ $ $ $
  
            // for $ 
            for ($i = 0; $i <= $r; $i++) 
                echo("$ ") ; 
  
            // for space 
            for ($i = 0; 
                 $i <= 2 * ($n - $r - 1) - 2; 
                 $i++) 
            {
                echo (" "); 
                echo (" "); 
                  
            }
  
            // for $ 
            for ($i = 0; $i <= $r; $i++) 
                echo("$ "); 
        
        else if ($r == $n - 1) 
        
  
            // for body 
            // $ $ $ $ $ $ $ 
  
            // for $ 
            for ($i = 0; $i < 2 * $r + 1; $i++) 
                echo("$ ") ; 
        
        else 
        
  
            // for body 
            // $ $ $ $ $ $
            // $ $ $ $
            // $         $
  
            // for $ 
            for ($i = 2 * $n - 1; $i > $r; $i--) 
                echo("$ ") ; 
  
            // for space 
            for ($i = $n; $i <= $r; $i++) 
            {
                echo(" ") ; 
                echo(" ") ; 
            }
  
            // for space 
            for ($i = $n; $i < $r; $i++)
            {
                echo (" ") ; 
                echo (" ") ; 
            }
  
            // for $ 
            for ($i = 2 * $n - 1; $i > $r; $i--) 
                echo("$ ") ; 
        
  
        // new line 
        echo("\n") ; 
    
  
    // for lower triangle 
    // $ $ $ $ $ $ $
    // $ $ $ $ $
    // $ $ $
    // $
    for ($r = 0; $r < $n; $r++) 
    
  
        // for space 
        for ($i = 0; $i < $r; $i++)
        
            echo(" ") ; 
            echo(" ") ;
        }
  
        // for $ 
        for ($i = 0; $i < 2 * ($n - $r) - 1; $i++) 
            echo ("$ ") ; 
  
        // new line 
        echo ("\n") ; 
    
  
// Driver Code
$n = 4; 
printPattern($n); 
  
  
// This code is contributed 
// by Shivi_Aggarwal
?>

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.