C++ Program to Print Triangle, Reverse Pyramid, and Diamond Patterns using stars
In this program, we will learn how to print Triangle, Reverse Pyramid, and Diamond patterns using stars in C++ programming language.
C++ Programming Code to Print Triangle Pattern using stars
Code:-
#include <iostream> using namespace std; int main(){ int rows, space; cout << "Enter the number of rows: "; cin >> rows; space = rows - 1; for (int i = 1; i<=rows; i++){ for (int j = 1; j<=space; j++) cout << " "; --space; for (int k = 1; k<= 2*i-1; k++) cout << "* "; cout << endl; } return 0; }
Output:-
* * * * * * * * * * * * * * * * * * * * * * * * *
C++ Program to Print Reverse Pyramid Pattern using stars
Code:-
#include <iostream> using namespace std; int main(){ int rows, space; cout<<"Enter the number of rows: "; cin>>rows; space = 0; for (int i = 0; i < rows; i++){ for (int j = 1; j<= space; j++) cout << " "; space++; for (int k = 1 ; k <= 2*(rows-i)-1; k++) cout << "* "; cout << endl; } return 0; }
Output:-
* * * * * * * * * * * * * * * * * * * * * * * * *
C++ Program to Print Diamond Pattern using stars
Code:-
#include <iostream> using namespace std; int main(){ int rows, space; cout << "Enter the number of rows: "; cin >> rows; space = rows - 1; for (int i = 1; i<=rows; i++){ for (int j = 1; j<=space; j++) cout << " "; --space; for (int k = 1; k<= 2*i-1; k++) cout << "* "; cout << endl; } space = 1; for (int i = 1; i<= rows - 1; i++){ for (int j = 1; j<= space; j++) cout << " "; space++; for (int k = 1 ; k<= 2*(rows-i)-1; k++) cout << "* "; cout << endl; } return 0; }
Output:-
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
You can learn about many other C++ Programs Here.
Best Books for learning C++ programming language with Data Structure and Algorithms.
Amit Rawat
Latest posts by Amit Rawat (see all)
- Python Program to Print the Fibonacci Sequence (2 ways) - April 7, 2020
- Python Program to Display or Print Prime Numbers Between a Range or an Interval - June 18, 2019
- Python Program To Print Pascal’s Triangle (2 Ways) - June 17, 2019