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
// 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
filter_none
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 } |
chevron_right
filter_none
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, 10, 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) print() nos -= 1 nosp = nosp - 2 nbsp = nbsp - 2 # This code is contributed by # ChitraNayal |
chevron_right
filter_none
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 } |
chevron_right
filter_none
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 ?> |
chevron_right
filter_none
Output :
$$$$$$$$$$$$$$$$$$$$$$$$$
$ $ $ $ $ $
$ $ $ $ $ $
$ $ $ $ $ $
$ $ $
$ $ $ $ $ $
$ $ $ $ $ $
$ $ $ $ $ $
$$$$$$$$$$$$$$$$$$$$$$$$$
Recommended Posts:
- Create Directory or Folder with C/C++ Program
- C Program to Swap two Numbers
- C Program for Bubble Sort on Linked List
- C program to invert (making negative) an image content in PGM format
- C Program for Selection Sort
- C Program for Bubble Sort
- C Program for Insertion Sort
- C Program for Radix Sort
- C/C++ Program for n-th Fibonacci number
- C Program for Counting Sort
- C Program for Binary Search (Recursive and Iterative)
- C Program for Min Cost Path
- C/C++ Program for Longest Increasing Subsequence
- C Program for Pancake sorting
- C Program for Binary Insertion Sort
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.



