Codeforcoding

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

Cpp program to pyramid number pattern
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

For loop in C++ language

For loop in Java language

For loop in C language

For loop in Python language

while loop in C++ language

while loop in Java language

while loop in C language

while loop in Python language

Nested for loop in Java language

Nested for loop in C++ 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

Pyramid star pattern in Cpp

Pyramid star pattern in C

Pyramid star pattern in Java 

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 program to display pyramid star pattern
Inverted Pyramid number pattern in Cpp language
Exit mobile version