In this tutorial, we will discuss about Cpp program to display Pyramid star pattern
In this topic, we will learn how to create Pyramid star pyramid pattern in C++ language using nested for loop
Program 1
Pyramid pattern
#include <iostream> #include <conio.h> using namespace std; int main() { int rows; cout << "Enter the number of rows" << endl; cin>>rows; for(int i=1; i<=rows; i++){//outer for loop for(int j=1; j<=(rows-i)*2; j++){ cout<<" ";//print space fot pyramid } for(int k=i; k>=1; k--){//inner for loop cout<<" "<<"*"; //create left half } for(int l=2; l<=i; l++){ cout<<" "<<"*" ; //create right half } cout<<"\n"; } getch(); return 0; }
Enter the number of rows 6 * *** ***** ******* ********* ***********
Program 2
Inverted Pyramid pattern
#include <iostream> #include <conio.h> using namespace std; int main() { int rows; cout << "Enter the number of rows" << endl; cin>>rows; for(int i=rows; i>=1; i--){//outer for loop for(int j=i; j<=rows; j++){ cout<<" ";//print space fot pyramid } for(int j=1; j<=(2*i-1); j++){//inner for loop cout<<"*"; //print star } cout<<"\n";//move to next line } getch(); return 0; }
Enter the number of rows 6 *********** ********* ******* ***** *** *
Program 3
Right leaned Pyramid pattern
#include <iostream> #include <conio.h> using namespace std; int main() { int rows; cout << "Enter the number of rows" << endl; cin>>rows; for(int i=1; i<=rows; i++){//outer for loop for(int j=1; j<=i; j++){//inner for loop cout<<"*";//print star fot pyramid top half } cout<<"\n"; //move to next line } for(int i=rows; i>=1; i--){//outer for loop for(int j=1; j<=i; j++){//inner loop cout<<"*" ; //print star for bottom half } cout<<"\n"; } getch(); return 0; }
Enter the number of rows 6 * ** *** **** ***** ****** ***** **** *** ** *
Program 4
Left leaned Pyramid pattern
#include <iostream> #include <conio.h> using namespace std; int main() { int rows; cout << "Enter the number of rows" << endl; cin>>rows; for(int i=1; i<=rows; i++){//outer for loop for(int j=i; j<rows; j++){ cout<<" ";//print space for pyramid } for(int j=1; j<=i; j++){ cout<<"*";//print star for pyramid top part }//inner for loop cout<<"\n"; } for(int i=rows; i>=1; i--){//outer for loop for(int j=i; j<=rows; j++){ cout<<" ";//print space for pyramid } for(int j=1; j<i; j++){ cout<<"*";//print star for pyramid below part }//inner for loop cout<<"\n"; } getch(); return 0; }
Enter the number of rows 6 * ** *** **** ***** ****** ***** **** *** ** *
Suggested for you
For loop in C language
For loop in Python language
Nested for loop in Java language
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…