
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Print a Triangle Star Pattern
When we start learning to code, practicing star patterns is one of the best ways to improve logic building. One of the engaging patterns is the triangle pattern. In this article, we are going to learn how to print a Triangle Star Pattern using C++.
What is a Triangle Pattern?
A triangle pattern is a geometrical arrangement of stars that forms the shape of a triangle. The stars are printed so that if we visualize the pattern as a closed figure, it resembles a triangle. Each row has a different number of stars, increasing as we move downward. The alignment of stars is centered, forming a symmetrical triangular shape.
The triangle pattern looks like this:
* *** ***** ******* *********
Using Centered Star Printing Approach
We will first enter the number of rows we want to print. To create the triangle pattern, spaces are added before stars in each row to maintain the symmetry of the triangle. First, print spaces before the stars to make it centered. Now print the stars for that row. Repeat for each row. After printing the stars for one row, move to the next row and repeat the process. This will gradually decrease the number of spaces and increase the number of stars.
Steps for Implementation
- Step 1 : Input the number of rows from the user.
- Step 2 : Use nested loops to print the triangle pattern.
- Step 3 : The outer loop iterates through the rows.
- Step 4 : The first inner loop prints decreasing spaces for each row to center the stars.
- Step 5 : The second inner loop prints an increasing number of stars in each row.
- Step 6 : Move to the next line after printing each row and repeat until the specified number of rows is reached.
Implementation Code
include<bits/stdc++.h> using namespace std; int main() { int rows; cout << "Enter the number of rows: "; cin >> rows; for (int i = 0; i < rows; i++) { // Print spaces for (int j = 0; j < rows - i - 1; j++) { cout << " "; } // Print stars for (int j = 0; j < 2 * i + 1; j++) { cout << "*"; } cout << endl; } return 0; }
Output
The above code produces the following result ?
Enter the number of rows: 5 * *** ***** ******* *********
Time Complexity
O(n2)
Space Complexity
O(1)
Using String Concatenation Approach
Another way to print the triangle pattern is by using the string method. A nested loop is used, where the outer loop iterates through each row. The first inner loop initializes a string object with spaces (rows - i - 1) for centering the stars. The second inner loop is for creating a string with stars. We concatenate the spaces and stars, print the result, and repeat for each row.
Steps for Implementation
- Step 1 : Take the input for the number of rows.
- Step 2 : Use a nested loop where the outer loop iterates through each row.
- Step 3 : Create a string of spaces for the current row, decreasing as we move to the next row.
- Step 4 : Create a string of stars for the current row, increasing as we move downward.
- Step 5 : Print the concatenated string of spaces and stars.
Implementation Code
#include<bits/stdc++.h> using namespace std; int main() { int rows = 5; for (int i = 0; i < rows; i++) { string spaces(rows - i - 1, ' '); string stars(2 * i + 1, '*'); cout << spaces + stars << endl; } return 0; }
Output
The above code produces the following result ?
* *** ***** ******* *********
Time Complexity
O(n2)
Space Complexity
O(n)
By understanding and implementing these approaches, you can efficiently create a triangle star pattern and practice improving your logical skills in C++.