Open In App

Program to print the pattern with two hollow Triangles

Last Updated : 13 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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 
     
        $
      $ $ $
    $ $ $ $ $
  $ $ $ $ $ $ $

  1. 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 
     
   $           $
   $ $       $ $
   $ $ $   $ $ $
   $ $ $ $ $ $ $
   $ $ $   $ $ $
   $ $       $ $
   $           $

  1. 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 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. 
     
  1. The lower triangle 
     
   $ $ $ $ $ $ $
     $ $ $ $ $
       $ $ $
         $

  1. 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++
// 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
}
Java
// 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);
    }
}
Python3
# 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
    
C#
// 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
PHP
<?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
?>
JavaScript
<script>
      // JavaScript implementation of above approach
      function printPattern(n)
      {
      
        // for upper triangle
        //       $
        //     $ $ $
        //   $ $ $ $ $
        // $ $ $ $ $ $ $

        for (var r = 0; r < n; r++)
        {
        
          // for space
          for (var i = r; i < n - 1; i++)
          {
            document.write("&nbsp;&nbsp;");
            document.write("&nbsp;");
          }

          // for $
          for (var i = 0; i < 2 * r + 1; i++) document.write("$ ");

          // new line
          document.write("<br>");
        }

        // for middle body
        // $           $
        // $ $       $ $
        // $ $ $   $ $ $
        // $ $ $ $ $ $ $
        // $ $ $   $ $ $
        // $ $       $ $
        // $           $
        for (var r = 0; r < 2 * n - 1; r++)
        {
          if (r < n - 1)
          {
          
            // for body
            // $           $
            // $ $       $ $
            // $ $ $   $ $ $

            // for $
            for (var i = 0; i <= r; i++) document.write("$ ");

            // for space
            for (var i = 0; i <= 2 * (n - r - 1) - 2; i++) {
              document.write("&nbsp;&nbsp;");
              document.write("&nbsp;");
            }

            // for $
            for (var i = 0; i <= r; i++) document.write("$ ");
          } else if (r == n - 1) {
            // for body
            // $ $ $ $ $ $ $

            // for $
            for (var i = 0; i < 2 * r + 1; i++) document.write("$ ");
          } else {
            // for body
            // $ $ $   $ $ $
            // $ $       $ $
            // $           $

            // for $
            for (var i = 2 * n - 1; i > r; i--) document.write("$ ");

            // for space
            for (var i = n; i <= r; i++) {
              document.write("&nbsp;&nbsp;");
              document.write("&nbsp;");
            }

            // for space
            for (var i = n; i < r; i++) {
              document.write("&nbsp;&nbsp;");
              document.write("&nbsp;");
            }

            // for $
            for (var i = 2 * n - 1; i > r; i--) document.write("$ ");
          }

          // new line
          document.write("<br>");
        }

        // for lower triangle
        // $ $ $ $ $ $ $
        //   $ $ $ $ $
        //     $ $ $
        //       $

        for (var r = 0; r < n; r++) 
        {
        
          // for space
          for (var i = 0; i < r; i++) {
            document.write("&nbsp;&nbsp;");
            document.write("&nbsp;");
          }

          // for $
          for (var i = 0; i < 2 * (n - r) - 1; i++) document.write("$ ");

          // new line
          document.write("<br>");
        }
      }

      var n = 4;
      printPattern(n);
      
      // This code is contributed by rdtank.
    </script>

Output: 
      $ 
    $ $ $ 
  $ $ $ $ $ 
$ $ $ $ $ $ $ 
$           $ 
$ $       $ $ 
$ $ $   $ $ $ 
$ $ $ $ $ $ $ 
$ $ $   $ $ $ 
$ $       $ $ 
$           $ 
$ $ $ $ $ $ $ 
  $ $ $ $ $ 
    $ $ $ 
      $

 

Time Complexity : O(n2)
Auxiliary Space : O(1)


Article Tags :
Practice Tags :

Similar Reads