Program to print the Cot Bed Pattern

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

Cot Bed Pattern:

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

Below is the code to implement the above problem:

Program :

C

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
}

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

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


My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.