Site icon Codeforcoding

Inverted Pyramid number pattern in Cpp language

Inverted Pyramid number pattern in Cpp language

In this tutorial, we will discuss the Inverted Pyramid number pattern in Cpp language

In this topic, we will learn about how to create an Inverted Pyramid number pattern in C++ using for loop or nested for loop

Inverted Pyramid number pattern in Cpp language

Pattern 1

Pyramid pattern in C++

Inverted Pyramid number pattern program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,j,k,rows,count1,temp,num;
    cout<<"Enter the number of rows: ";
    cin>>rows;//get input from user for number rows
    cout<<"Here your pattern: \n";
    count1=1;
    temp=1+(rows-1)*2;
    for(i=1; i<=rows; i++){  
//parent for loop to iterates row
        num=1;
        for(j=1; j<=count1; j++){//print space in biginning
        cout<<" ";
        }
        for(k=temp; k>=1; k--){
            cout<<num;//print number for pattern
            num++;
        }
        count1++;
        temp=temp-2;
        cout<<"\n";
    }
    getch();
    return 0;
}

When the above code is compiled and executed, it produces the following results

Enter the number of rows: 5
Here your pattern:
123456789
  1234567
    12345
      123
        1

 

Pattern 2

Pyramid pattern in c++

Pyramid  pattern program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,j,k,rows;
    int count1=1, num;
    cout<<"Enter the number of rows: ";
    cin>>rows;//get input from user for number of rows
    cout<<"Here your pattern: \n";
    count1=1;
    num=1+(rows-1)*2;
    for(i=1; i<=rows; i++){//outer for loop for iterate row
        for(j=1; j<=count1; j++){//inner for loop to print space
        cout<<" ";
        }
        for(k=num; k>=1; k--){//inner for loop for print number
            cout<<i;
        }
        count1++;
        num=num-2;
        cout<<"\n";
    }
    getch();
    return 0;
}

When the above code is compiled and executed, it produces the following results

Enter the number of rows: 5
Here your pattern
111111111
  2222222
    33333
      444
        5

 

Pattern 3

Pyramid number pattern in C++

 Pyramid  pattern program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,j,rows,space=0;
    int count1=1, num;
    cout<<"Enter the number of rows: ";
    cin>>rows;//get input from user for pyramid
    cout<<"Here your pattern: \n";
    for(i=rows; i>=1; i--){//outer for loop for iterate row
        for(j=1; j<=space; j++)//inner for loop to print space
        cout<<" ";

        for(j=1; j<=i; j++)//print right side of piramid
            cout<<j;
        for(j=i-1; j>=1; j--)//print right side of piramid
            cout<<j;
            cout<<"\n";
            space++;
    }
    getch();
    return 0;
}

When the above code is compiled and executed, it produces the following results

Enter the number of rows: 6
Here your pattern:
12345654321
  123454321
    1234321
      12321
        121
          1

 

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

Parallelogram and Hollow Parallelogram star pattern in Java

Parallelogram and Hollow Parallelogram star pattern in C Language

 

 

Cpp program to pyramid number pattern
triangle number pattern using nested while in C
Exit mobile version