Home > Backend Development > C++ > body text

C++ program to print left triangle star pattern

WBOY
Release: 2023-08-29 10:25:05
forward
1092 people have browsed it

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 −
*
* *
* * *
* * * *
* * * * *
* * * * * *
Copy after login

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 );
}
Copy after login

Output

Left Star Pattern using 10 number of lines:
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 
* * * * * * * * 
* * * * * * * * * 
* * * * * * * * * * 
Copy after login

Output (n = 18)

Left Star Pattern using 18 number of lines:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
Copy after login

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!

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