Program to print hollow pyramid, diamond pattern and their modifications
For Prerequisite : Loops, If Else Statement
1. Hollow pyramid/triangle pattern
The pattern is similar to pyramid pattern. The only difference is, we will replace all internal β#β or β*β characters by space character and we will print 2*N-1 (N = number of rows in pattern) β#β or β*β characters in last row.
Examples:
Input: n=6 Output: # # # # # # # # # # # ###########
C++14
Java
Python
C#
PHP
Javascript
# # # # # # # # # # # ###########
Time complexity: O(N^2),This algorithm has a time complexity of O(N^2), where N is the number of rows. This is because we are looping through the rows and columns of the pattern, which takes O(N^2) time to complete.
Space complexity: O(1),This algorithm does not require any additional space, so its space complexity is O(1).
2. Hollow Diamond
Note: For even input, print the pattern for n-1.
Example:
Input: 1
Output:
Input: 7
Output:
Input: 9
Output:
Approach: To print diamond we need to print spaces before star and after the star to achieve constant increasing distance of stars.
To print the box shape we need to print β-β for i==1 (first row) & i==n (last row) and β|β for j==1 (first column) and j==n (last column).
Algorithm: 1. If n is odd increment n.
2. Find mid=n/2.
3. Traverse from 1 to mid to print upper half of the pattern (say i).
4. Traverse from 1 to mid-i to print spaces for upper left most outer box (say j).
5. If (i==1) print β*β (since for first row we need only one star).
6. else print β*β and traverse from 1 to 2*i-3 to print spaces for hollow diamond (say j) and print β*β after loop is over.
7. Traverse from 1 to mid-i to print spaces again for upper right most outer box (say j).
8. Close the loop at step 3.
9. Traverse from mid+1 to n-1 to print lower half of the pattern (say i).
4. Traverse from 1 to i-mid to print spaces for lower left most outer box (say j).
5. If (i==n-1) print β*β (since for last row we need only one star).
6. else print β*β and traverse from 1 to 2*(n-i)-3 to print spaces for hollow diamond (say j) and print β*β after loop is over.
7. Traverse from 1 to i-mid to print spaces again for lower right most outer box (say j).
8. Close the loop at step 9.
C++14
#include <bits/stdc++.h> using namespace std; // function to print the pattern void printPattern( int & n) { int i,j,mid; if (n%2==1) //when n is odd, increase it by 1 to make it even n++; mid = n/2; // upper half pattern for (i = 1; i<= mid; i++) { for (j = 1; j<=mid-i; j++) //print the blank spaces and outer box before star cout<< " " ; if (i == 1) { cout << "*" ; } else { cout << "*" ; //in each line star at start and end position for (j = 1; j<=2*i-3; j++) { //print space to make hollow cout << " " ; } cout << "*" ; } for (j = 1; j<=mid-i; j++) //print the blank spaces and outer box after star cout<< " " ; cout << endl; } // lower half pattern for (i = mid+1; i<n; i++) { for (j = 1; j<=i-mid; j++) //print the blank spaces and outer box before star cout<< " " ; if (i == n-1) { cout << "*" ; } else { cout << "*" ; //in each line star at start and end position for (j = 1; j<=2*(n - i)-3; j++) { //print space to make hollow cout << " " ; } cout << "*" ; } for (j = 1; j<=i-mid; j++) //print the blank spaces and outer box after star cout<< " " ; cout << endl; } } // driver's code int main() { int n=7; printPattern(n); } // this code is contributed by prophet1999 |
Java
// JAVA program class GFG{ // function to print the pattern static void printPattern( int n) { int i,j,mid; if (n% 2 == 1 ) //when n is odd, increase it by 1 to make it even n++; mid = n/ 2 ; // upper half pattern for (i = 1 ; i<= mid; i++) { for (j = 1 ; j<=mid-i; j++) //print the blank spaces and outer box before star System.out.print( " " ); if (i == 1 ) { System.out.print( "*" ); } else { System.out.print( "*" ); //in each line star at start and end position for (j = 1 ; j<= 2 *i- 3 ; j++) { //print space to make hollow System.out.print( " " ); } System.out.print( "*" ); } for (j = 1 ; j<=mid-i; j++) //print the blank spaces and outer box after star System.out.print( " " ); System.out.println(); } // lower half pattern for (i = mid+ 1 ; i<n; i++) { for (j = 1 ; j<=i-mid; j++) //print the blank spaces and outer box before star System.out.print( " " ); if (i == n- 1 ) { System.out.print( "*" ); } else { System.out.print( "*" ); //in each line star at start and end position for (j = 1 ; j<= 2 *(n - i)- 3 ; j++) { //print space to make hollow System.out.print( " " ); } System.out.print( "*" ); } for (j = 1 ; j<=i-mid; j++) //print the blank spaces and outer box after star System.out.print( " " ); System.out.println(); } } // driver's code public static void main(String args[]) { int n = 7 ; printPattern(n); } } // This code is contributed by Pushpesh Raj. |
C#
// C# program using System; public class GFG{ // function to print the pattern public static void printPattern( int n) { int i, j, mid; if (n % 2 == 1) //when n is odd, increase it by 1 to make it even n++; mid = n/2; // upper half pattern for (i = 1; i <= mid; i++) { for (j = 1; j <= mid - i; j++) //print the blank spaces and outer box before star Console.Write( " " ); if (i == 1) { Console.Write( "*" ); } else { Console.Write( "*" ); //in each line star at start and end position for (j = 1; j <= 2 * i - 3; j++) { //print space to make hollow Console.Write( " " ); } Console.Write( "*" ); } for (j = 1; j <= mid - i; j++) //print the blank spaces and outer box after star Console.Write( " " ); Console.WriteLine(); } // lower half pattern for (i = mid + 1; i < n; i++) { for (j = 1; j <= i - mid; j++) //print the blank spaces and outer box before star Console.Write( " " ); if (i == n - 1) { Console.Write( "*" ); } else { Console.Write( "*" ); //in each line star at start and end position for (j = 1; j <= 2*(n - i)-3; j++) { //print space to make hollow Console.Write( " " ); } Console.Write( "*" ); } for (j = 1; j<=i-mid; j++) //print the blank spaces and outer box after star Console.Write( " " ); Console.WriteLine(); } } // driver's code public static void Main() { int n = 7; printPattern(n); } } // This code is contributed by Aman Kumar. |
Javascript
// function to print the pattern function printPattern( n) { let i,j,mid; if (n % 2 == 1) //when n is odd, increase it by 1 to make it even n++; mid = n/2; // upper half pattern for (i = 1; i <= mid; i++) { for (j = 1; j <= mid - i; j++) //print the blank spaces and outer box before star console.log( " " ); if (i == 1) { console.log( "*" ); } else { console.log( "*" ); //in each line star at start and end position for (j = 1; j<=2*i-3; j++) { //print space to make hollow console.log( " " ); } console.log( "*" ); } for (j = 1; j<=mid-i; j++) //print the blank spaces and outer box after star console.log( " " ); console.log( "<br>" ); } // lower half pattern for (i = mid+1; i<n; i++) { for (j = 1; j<=i-mid; j++) //print the blank spaces and outer box before star console.log( " " ); if (i == n-1) { console.log( "*" ); } else { console.log( "*" ); //in each line star at start and end position for (j = 1; j<=2*(n - i)-3; j++) { //print space to make hollow console.log( " " ); } console.log( "*" ); } for (j = 1; j<=i-mid; j++) //print the blank spaces and outer box after star console.log( " " ); console.log( "<br>" ); } } // driver's code let n=7; printPattern(n); |
Python3
* * * * * * * * * * * *
Time Complexity: O(n^2) for given input n
Auxiliary Space: O(1)
3. Hollow Diamond bounded inside a rectangular box made of horizontal and vertical dashes(-).
Write a program to Print hollow diamond pattern bound inside a box made of dash(-) and bitwise-OR(|) as shown below.
Note: For even input, print the pattern for n-1.
Example:
Input: 1
Output:
Input: 7
Output:
Input: 9
Output:
Approach: To print diamond we need to print spaces before star and after the star to achieve constant increasing distance of stars.
To print the box shape we need to print β-β for i==1 (first row) & i==n (last row) and β|β for j==1 (first column) and j==n (last column).
Algorithm: 1. If n is odd increment n.
2. Find mid=n/2.
3. Traverse from 1 to mid to print upper half of the pattern (say i).
4. Traverse from 1 to mid-i to print upper left most outer box (say j).
5. If (i==1) print β*β (since for first row we need only one star).
6. else print β*β and traverse from 1 to 2*i-3 to print spaces for hollow diamond (say j) and print β*β after loop is over.
7. Traverse from 1 to mid-i to print upper right most outer box (say j).
8. Close the loop at step 3.
9. Traverse from mid+1 to n-1 to print lower half of the pattern (say i).
4. Traverse from 1 to i-mid to print lower left most outer box (say j).
5. If (i==n-1) print β*β (since for last row we need only one star).
6. else print β*β and traverse from 1 to 2*(n-i)-3 to print spaces for hollow diamond (say j) and print β*β after loop is over.
7. Traverse from 1 to i-mid to print lower right most outer box (say j).
8. Close the loop at step 9.
C++14
#include <bits/stdc++.h> using namespace std; // function to print the pattern void printPattern( int & n) { int i,j,mid; if (n%2==1) //when n is odd, increase it by 1 to make it even n++; mid = n/2; // upper half pattern for (i = 1; i<= mid; i++) { for (j = 1; j<=mid-i; j++) { //print the blank spaces and outer box before star if (i==1) cout<< "-" ; else if (j==1) cout << "|" ; else cout<< " " ; } if (i == 1) { cout << "*" ; } else { cout << "*" ; //in each line star at start and end position for (j = 1; j<=2*i-3; j++) { //print space to make hollow cout << " " ; } cout << "*" ; } for (j = 1; j<=mid-i; j++) { //print the blank spaces and outer box after star if (i==1) cout<< "-" ; else if (j==mid-i) cout << "|" ; else cout<< " " ; } cout << endl; } // lower half pattern for (i = mid+1; i<n; i++) { for (j = 1; j<=i-mid; j++) { //print the blank spaces and outer box before star if (i==n-1) cout<< "-" ; else if (j==1) cout << "|" ; else cout<< " " ; } if (i == n-1) { cout << "*" ; } else { cout << "*" ; //in each line star at start and end position for (j = 1; j<=2*(n - i)-3; j++) { //print space to make hollow cout << " " ; } cout << "*" ; } for (j = 1; j<=i-mid; j++) { //print the blank spaces and outer box after star if (i==n-1) cout<< "-" ; else if (j==i-mid) cout << "|" ; else cout<< " " ; } cout << endl; } } // driver's code int main() { int n=12; printPattern(n); } // this code is contributed by prophet1999 |
Java
// Java code to implement the above approach import java.io.*; import java.util.*; class GFG { // function to print the pattern public static void printPattern( int n) { int i,j,mid; if (n% 2 == 1 ) //when n is odd, increase it by 1 to make it even n++; mid = n/ 2 ; // upper half pattern for (i = 1 ; i<= mid; i++) { for (j = 1 ; j<=mid-i; j++) { //print the blank spaces and outer box before star if (i== 1 ) System.out.print( "-" ); else if (j== 1 ) System.out.print( "|" ); else System.out.print( " " ); } if (i == 1 ) { System.out.print( "*" ); } else { System.out.print( "*" ); //in each line star at start and end position for (j = 1 ; j<= 2 *i- 3 ; j++) { //print space to make hollow System.out.print( " " ); } System.out.print( "*" ); } for (j = 1 ; j<=mid-i; j++) { //print the blank spaces and outer box after star if (i== 1 ) System.out.print( "-" ); else if (j==mid-i) System.out.print( "|" ); else System.out.print( " " ); } System.out.print( "\n" ); } // lower half pattern for (i = mid+ 1 ; i<n; i++) { for (j = 1 ; j<=i-mid; j++) { //print the blank spaces and outer box before star if (i==n- 1 ) System.out.print( "-" ); else if (j== 1 ) System.out.print( "|" ); else System.out.print( " " ); } if (i == n- 1 ) { System.out.print( "*" ); } else { System.out.print( "*" ); //in each line star at start and end position for (j = 1 ; j<= 2 *(n - i)- 3 ; j++) { //print space to make hollow System.out.print( " " ); } System.out.print( "*" ); } for (j = 1 ; j<=i-mid; j++) { //print the blank spaces and outer box after star if (i==n- 1 ) System.out.print( "-" ); else if (j==i-mid) System.out.print( "|" ); else System.out.print( " " ); } System.out.print( "\n" ); } } // Driver Code public static void main(String[] args) { int n= 12 ; printPattern(n); } } //this code is contributed by adityapatil12 |
Python3
# function to print the pattern def printPattern(n): if n % 2 = = 1 : # when n is odd, increase it by 1 to make it even n + = 1 mid = n / / 2 # upper half pattern for i in range ( 1 , mid + 1 ): for j in range ( 1 , mid - i + 1 ): # print the blank spaces and outer box before star if i = = 1 : print ( "-" , end = "") elif j = = 1 : print ( "|" , end = "") else : print ( " " , end = "") if i = = 1 : print ( "*" , end = "") else : print ( "*" , end = "") # in each line star at start and end position for j in range ( 1 , 2 * i - 2 ): # print space to make hollow print ( " " , end = "") print ( "*" , end = "") for j in range ( 1 , mid - i + 1 ): # print the blank spaces and outer box after star if i = = 1 : print ( "-" , end = "") elif j = = mid - i: print ( "|" , end = "") else : print ( " " , end = "") print () # lower half pattern for i in range (mid + 1 , n): for j in range ( 1 , i - mid + 1 ): # print the blank spaces and outer box before star if i = = n - 1 : print ( "-" , end = "") elif j = = 1 : print ( "|" , end = "") else : print ( " " , end = "") if i = = n - 1 : print ( "*" , end = "") else : print ( "*" , end = "") # in each line star at start and end position for j in range ( 1 , 2 * (n - i) - 2 ): # print space to make hollow print ( " " , end = "") print ( "*" , end = "") for j in range ( 1 , i - mid + 1 ): # print the blank spaces and outer box after star if i = = n - 1 : print ( "-" , end = "") elif j = = i - mid: print ( "|" , end = "") else : print ( " " , end = "") print () # driver's code n = 12 printPattern(n) # This code is contributed by prasad264 |
C#
using System; namespace Pattern { class Program { static void PrintPattern( ref int n) { int i, j, mid; if (n % 2 == 1) //when n is odd, increase it by 1 to make it even n++; mid = n / 2; // upper half pattern for (i = 1; i <= mid; i++) { for (j = 1; j <= mid - i; j++) { //print the blank spaces and outer box before star if (i == 1) Console.Write( "-" ); else if (j == 1) Console.Write( "|" ); else Console.Write( " " ); } if (i == 1) { Console.Write( "*" ); } else { Console.Write( "*" ); //in each line star at start and end position for (j = 1; j <= 2 * i - 3; j++) { //print space to make hollow Console.Write( " " ); } Console.Write( "*" ); } for (j = 1; j <= mid - i; j++) { //print the blank spaces and outer box after star if (i == 1) Console.Write( "-" ); else if (j == mid - i) Console.Write( "|" ); else Console.Write( " " ); } Console.WriteLine(); } // lower half pattern for (i = mid + 1; i < n; i++) { for (j = 1; j <= i - mid; j++) { //print the blank spaces and outer box before star if (i == n - 1) Console.Write( "-" ); else if (j == 1) Console.Write( "|" ); else Console.Write( " " ); } if (i == n - 1) { Console.Write( "*" ); } else { Console.Write( "*" ); //in each line star at start and end position for (j = 1; j <= 2 * (n - i) - 3; j++) { //print space to make hollow Console.Write( " " ); } Console.Write( "*" ); } for (j = 1; j <= i - mid; j++) { //print the blank spaces and outer box after star if (i == n - 1) Console.Write( "-" ); else if (j == i - mid) Console.Write( "|" ); else Console.Write( " " ); } Console.WriteLine(); } } static void Main( string [] args) { int n = 12; PrintPattern( ref n); } } } // This code is contributed by factworx412 |
Javascript
// JavaScript code to implement the above approach // function to print the pattern function printPattern(n) { let i, j, mid; if (n % 2 === 1) { // when n is odd, increase it by 1 to make it even n++; } mid = n / 2; // upper half pattern for (i = 1; i <= mid; i++) { let line = "" ; for (j = 1; j <= mid - i; j++) { // print the blank spaces and outer box before star if (i === 1) { line += "-" ; } else if (j === 1) { line += "|" ; } else { line += " " ; } } if (i === 1) { line += "*" ; } else { line += "*" ; // in each line star at start and end position for (j = 1; j <= 2 * i - 3; j++) { // print space to make hollow line += " " ; } line += "*" ; } for (j = 1; j <= mid - i; j++) { // print the blank spaces and outer box after star if (i === 1) { line += "-" ; } else if (j === mid - i) { line += "|" ; } else { line += " " ; } } console.log(line); } // lower half pattern for (i = mid + 1; i < n; i++) { let line = "" ; for (j = 1; j <= i - mid; j++) { // print the blank spaces and outer box before star if (i === n - 1) { line += "-" ; } else if (j === 1) { line += "|" ; } else { line += " " ; } } if (i === n - 1) { line += "*" ; } else { line += "*" ; // in each line star at start and end position for (j = 1; j <= 2 * (n - i) - 3; j++) { // print space to make hollow line += " " ; } line += "*" ; } for (j = 1; j <= i - mid; j++) { // print the blank spaces and outer box after star if (i === n - 1) { line += "-" ; } else if (j === i - mid) { line += "|" ; } else { line += " " ; } } console.log(line); } } // Driver Code let n = 12; printPattern(n); |
-----*----- | * * | | * * | | * * | |* *| * * |* *| | * * | | * * | | * * | -----*-----
Time Complexity: O(n*n)
Auxiliary Space: O(1)
Contact Us