Open In App

Program to print the Cot Bed Pattern

Improve
Improve
Like Article
Like
Save
Share
Report

The given task is to draw the Cot Bed pattern using ‘$’

Cot Bed Pattern:  

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

Below is the code to implement the above problem:

Program:  

C++




// C++ program to print
// the Cot Bed Pattern
 
// header files
#include <bits/stdc++.h>
using namespace std;
 
// function to print the pattern
void pattern(int nos, int i, int space)
{
    char prt = '$';
    int s, j;
 
    // loop for the spacing between the $ sign
    for (s = nos; s >= 1; s--)
    {
        cout <<" ";
    }
 
    // skipping the $
    for (j = 1; j <= i; j++)
    {
        if (space != 0)
        {
            if (i == 9 && j == 1)
            {
                continue;
            }
        }
 
        // printing the $
        if (i == 1 || i == 9)
        {
            cout << prt;
        }
        else if (j == 1 || j == i)
        {
            cout << prt;
        }
        else
        {
            cout <<" ";
        }
    }
}
 
// Driver code
int main()
{
    int i, nos = 0, nosp = -1, nbsp = -1;
    for (i = 9; i >= 1; (i = i - 2))
    {
 
        // printing the first upper triangle
        pattern(nos, i, 0);
 
        // printing the second upper triangle
        pattern(nosp, i, 1);
 
        // printing the third upper triangle
        pattern(nbsp, i, 1);
 
        cout << endl;
        nos++;
        nosp = nosp + 2;
        nbsp = nbsp + 2;
    }
 
    nos = 3, nosp = 5, nbsp = 5;
 
    for (i = 3; i <= 9; (i = i + 2))
    {
 
        // printing the first lower triangle
        pattern(nos, i, 0);
 
        // printing the second lower triangle
        pattern(nosp, i, 1);
 
        // printing the third lower triangle
        pattern(nbsp, i, 1);
 
        cout << endl;
        nos--;
        nosp = nosp - 2;
        nbsp = nbsp - 2;
    }
 
    return 0;
}
 
// This code is contributed by SHUBHAMSINGH10


C




// C program to print
// the Cot Bed Pattern
 
// header files
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
 
// function to print the pattern
void pattern(int nos, int i, int space)
{
    char prt = '$';
    int s, j;
 
    // loop for the spacing between the $ sign
    for (s = nos; s >= 1; s--) {
        printf("  ");
    }
 
    // skipping the $
    for (j = 1; j <= i; j++) {
        if (space != 0) {
            if (i == 9 && j == 1) {
                continue;
            }
        }
 
        // printing the $
        if (i == 1 || i == 9) {
            printf("%c", prt);
        }
        else if (j == 1 || j == i) {
            printf("%c", prt);
        }
        else {
            printf("  ");
        }
    }
}
// Driver code
int main()
{
    int i, nos = 0, nosp = -1, nbsp = -1;
    for (i = 9; i >= 1; (i = i - 2)) {
 
        // printing the first upper triangle
        pattern(nos, i, 0);
 
        // printing the second upper triangle
        pattern(nosp, i, 1);
 
        // printing the third upper triangle
        pattern(nbsp, i, 1);
 
        printf("\n");
        nos++;
        nosp = nosp + 2;
        nbsp = nbsp + 2;
    }
 
    nos = 3, nosp = 5, nbsp = 5;
 
    for (i = 3; i <= 9; (i = i + 2)) {
 
        // printing the first lower triangle
        pattern(nos, i, 0);
 
        // printing the second lower triangle
        pattern(nosp, i, 1);
 
        // printing the third lower triangle
        pattern(nbsp, i, 1);
 
        printf("\n");
        nos--;
        nosp = nosp - 2;
        nbsp = nbsp - 2;
    }
 
    return 0;
}


Java




// Java program to print
// the Cot Bed Pattern
 
public class Improve {
     
