Triangle_Patterns_in_CPP
Triangle_Patterns_in_CPP
Theory
This triangle has its left side aligned. For n rows:
- Row 1 → 1 star
- Row 2 → 2 stars
- ...
- Row n → n stars
Code
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number of rows: ";
cin >> n;
return 0;
}
Example Output (n = 5)
*
**
***
****
*****
Theory
This is aligned to the right side. For this:
- First print spaces, then stars.
- Spaces decrease as rows increase.
Code
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number of rows: ";
cin >> n;
return 0;
}
Example Output (n = 5)
*
**
***
****
*****
4. Pyramid (Centered Triangle)
Theory
- This pattern is symmetrical.
- Each row has:
- Decreasing spaces on the left.
- Increasing stars (odd numbers: 1, 3, 5…).
Code
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number of rows: ";
cin >> n;
return 0;
}
Example Output (n = 5)
*
***
*****
*******
*********
Theory
- Only borders of the triangle are shown using `*`.
- Use `if` statements inside inner loop.
Code
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number of rows: ";
cin >> n;
return 0;
}
Example Output (n = 5)
*
**
* *
* *
*****