C++ Program to Print Triangle of Stars

In this tutorial, I am going to explain how to print the triangle of stars by using c++ programming language, keep your close attention in this post as I am going to share a C++ Program to Print Triangle of Stars while executing below C++ program.

C++ Program to Print Triangle of Stars

This program will print the triangle of stars.

C++

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

#include <iostream>

usingnamespacestd;

intmain()

{

inti,j,k;

for(i=1;i<=5;i++)

{

for(j=4;j>=i;j--)

{

cout<<" ";

}

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

{

cout<<"*";

}

cout<<"\n";

}

}

Output is:

Here is another program to print the triangle of stars in reverse orders

C++

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

#include <iostream>

usingnamespacestd;

intmain()

{

inti,j,k;

for(i=5;i>=1;i--)

{

for(j=5;j>i;j--)

{

cout<<" ";

}

for(k=1;k<(i*2);k++)

{

cout<<"*";

}

cout<<"\n";

}

}

Output is:

About: Prem Tiwari

Prem Tiwari is the founder of freewebmentor.com and is a professional developer who has vast experience in PHP and open source technologies. Apart from this, he is a blogger by hobby.
View all posts by Prem Tiwari