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++ program to print // right triangle #include <iostream> using namespace std;
// Driver code int main()
{ int n = 5;
// ith row has i elements
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= i; j++)
cout << "* " ;
cout << endl;
}
return 0;
} |
* * * * * * * * * * * * * * *
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++ program to print inverted // right triangle #include <iostream> using namespace std;
// Driver code int main()
{ int n = 5;
// ith row has n-i+1 elements
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= n - i + 1; j++)
cout << "* " ;
cout << endl;
}
return 0;
} |
* * * * * * * * * * * * * * *
Time Complexity: O(n^2)
Auxiliary Space: O(1)
Another Approach:
// Another C++ Approach for printing // Inverted Right Triangle #include <bits/stdc++.h> using namespace std;
// similar to right triangle's code int main()
{ int n = 5;
// start printing from the last row, i.e., nth row
for ( int i = n; i >= 1; i--) {
for ( int j = i; j >= 1; j--)
cout << "* " ;
cout << endl;
}
return 0;
} |
* * * * * * * * * * * * * * *
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++ program to print // equilateral triangle #include <iostream> using namespace std;
// Driver code int main()
{ int n = 5;
// ith row has n-i leading spaces
// and i elements
for ( int i = 1; i <= n; i++) {
// n-i leading spaces
for ( int j = 0; j < n - i; j++)
cout << " " ;
// i elements
for ( int j = 1; j <= i; j++)
cout << "* " ;
cout << endl;
}
return 0;
} |
* * * * * * * * * * * * * * *
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++ program to print an // inverted equilateral triangle #include <iostream> using namespace std;
// Driver code int main()
{ int n = 5;
// ith row has n-i+1 elements
for ( int i = 1; i <= n; i++) {
// leading spaces
for ( int j = 1; j < i; j++)
cout << " " ;
for ( int j = 1; j <= n - i + 1; j++)
cout << "* " ;
cout << endl;
}
return 0;
} |
* * * * * * * * * * * * * * *
Time Complexity: O(n^2)
Auxiliary Space: O(1)
Another Approach:
#include <bits/stdc++.h> using namespace std;
// similar to the equilateral triangle code int main()
{ int n = 5;
// start printing from the last row, i.e., nth row
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;
} |
* * * * * * * * * * * * * * *
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++ program to print a // inverted mirrored right triangle #include <iostream> using namespace std;
// Driver code int main()
{ int n = 5;
// ith row has n-i+1 elements
for ( int i = 1; i <= n; i++) {
// leading spaces
for ( int j = 1; j < i; j++)
cout << " " ;
for ( int j = 1; j <= n - i + 1; j++)
cout << "* " ;
cout << endl;
}
return 0;
} |
* * * * * * * * * * * * * * *
Time Complexity: O(n^2)
Auxiliary Space: O(1)