    // function to print the pattern
    static void pattern(int nos, int i, int space)
    {
        char prt = '$';
        int s, j;
       
        // loop for the spacing between the $ sign
        for (s = nos; s >= 1; s--) {
            System.out.print("  ");
        }
       
        // skipping the $
        for (j = 1; j <= i; j++) {
            if (space != 0) {
                if (i == 9 && j == 1) {
                    continue;
                }
            }
       
            // printing the $
            if (i == 1 || i == 9) {
                System.out.print(prt + "");
            }
            else if (j == 1 || j == i) {
                System.out.print(prt + "");
            }
            else {
                System.out.print("  ");
            }
        }
    }
    // Driver code 
    public static void main(String args[])
    {
           int i, nos = 0, nosp = -1, nbsp = -1;
            for (i = 9; i >= 1;  i -= 2) {
           
                // printing the first upper triangle
                pattern(nos, i, 0);
           
                // printing the second upper triangle
                pattern(nosp, i, 1);
           
                // printing the third upper triangle
                pattern(nbsp, i, 1);
           
                System.out.println();
                nos++;
                nosp = nosp + 2;
                nbsp = nbsp + 2;
            }
           
             nos = 3;
             nosp = 5;
             nbsp = 5 ;
           
            for (i = 3; i <= 9; i += 2) {
           
                // printing the first lower triangle
                pattern(nos, i, 0);
           
                // printing the second lower triangle
                pattern(nosp, i, 1);
           
                // printing the third lower triangle
                pattern(nbsp, i, 1);
           
                System.out.println();
                nos--;
                nosp = nosp - 2;
                nbsp = nbsp - 2;
            }
           
    }
    // This Code is contributed by ANKITRAI1
}


Python 3




# Python 3 program to print
# the Cot Bed Pattern
  
# function to print the pattern
def pattern(nos, i, space):
 
    prt = '$'
  
    # loop for the spacing between the $ sign
    for s in range(nos, 0, -1) :
        print(end="  ")
  
    # skipping the $
    for j in range(1, i+1) :
        if (space != 0) :
            if (i == 9 and j == 1) :
                continue
  
        # printing the $
        if (i == 1 or i == 9) :
            print(prt,end="")
         
        elif (j == 1 or j == i) :
            print(prt,end="")
         
        else :
            print(end="  ")
         
# Driver code
if __name__ == "__main__":
     
    nos = 0
    nosp = -1
    nbsp = -1
    for i in range( 9, 0 , -2) :
  
        # printing the first upper triangle
        pattern(nos, i, 0)
  
        # printing the second upper triangle
        pattern(nosp, i, 1)
  
        # printing the third upper triangle
        pattern(nbsp, i, 1)
  
        print()
        nos += 1
        nosp = nosp + 2
        nbsp = nbsp + 2
  
    nos = 3
    nosp = 5
    nbsp = 5
  
    for i in range(3, 102) :
  
        # printing the first lower triangle
        pattern(nos, i, 0)
  
        # printing the second lower triangle
        pattern(nosp, i, 1)
  
        # printing the third lower triangle
        pattern(nbsp, i, 1)
  
        print()
        nos -= 1
        nosp = nosp - 2
        nbsp = nbsp - 2
 
# This code is contributed by
# ChitraNayal


C#




// C#  program to print
// the Cot Bed Pattern
using System;
 
public class GFG{
     
    // function to print the pattern
    static void pattern(int nos, int i, int space)
    {
        char prt = '$';
        int s, j;
         
        // loop for the spacing between the $ sign
        for (s = nos; s >= 1; s--) {
            Console.Write(" ");
        }
         
        // skipping the $
        for (j = 1; j <= i; j++) {
            if (space != 0) {
                if (i == 9 && j == 1) {
                    continue;
                }
            }
         
            // printing the $
            if (i == 1 || i == 9) {
                Console.Write(prt + "");
            }
            else if (j == 1 || j == i) {
                Console.Write(prt + "");
            }
            else {
                Console.Write(" ");
            }
        }
    }
         
    // Driver code
    static public void Main (){
            int i, nos = 0, nosp = -1, nbsp = -1;
            for (i = 9; i >= 1; i -= 2) {
             
                // printing the first upper triangle
                pattern(nos, i, 0);
             
                // printing the second upper triangle
                pattern(nosp, i, 1);
             
                // printing the third upper triangle
                pattern(nbsp, i, 1);
             
                Console.WriteLine();
                nos++;
                nosp = nosp + 2;
                nbsp = nbsp + 2;
            }
             
            nos = 3;
            nosp = 5;
            nbsp = 5 ;
             
            for (i = 3; i <= 9; i += 2) {
             
                // printing the first lower triangle
                pattern(nos, i, 0);
             
                // printing the second lower triangle
                pattern(nosp, i, 1);
             
                // printing the third lower triangle
                pattern(nbsp, i, 1);
             
                Console.WriteLine();
                nos--;
                nosp = nosp - 2;
                nbsp = nbsp - 2;
            }
             
    }
    // This Code is contributed by Sachin
}


