In this tutorial, we will discuss the Cpp program to pyramid number pattern
In this topic, we will learn how to create number pattern in C++ language using for loop and while loop
Pattern 1
#include <iostream> #include <conio.h> using namespace std; int main() { int i,j,k,rows; cout<<"Enter the rows: "; cin>>rows;//get input from user for(i=1; i<=rows; i++){ for(j=1; j<=rows; j++){ cout <<" ";//print space } for(k=1; k<=i; k++){ cout <<i;//print number cout <<" ";//print space amon g the numbers } cout<<"\n";//move to nesxt line rows=rows-1;//row value decrease by 1 } getch(); return 0; }
When the above code is compiled and executed, it produces the following results
Enter the rows: 9 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Pattern 2
#include <iostream> #include <conio.h> using namespace std; int main() { int rows,i,j,k,l=1; cout<<"Enter the number of rows :"; cin>>rows;//get user input from user cout<<"Here your pyramid pattern\n"; for(i=1; i<=rows; i++){ for(j=1; j<=rows-i; j++){//iterates for print space cout<<" ";//print space } for(k=1; k<=i; k++){ cout<<k; //print number cout<<" ";//print space among the numnbers } cout<<"\n"; k=k-1; } getch(); return 0; }
When the above code is compiled and executed, it produces the following results
Enter the number of rows :7 Here your pyramid pattern 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7
Pattern 3
#include <iostream> #include <conio.h> using namespace std; int main() { int rows,i,j,k,l=1; cout<<"Enter the number of rows :"; cin>>rows; cout<<"Here your pyramid pattern\n"; for(i=1; i<=rows; i++){ for(j=1; j<=rows-i; j++){//iterates for space cout<<" ";//print initial space for pyramid shape } for(k=1; k<=i; k++,l++){ cout<<l;//print numbers cout<<" ";//print space amongthe numbers } cout<<"\n";//move to next line } getch(); return 0; }
When the above code is compiled and executed, it produces the following results
Enter the number of rows: 4 Here your pyramid pattern 1 2 3 4 5 6 7 8 9 10
Pattern 4
Pascal triangle pyramid pattern
#include <iostream> #include <conio.h> using namespace std; int main() { int rows,i,j,space_count,count1=1; cout<<"Enter the number of rows: \n"; cin>>rows; for(i=0; i<=rows; i++){//outer for loop - parent for(space_count=1; space_count<=rows-i; space_count++){ cout<<" ";//print space-inner for loop } for(j=0; j<=i; j++){ if(j==0 || i==0) count1=1; else count1=count1*(i-j+1)/j; cout<<count1<<" "; } cout<<"\n"; } getch(); return 0; }
When the above code is compiled and executed, it produces the following results
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1
Pattern 5
#include <iostream> #include <conio.h> using namespace std; int main() { int rows,i,j=0,num=0,count1=0,spaceCount; cout<<"Enter the number of rows: \n"; cin>>rows; for(i=1; i<=rows; i++){//outer for loop - parent for(spaceCount=1; spaceCount<=rows-i; spaceCount++){ cout<<" ";//print space-inner for loop count1++; } while(j!=2*i-1) { if(count1<=rows-1){ cout<<i+j; num++; } else{ num++; cout<<(i+j-2*num); } ++j; } num=count1=j=0; cout<<"\n"; } getch(); return 0; }
When the above code is compiled and executed, it produces the following results
Enter the number of rows: 4 1 2 3 4 3 4 5 6 7 4 5 6 7 8 9 10
Pattern 6
#include <iostream> #include <conio.h> using namespace std; int main() { int rows,i,j=0,num=0,count1=0,spaceCount; cout<<"Enter the number of rows: \n"; cin>>rows; for(i=1; i<=rows; i++){//outer for loop - parent for(spaceCount=1; spaceCount<=rows-i; spaceCount++){ cout<<" ";//print space-inner for loop count1++; } while(j!=2*i-1) { if(count1<=rows-1){ cout<<i+j; count1++; } else{ num++; cout<<(i+j-2*num); } ++j; } num=count1=j=0; cout<<"\n"; } getch(); return 0; }
When the above code is compiled and executed, it produces the following results
Enter the number of rows: 6 1 232 34543 4567654 567898765 67891011109876
Pattern 7
#include <iostream> #include <conio.h> using namespace std; int main() { int rows,i,j; cout<<"Enter the number of rows: \n"; cin>>rows; for(i=1; i<=rows; i++){//outer for loop - parent for(j=1; j<=rows-i; j++){ cout<<" ";//print space-inner for loop } for(j=1; j<i; j++){ cout<<j; cout<<" "; } cout<<"\n" ;//move to next line } getch(); return 0; }
When the above code is compiled and executed, it produces the following results
Enter the number of rows: 8 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7
Suggested for you
Nested for loop in Java language
Nested for loop in C++ language
Nested for loop in Python language
If statements in Java language
Nested if statements in Java language
Floyd’s triangle number pattern using for loop in C
Floyd’s triangle pattern using nested for loop in Java
Floyd’s triangle pattern using nested while loop in Java
Hollow pyramid triangle pattern in C++ language
Rhombus pattern in Java using for loop
Rhombus pattern in C using while loop
Rhombus pattern in C++ using do-while loop
Display Pyramid star pattern in C
Display Pyramid star pattern in Java
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…
View Comments
#include
using namespace std;
int main()
{
int i,j,k,row,c,l;
cin>>row;
for(i=1;i<=row;i++)
{
for(j=i;j<row;j++)
cout<<" ";
for(c=i,k=1;k<=i;k++)
{
cout<=1;l--)
{
cout<<c;
c--;
}
cout<<endl;
}
return 0;
}