C++ Program To Print Triangle Pattern
Last Updated :
06 Oct, 2023
Here we will see how to print triangle patterns using a C++ program. There are 4 patterns discussed here:
- Right Triangle.
- Inverted Right Triangle.
- Equilateral Triangle.
- Inverted Equilateral Triangle.
- Inverted Mirrored Right Triangle.
Let’s start discussing each of these in detail.
1. Right Triangle
Below are the examples for the right triangle:
Input: 4
Output:
*
* *
* * *
* * * *
Input: 5
Output:
*
* *
* * *
* * * *
* * * * *
In the above pattern, it can be observed that
ith row has i elements
Below is the C++ program to print the right triangle:
C++
#include <iostream>
using namespace std;
int main()
{
int n = 5;
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= i; j++)
cout << "* " ;
cout << endl;
}
return 0;
}
|
Output
*
* *
* * *
* * * *
* * * * *
Time Complexity: O(n^2)
Auxiliary Space: O(1)
2. Inverted Right Triangle
Below are the examples of inverted right triangles:
Input: 5
Output:
* * * * *
* * * *
* * *
* *
*
Input: 6
Output:
* * * * * *
* * * * *
* * * *
* * *
* *
*
In this example, it can be observed that if there are a total of n rows in the triangle then:
ith row has n-i+1 elements
Below is the C++ program to print an inverted right triangle:
C++
#include <iostream>
using namespace std;
int main()
{
int n = 5;
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= n - i + 1; j++)
cout << "* " ;
cout << endl;
}
return 0;
}
|
Output
* * * * *
* * * *
* * *
* *
*
Time Complexity: O(n^2)
Auxiliary Space: O(1)
Another Approach:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n = 5;
for ( int i = n; i >= 1; i--) {
for ( int j = i; j >= 1; j--)
cout << "* " ;
cout << endl;
}
return 0;
}
|
Output
* * * * *
* * * *
* * *
* *
*
Time Complexity: O(n^2)
Auxiliary Space: O(1)
3. Equilateral Triangle
Below are examples of an equilateral triangle:
Input: 5
Output:
*
* *
* * *
* * * *
* * * * *
Input: 6
Output:
*
* *
* * *
* * * *
* * * * *
* * * * * *
In this example, it can be observed that if there are n rows in the pattern then:
ith row has i elements and has (n – i) leading spaces
Below is the C++ program to print an equilateral triangle:
C++
#include <iostream>
using namespace std;
int main()
{
int n = 5;
for ( int i = 1; i <= n; i++) {
for ( int j = 0; j < n - i; j++)
cout << " " ;
for ( int j = 1; j <= i; j++)
cout << "* " ;
cout << endl;
}
return 0;
}
|
Output
*
* *
* * *
* * * *
* * * * *
Time Complexity: O(n^2)
Auxiliary Space: O(1)
4. Inverted Equilateral Triangle
Below are examples of an inverted equilateral triangle:
Input: 5
Output:
* * * * *
* * * *
* * *
* *
*
Input: 6
Output:
* * * * * *
* * * * *
* * * *
* * *
* *
*
It can be observed if there are n rows in the pattern then:
ith row has n-i+1 elements and i-1 leading spaces
Below is the C++ program to print an inverted equilateral triangle:
C++
#include <iostream>
using namespace std;
int main()
{
int n = 5;
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j < i; j++)
cout << " " ;
for ( int j = 1; j <= n - i + 1; j++)
cout << "* " ;
cout << endl;
}
return 0;
}
|
Output
* * * * *
* * * *
* * *
* *
*
Time Complexity: O(n^2)
Auxiliary Space: O(1)
Another Approach:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n = 5;
for ( int i = n; i >= 1; i--) {
for ( int j = 0; j <= n - i; j++)
cout << " " ;
for ( int j = 1; j <= i; j++)
cout << "* " ;
cout << endl;
}
return 0;
}
|
Output
* * * * *
* * * *
* * *
* *
*
Time Complexity: O(n^2)
Auxiliary Space: O(1)
5. Inverted Mirrored Right Triangle
Below are the examples of inverted mirrored right triangles:
Input: 5
Output:
* * * * *
* * * *
* * *
* *
*
Input: 6
Output:
* * * * * *
* * * * *
* * * *
* * *
* *
*
It can be observed if there are n rows in the pattern then:
ith row has n-i+1 elements and 2*(i-1) leading spaces
Below is the C++ program to print an inverted mirrored right triangle:
C++
#include <iostream>
using namespace std;
int main()
{
int n = 5;
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j < i; j++)
cout << " " ;
for ( int j = 1; j <= n - i + 1; j++)
cout << "* " ;
cout << endl;
}
return 0;
}
|
Output
* * * * *
* * * *
* * *
* *
*
Time Complexity: O(n^2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...