The Wayback Machine - https://web.archive.org/web/20241123001324/https://www.geeksforgeeks.org/cpp-program-to-print-triangle-pattern/
Open In App

C++ Program To Print Triangle Pattern

Last Updated : 06 Oct, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Here we will see how to print triangle patterns using a C++ program. There are 4 patterns discussed here:

  1. Right Triangle.
  2. Inverted Right Triangle.
  3. Equilateral Triangle.
  4. Inverted Equilateral Triangle.
  5. 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++




// 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;
}


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++




// 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;
}


Output

* * * * * 
* * * * 
* * * 
* * 
* 

Time Complexity: O(n^2)
Auxiliary Space: O(1)

Another Approach:

C++




// 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;
}


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++




// 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;
}


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++




// 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;
}


Output

* * * * * 
 * * * * 
  * * * 
   * * 
    * 

Time Complexity: O(n^2)
Auxiliary Space: O(1)

Another Approach:

C++




#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;
}


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++




// 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;
}


Output

* * * * * 
  * * * * 
    * * * 
      * * 
        * 

Time Complexity: O(n^2)
Auxiliary Space: O(1)



Previous Article
Next Article

Similar Reads

Program to print a Hollow Triangle inside a Triangle
Given a number N(? 8), the task is to print a Hollow Triangle inside a Triangle pattern.Example: Input: N = 9 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Approach: Let i be the index for rows and j be the index for columns. Then: For sides of outer triangle: If the index of column(j) is equals to (N - i + 1) or (
7 min read
Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle
Given here is a right angle triangle with height l, base b & hypotenuse h, which inscribes a square which in turn inscribes a reuleaux triangle. The task is to find the maximum possible area of this reuleaux triangle.Examples: Input: l = 5, b = 12, h = 13 Output: 8.77914 Input: l = 3, b = 4, h = 5 Output: 2.07116 Approach: We know, the side of
5 min read
Biggest Reuleaux Triangle inscribed within a Square inscribed in an equilateral triangle
Given here is an equilateral triangle of sidelength a which inscribes a square which in turn inscribes a reuleaux triangle. The task is to find the maximum possible area of this reuleaux triangle.Examples: Input : a = 5 Output : 3.79335 Input : a = 9 Output : 12.2905 Approach: We know that the side of the square inscribed within an equilateral tria
4 min read
C++ Program To Print Reverse Floyd's Triangle
Floyd’s triangle is a triangle with first natural numbers. Task is to print reverse of Floyd’s triangle.Examples: Input: 4 Output: 10 9 8 7 6 5 4 3 2 1 Input: 5 Output: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 C/C++ Code // C++ program to print reverse // of Floyd's triangle #include <bits/stdc++.h> using namespace std; void printReverseFloyd(int
1 min read
C++ Program To Print Floyd's Triangle
Floyd's triangle is a triangular array of natural numbers and it is named after Robert Floyd, a renowned computer scientist popularly known for the design of the Floyd–Warshall algorithm. Here, we will see how to print Floyd's pattern triangle pyramid using the C++ program. Below are the examples: Input: row = 5Output:1 2 3 4 5 6 7 8 9 10 11 12 13
4 min read
Program to print half diamond Number-Star pattern
Given a number N which represents the number of rows. The task is to print a half diamond Number-Star pattern as shown in the below examples.Note: N is always an even number.Examples: Input: N = 4 Output: 2*2 1 1 2*2 Input: N = 6 Output: 3*3*3 2*2 1 1 2*2 3*3*3 On carefully observing the above pattern, it can be broken down into two different trian
6 min read
Program to print the given Z Pattern
Given an integer N, the task is to print the Alphabet Z Pattern as given below: 1 2 3 * * * N * * * 3 2 1 2 3 * * * N Examples: Input: N = 5 Output: 1 2 3 4 5 4 3 2 1 2 3 4 5 Input: N = 4 Output: 1 2 3 4 3 2 1 2 3 4 Recommended: Please try your approach on {IDE} first, before moving on to the solution. Approach: Print the first row with 1 to N numb
7 min read
Program to print Step Pattern
The program must accept a string S and an integer N as the input. The program must print the desired pattern as shown below: Examples: Input: string = "abcdefghijk", n = 3 Output: a *b **c *d e *f **g *h i *j **k Explanation: Here N is 3. The highest possible height of the string pattern must be 3. Start from the height as 1. Increment till the val
7 min read
Program to print half Diamond star pattern
Given an integer N, the task is to print half-diamond-star pattern. ************************************ Examples: Input: N = 3 Output: * ** *** ** * Input: N = 6 Output: * ** *** **** ***** ****** ***** **** *** ** * Approach: The idea is to break the pattern into two halves that is upper half and lower half. Then print them separately with the he
4 min read
Program to print the pattern 1020304017018019020 **50607014015016 ****809012013 ******10011...
Given an integer N, the task is to print the pattern below for the given value of N. For N = 5, below is the given pattern: Examples: Input: N = 4 Output: 1020304017018019020 **50607014015016 ****809012013 ******10011 Input: N = 3 Output: 10203010011012 **4050809 ****607 Approach: The idea to understand the logic behind the given pattern is stated
9 min read
C++ Program To Print Number Pattern
Here, we will see a C++ program to print the 3 different number patterns. There are 3 number patterns covered using for loop and while loop with their respective explanation.3 Different Number Patterns: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 1 12 123 1234 12345Pattern 1:Input: n = 5 Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 1
6 min read
C++ Program To Print Character Pattern
Here we will build a C++ Program To Print Character patterns using 2 Approaches i.e. Using for loopUsing while loop Printing 1 character pattern in C++ using different approaches. 1. Using for loop Input: rows = 5 Output: A B B C C C D D D D E E E E E Approach 1: Assign any character to one variable for the printing pattern. The first for loop is u
5 min read
C++ Program To Print Continuous Character Pattern
Here we will build a C++ Program To Print Continuous Character patterns using 2 different methods i.e: Using for loopsUsing while loopsInput: rows = 5Output: A B C D E F G H I J K L M N O 1. Using for loopApproach 1: Assign any character to one variable for the printing pattern. The first for loop is used to iterate the number of rows and the secon
5 min read
C++ Program To Print Left Half Pyramid Pattern
Here, we will build a C++ program to print the left half of pyramid pattern using 2 approaches i.e. Using for loopUsing while loop1. Using for loop Input: rows = 5 Output: * ** *** **** ***** First, for loop is used to identify the number of rows and the second for loop is used to identify the number of columns. Here the values will be changed acco
3 min read
C++ Program To Print Inverted Hollow Star Pyramid Pattern
Given the value of R(number of rows), write a C++ program to print the Inverted Hollow Pyramid using stars and white spaces. Examples: Input: R = 5 Output: ********* * * * * * * * Input: R = 10 Output: ******************* * * * * * * * * * * * * * * * * * Algorithm: At first, take the number of rows as input.The next step is to implement the nested
2 min read
C++ Program To Print Right Half Pyramid Pattern
Here we will build a C++ Program To Print Right Half Pyramid Pattern with the following 2 approaches: Using for loop Using while loop Input: rows = 5 Output: * * * * * * * * * * * * * * * 1. Using for loop First for loop is used to identify the number of rows and the second for loop is used to identify the number of columns. Here the values will be
2 min read
C++ Program To Print Hollow Star Pyramid Diamond Shape Pattern
Here, we will build a C++ program to print the hollow star pyramid diamond shape pattern that can be achieved with two approaches i.e. Using for LoopUsing while loop Input: n = 5 Output: * * * * * * * * * * * * * * * *1. Using for loop C/C++ Code // C++ program to print hollow diamond pattern #include <iostream> using namespace std; int main(
3 min read
C++ Program to Print the Pattern 'G"
In this article, we will learn how to print the pattern G using stars and white spaces. Given a number n, we will write a program to print the pattern G over n lines or rows.Examples: Input : 7 Output : *** * * * *** * * * * *** Input : 9 Output : ***** * * * * *** * * * * * * ***** In this program, we have used the simple logic of iteration over l
2 min read
C++ Program to Print Cross or X Pattern
Given a number n, we need to print an X pattern of size n. Input : n = 3Output : $ $ $ $ $Input : n = 5Output : $ $ $ $ $ $ $ $ $Input : n = 4Output : $ $ $$ $$ $ $ We need to print n rows and n columns. So we run two nested loops. The outer loop prints all rows one by one (runs for i = 1 to n). The inner loop (runs for j = 1 to n) runs all columns
2 min read
Program to print reverse character bridge pattern
For a given value N, denoting the number of Charters starting from the A, print reverse character bridge pattern.Examples : Input : n = 5 Output : ABCDEDCBA ABCD DCBA ABC CBA AB BA A A Input : n = 8 Output : ABCDEFGHGFEDCBA ABCDEFG GFEDCBA ABCDEF FEDCBA ABCDE EDCBA ABCD DCBA ABC CBA AB BA A A Recommended: Please solve it on PRACTICE first, before m
5 min read
C++ Program to Print matrix in snake pattern
Given an n x n matrix in the given matrix, you have to print the elements of the matrix in the snake pattern. Examples: Input :mat[][] = { {10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}}; Output : 10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32 Input :mat[][] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Output : 1 2 3 6 5 4 7 8 9Re
2 min read
Program to print hollow pyramid, diamond pattern and their modifications
For Prerequisite : Loops, If Else Statement1. Hollow pyramid/triangle pattern The pattern is similar to pyramid pattern. The only difference is, we will replace all internal '#' or '*' characters by space character and we will print 2*N-1 (N = number of rows in pattern) '#' or '*' characters in last row. Examples: Input: n=6 Output: # # # # # # # #
15+ min read
C program to print name pattern
The printing of patterns is the most common and interesting problem. This C program prompts users to input their names and transform each letter of the name into a visually appealing big star pattern. For Example, Input: NAME Output: * * **** * * ****** ** * * * ** ** * * * * ****** * ** * **** * ** * * * * * * * * * * * ******ApproachThe approach
5 min read
Print the pyramid pattern with given height and minimum number of stars
Given the minimum number of stars and the height of the pyramid, print the pattern.Examples: Input: min_stars = 1 p_height = 5 Output: * * *** *** ***** ***** ******* ******* ****************** Input: min_stars = 3 p_height = 7 Output: *** *** ***** ***** ******* ******* ********* ********* *********** *********** ************* ************* ******
8 min read
Print pattern using only one loop | Set 1 (Using setw)
Print simple patterns like below using single line of code under loop. Examples: Input : 5Output : * ** *** *********Input : 6Output : * ** *** **** ***********setw(n) Creates n columns and fills these n columns from right. We fill i of them with a given character, here we create a string with i asterisks using string constructor. setfill() Used to
4 min read
Program to find the Radius of the incircle of the triangle
Given a circle which is the incircle of a triangle whose sides are a, b< and c, the task is to find the radius of this incircle. Examples: Input: a = 2, b = 2, c = 3 Output: 0.566947 Input: a = 3, b = 4, c = 5 Output: 1 Approach: Radius of the incircle = area of the triangle / half of perimeter of the triangle where: Area of the triangle = √(p*(
6 min read
C++ Program For Pascal's Triangle
Pascal's triangle is a triangular array of the binomial coefficients. Write a function that takes an integer value n as input and prints first n lines of the Pascal's triangle. Following are the first 6 rows of Pascal's Triangle.  1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 Method 1 ( O(n^3) time complexity ) Number of entries in every line is equa
5 min read
Rendering a Triangle using OpenGL(using Shaders)
In this article we'll see how to render a triangle using OpenGL. A triangle is probably the simplest shapes you can draw in OpenGL after points and lines and any complicated geometry that you make will me made up of number of triangles joined together.We'll be using the programmable pipeline, so we'll be writing simple shader programs as well and c
9 min read
Biggest Square that can be inscribed within an Equilateral triangle
Given here is an equilateral triangle of side length a. The task is to find the side of the biggest square that can be inscribed within it.Examples: Input: a = 5 Output: 2.32 Input: a = 7 Output: 3.248 Approach: Let the side of the square be x. Now, AH is perpendicular to DE. DE is parallel to BC, So, angle AED = angle ACB = 60 In triangle EFC, =
4 min read
Area of Circumcircle of a Right Angled Triangle
Given an integer C which is the length of the hypotenuse of a right angled triangle of a circumcircle passing through the centre of the circumcircle. The task is to find the area of the circumcircle. Examples: Input: C = 8 Output: 50.26Input: C = 10 Output: 78.53 Approach: Since the hypotenuse C passes through the center of the circle, the radius o
5 min read
Practice Tags :
three90RightbarBannerImg