• Tutorials
  • DSA
  • Data Science
  • Web Tech
  • Courses
June 28, 2022 |450 Views

CPP Program to Print Hollow Diamond Star Pattern

Description
Discussion


In this video, we will see a C++ program to Print Hollow Diamond Star Pattern.
When it comes to pattern printing we do opt for standard ways of printing them via loops only. Here we print a hollow diamond star pattern using “for loop”.

Here we see three different approaches to Print Hollow Diamond Pattern.

Algorithm 1:

1. Take input as a number of rows n.
2. Divide the entire pattern of hollow diamond into two halves - upper and lower.
3. Each part will consist of two for loops.
4. Use first for loop to print Star in rows.
5. Use the second for loop inside the first for loop to print stars in the column.
6. Put necessary conditions by using an if-else ladder to print stars and spaces respectively.

Algorithm: 2.

1. If n is odd increment n.
2. Find mid=n/2.
3. Traverse from 1 to mid to print the upper half of the pattern. Now traverse from 1 to mid-i to print spaces for the upper left-most outer box (say j).
4. Check  If (i==1) print ‘*’ (since for the first row we need only one star).
5. Otherwise print ‘*’ and traverse from 1 to 2*i-3 to print spaces for hollow diamond and print ‘*’ after the loop is over.
6. Traverse from 1 to mid-i to print spaces again for the upper right-most outer box.
7. Close the loop at step 3.
8. Traverse from mid+1 to n-1 to print lower half of the pattern. Now traverse from 1 to i-mid to print spaces for lower left most outer box..
9. If (i==n-1) print ‘*’.
10. else print ‘*’ and traverse from 1 to 2*(n-i)-3 to print spaces for hollow diamond and print ‘*’ after loop is over.
11. Traverse from 1 to i-mid to print spaces again for lower right most outer box and Close the loop.

Algorithm 3:

1. if n is odd decerment n.
2. Consider entire pattern.
3. Use for loop one inside the another.
4. Use if else ladder to put conditions to print star and spaces.

Program to print hollow diamond pattern: https://www.geeksforgeeks.org/program-print-hollow-pyramid-diamond-pattern/