pyramid triangle

Cpp program to pyramid number pattern

Cpp program to pyramid number pattern

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

pyramid number pattern programs in C++language

Pattern 1

Number Pyramid pattern 1

Pyramid triangle number pattern program 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

Number Pyramid pattern 2

Pyramid triangle number pattern program 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

Number Pyramid pattern 3

Pyramid triangle number pattern program 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

Number Pyramid pattern 4

Pyramid triangle number pattern program 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

Number Pyramid pattern 5

Pyramid triangle number pattern program 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

Number Pyramid pattern 6

Pyramid triangle number pattern program 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

Number Pyramid pattern 7

Pyramid triangle number pattern program 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

The operator on C++ language

for loop in C++ language

While loop in C++ language

Nested for loop in C++ language

If statements in C++ language

C program to display pyramid star pattern
Inverted Pyramid number pattern in Cpp language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

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

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago