0% found this document useful (0 votes)
5 views

Triangle_Patterns_CPP_Guide

The document provides a comprehensive guide to various triangle patterns in C++, including left-aligned, right-aligned, pyramids, inverted triangles, and more. Each pattern is accompanied by a visual representation and corresponding C++ code for implementation. Additionally, it offers tips for designing custom patterns by understanding shapes and utilizing loops effectively.

Uploaded by

solomontareke304
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Triangle_Patterns_CPP_Guide

The document provides a comprehensive guide to various triangle patterns in C++, including left-aligned, right-aligned, pyramids, inverted triangles, and more. Each pattern is accompanied by a visual representation and corresponding C++ code for implementation. Additionally, it offers tips for designing custom patterns by understanding shapes and utilizing loops effectively.

Uploaded by

solomontareke304
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Complete Guide to Triangle Patterns in C++

1. Left-Aligned Triangle

Pattern:
*
**
***
****

Code:
int n = 4;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j)
cout << "* ";
cout << endl;
}

2. Right-Aligned Triangle

Pattern:
*
**
***
****

Code:
int n = 4;
for (int i = 1; i <= n; ++i) {
for (int space = 1; space <= n - i; ++space)
cout << " ";
for (int star = 1; star <= i; ++star)
cout << "* ";
cout << endl;
}

3. Pyramid

Pattern:
*
***
*****
*******

Code:
int n = 4;
for (int i = 1; i <= n; ++i) {
Complete Guide to Triangle Patterns in C++

for (int space = 1; space <= n - i; ++space)


cout << " ";
for (int star = 1; star <= 2 * i - 1; ++star)
cout << "* ";
cout << endl;
}

4. Inverted Left-Aligned Triangle

Pattern:
****
***
**
*

Code:
int n = 4;
for (int i = n; i >= 1; --i) {
for (int j = 1; j <= i; ++j)
cout << "* ";
cout << endl;
}

5. Inverted Right-Aligned Triangle

Pattern:
****
***
**
*

Code:
int n = 4;
for (int i = 0; i < n; ++i) {
for (int space = 0; space < i; ++space)
cout << " ";
for (int star = 0; star < n - i; ++star)
cout << "* ";
cout << endl;
}

6. Inverted Pyramid

Pattern:
*******
Complete Guide to Triangle Patterns in C++

*****
***
*

Code:
int n = 4;
for (int i = n; i >= 1; --i) {
for (int space = 0; space < n - i; ++space)
cout << " ";
for (int star = 1; star <= 2 * i - 1; ++star)
cout << "* ";
cout << endl;
}

7. Half Pyramid with Numbers

Pattern:
1
12
123
1234

Code:
int n = 4;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j)
cout << j << " ";
cout << endl;
}

8. Floyd's Triangle

Pattern:
1
23
456
7 8 9 10

Code:
int n = 4, num = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j)
cout << num++ << " ";
cout << endl;
}
Complete Guide to Triangle Patterns in C++

9. Pascal's Triangle

Pattern:
1
1 1
1 2 1
1 3 3 1

Code:
int n = 4;
for (int i = 0; i < n; i++) {
int val = 1;
for (int space = 0; space < n - i - 1; space++)
cout << " ";
for (int j = 0; j <= i; j++) {
cout << val << " ";
val = val * (i - j) / (j + 1);
}
cout << endl;
}

10. Diamond Pattern

Pattern:
*
**
***
****
***
**
*

Code:
int n = 4;
// upper half
for (int i = 1; i <= n; ++i) {
for (int space = 1; space <= n - i; ++space)
cout << " ";
for (int star = 1; star <= 2 * i - 1; ++star)
cout << "*";
cout << endl;
}
// lower half
for (int i = n - 1; i >= 1; --i) {
for (int space = 1; space <= n - i; ++space)
cout << " ";
Complete Guide to Triangle Patterns in C++

for (int star = 1; star <= 2 * i - 1; ++star)


cout << "*";
cout << endl;
}

11. Hollow Pyramid

Pattern:
*
**
* *
*******

Code:
int n = 4;
for (int i = 1; i <= n; ++i) {
for (int space = 1; space <= n - i; ++space)
cout << " ";
for (int j = 1; j <= 2 * i - 1; ++j) {
if (j == 1 || j == 2 * i - 1 || i == n)
cout << "*";
else
cout << " ";
}
cout << endl;
}

Designing Your Own Pattern

1. Understand the shape: aligned left, right, center, etc.


2. Outer loop = number of rows.
3. Inner loop(s) = handle spaces, stars, or conditionals.
4. Test and refine using print debugging.

You might also like