Home > Backend Development > C++ > body text

C++ program to print hollow right triangular star pattern

PHPz
Release: 2023-08-30 11:01:04
forward
1305 people have browsed it

C++ program to print hollow right triangular star pattern

It is useful to display star patterns in different formats such as pyramid, square and diamond Commonly used in basic programming and logic construction. How many stars have we seen Number pattern issues when learning loop statements in programming. in the text, We will see how to print a hollow right triangle star pattern in C.

In this program we take row number n which will create a star pattern for n numbers Wire. Let's look at the same example.

Right hollow star pattern

            *
          * *
        *   *
      *     *
    *       *
  *         *
* * * * * * *
Copy after login

In this example, there are n rows. Each row has several stars, and at the beginning there are is a space. There are only two stars in the row except the first and last row, and There are spaces in between. The first row contains only one star and the last row Contains n stars.

Each line has n – i spaces, and then prints stars. For complete right-aligned triangles, we print the stars continuously. But here we print For the first and last positions, spaces are printed to make them hollow otherwise. in the final OK, it will print all the stars to make the border.

algorithm

  • Read n as input
  • For i from 1 to n, execute
    • For j ranging from 1 to n - i, do the following
      • Enter a space
    • Finish
    • For k from 1 to i, execute
      • If i is different from n, then
        • If k = 1 or k = i, then
          • show"*"
        • otherwise
          • Display blank
        • If it ends
      • otherwise
        • show"*"
      • If it ends
    • Finish
    • Move the cursor to the next line
  • Finish

Due to some limitations of the online compiler, we added dots instead of spaces at the code. start. Otherwise, it will sometimes truncate the line and the output will become incorrect.

Example

#include <iostream>
using namespace std;
void solve( int n ){
   for ( int i = 1; i <= n; i++ ) {
      for ( int j = 1; j <= n - i; j++ ) {
         cout << ".";
      }
      for ( int k = 1; k <= i; k++ ) {
         if ( i != n ) {
            if ( k == 1 || k == i ) {
               cout << "*";
            } else {
               cout << " ";
            }
         } else {
            cout << "*";
         }
      }
      cout << "\n";
   }
}
int main(){
   int n = 7;
   cout << "Hollow Star Pattern for " << n << " lines." << endl;
   solve( n );
}
Copy after login

Output

Hollow Star Pattern for 7 lines.
......*
.....**
....* *
...*  *
..*   *
.*    *
*******
Copy after login

Output (n = 18)

Hollow Star Pattern for 18 lines.
.................*
................**
...............* *
..............*  *
.............*   *
............*    *
...........*     *
..........*      *
.........*       *
........*        *
.......*         *
......*          *
.....*           *
....*            *
...*             *
..*              *
.*               *
******************
Copy after login

in conclusion

The star pattern is easy to implement and helps to study the concept of loops in programming. In some articles we have seen techniques for printing star patterns such as pyramids, boxes etc. diamond. In this article we learned how to display a hollow pyramid in C. it takes Row number n, then displays a right-aligned hollow triangular star pattern. Each line has After a few spaces there are a few stars. Spaces are used for alignment (due to limitations For the online compiler, we use dots instead of spaces for alignment). For the last line, it Prints all stars but for other rows it prints stars only at first and last row position Pillar. This creates a hollow triangle.

The above is the detailed content of C++ program to print hollow right triangular star pattern. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!