PHP




<?php
// PHP program to print the Cot
// Bed Pattern header files
 
// function to print the pattern
function pattern($nos, $i, $space)
{
    $prt = '$';
     
    // loop for the spacing between
    // the $ sign
    for ($s = $nos; $s >= 1; $s--)
    {
        echo (" ");
    }
 
    // skipping the $
    for ($j = 1; $j <= $i; $j++)
    {
        if ($space != 0)
        {
            if ($i == 9 && $j == 1)
            {
                continue;
            }
        }
 
        // printing the $
        if ($i == 1 || $i == 9)
        {
            echo $prt;
        }
        else if ($j == 1 || $j == $i)
        {
            echo $prt;
        }
        else
        {
            echo (" ");
        }
    }
}
 
// Driver code
$nos = 0;
$nosp = -1;
$nbsp = -1;
for ($i = 9; $i >= 1; ($i = $i - 2))
{
 
    // printing the first upper triangle
    pattern($nos, $i, 0);
 
    // printing the second upper triangle
    pattern($nosp, $i, 1);
 
    // printing the third upper triangle
    pattern($nbsp, $i, 1);
 
    echo ("\n");
    $nos++;
    $nosp = $nosp + 2;
    $nbsp = $nbsp + 2;
}
 
$nos = 3; $nosp = 5; $nbsp = 5;
 
for ($i = 3; $i <= 9; ($i = $i + 2))
{
 
    // printing the first lower triangle
    pattern($nos, $i, 0);
 
    // printing the second lower triangle
    pattern($nosp, $i, 1);
 
    // printing the third lower triangle
    pattern($nbsp, $i, 1);
 
    echo ("\n");
    $nos--;
    $nosp = $nosp - 2;
    $nbsp = $nbsp - 2;
}
 
// This code is contributed
// by Sach_Code
?>


Javascript




<script>
// Javascript program to print
// the Cot Bed Pattern
     
    // function to print the pattern
    function pattern(nos,i,space)
    {
        let prt = '$';
        let s, j;
         
        // loop for the spacing between the $ sign
        for (s = nos; s >= 1; s--) {
            document.write("  ");
        }
         
        // skipping the $
        for (j = 1; j <= i; j++) {
            if (space != 0) {
                if (i == 9 && j == 1) {
                    continue;
                }
            }
         
            // printing the $
            if (i == 1 || i == 9) {
                document.write(prt + "");
            }
            else if (j == 1 || j == i) {
                document.write(prt + "");
            }
            else {
                document.write("  ");
            }
        }
    }
     
    // Driver code 
    let i, nos = 0, nosp = -1, nbsp = -1;
    for (i = 9; i >= 1;  i -= 2) {
             
        // printing the first upper triangle
          pattern(nos, i, 0);
             
        // printing the second upper triangle
        pattern(nosp, i, 1);
             
        // printing the third upper triangle
        pattern(nbsp, i, 1);
             
        document.write("<br>");
        nos++;
        nosp = nosp + 2;
        nbsp = nbsp + 2;
      }
             
      nos = 3;
      nosp = 5;
      nbsp = 5 ;
         
      for (i = 3; i <= 9; i += 2) {
             
          // printing the first lower triangle
        pattern(nos, i, 0);
             
        // printing the second lower triangle
        pattern(nosp, i, 1);
             
        // printing the third lower triangle
        pattern(nbsp, i, 1);
             
        document.write("<br>");
        nos--;
        nosp = nosp - 2;
        nbsp = nbsp - 2;
      }
     
 
// This code is contributed by avanitrachhadiya2155
</script>


Output :  

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

Time complexity: O(n) for input n

Auxiliary Space: O(1)
 



Last Updated : 05 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads