Home  >  Article  >  Backend Development  >  C++ program to print left triangle star pattern

C++ program to print left triangle star pattern

WBOY
WBOYforward
2023-08-29 10:25:051090browse

C++ program to print left triangle star pattern

Star patterns are interesting problems showing different shapes such as right triangles

Or use the asterisk '*' to create other triangle and rhombus shapes. These shapes are called stars In this article we will see how to display left triangle star pattern in C We take as input the number of rows of the star pattern. It will print out the corresponding pattern

Rows.

We will develop logic to print asterisks in the table below. Let's follow the form Better understanding −
*
* *
* * *
* * * *
* * * * *
* * * * * *

In this example, there are 6 rows. So consider n = 6. For each row 'i' it will follow the asterisk count

Line number (i) Star Count (j)
1 1
2 2
3 3
4 4
5 5
6 6

When on any row 'i', j follows i, then there are 'i' stars on that row. Let's take a look

The algorithm is: algorithm for this −

algorithm

  • Read the number of lines as input n
  • for i ranging from 1 to n, do
    • For j from 1 to i, perform the following operations
      • Display asterisk (*)
    • end for
    • Move the cursor to the next line
  • end for
The Chinese translation of

Example

is:

Example

#include <iostream>
#include <ctype.h>
using namespace std;
void solve( int n ){
   int i, j;
   for( i = 1; i <= n; i++ ) {
      for( j = 1; j <= i; j++ ) {
         cout << "* ";
      }
      cout << endl;
   }
}
int main(){
   int n = 10;
   cout << "Left Star Pattern using " << n << " number of lines:" << endl;
   solve( n );
}

Output

Left Star Pattern using 10 number of lines:
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 
* * * * * * * * 
* * * * * * * * * 
* * * * * * * * * * 

Output (n = 18)

Left Star Pattern using 18 number of lines:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *

in conclusion

When learning programming, displaying star schemas can help understand nested loops.

any language. In this article we have seen how to display a left triangle using an asterisk (stars) Enter the number of rows and it will display the number of rows There are the same number of stars in each row. We also discussed a tabulation approach to achieve Specify the number of stars for row i. Using this idea we can simply change Determine the number of stars in row i. Using this idea we can simply change

Formulas can display other types of patterns.

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

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete