Python Program to Print a Unique Pyramid Pattern of Digits

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Given the number of rows, the task is to Print a Unique Pyramid Pattern of Digits in C, C++, and Python

Examples:

Example1:

Input:

Given number of rows = 8

Output:

1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 
1 2 3 4 5 6 5 4 3 2 1 
1 2 3 4 5 6 7 6 5 4 3 2 1

Example2:

Input:

Given number of rows = 7

Output:

1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 
1 2 3 4 5 6 5 4 3 2 1

Program to Print a Unique Pyramid Pattern of Digits in C, C++, and Python

Below are the ways to Print a Unique Pyramid Pattern of Digits in C, C++, and Python

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to m-1 using another For loop(Nested For loop) where m is the iterator value of the parent For loop.
  • Print the inner loop iterator value with space.
  • Loop from m-1 to 0 in decreasing order using another For loop(Nested For loop) where m is the iterator value of the parent For loop.
  • Print the inner loop iterator value with space.
  • Print the Newline character after the end of the inner loop.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numberOfRows = 8
# Loop from 1 to the number of rows using For loop.
for m in range(1, numberOfRows + 1):
    # Loop from 1 to m-1 using another For loop(Nested For loop)
    # where m is the iterator value of the parent For loop.
    for n in range(1, m - 1):
      # Print the inner loop iterator value with space.
        print(n, end=" ")
    # Loop from m-1 to 0 in decreasing order using another For loop(Nested For loop)
    # where m is the iterator value of the parent For loop.
    for n in range(m - 1, 0, -1):
      # Print the inner loop iterator value with space.
        print(n, end=" ")
    # Print the Newline character after the end of the inner loop.
    print()

Output:

1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 
1 2 3 4 5 6 5 4 3 2 1 
1 2 3 4 5 6 7 6 5 4 3 2 1

2) C++ Implementation

Below is the implementation:

#include <iostream>
#include <math.h>
using namespace std;
int main()
{

    // Give the number of rows as static input and store it
    // in a variable.
    int numberOfRows = 8;
    // Loop from 1 to the number of rows using For loop.

    for (int m = 1; m <= numberOfRows; m++) {
        // Loop from 1 to m-1 using another For loop(Nested
        // For loop)
        // where m is the iterator value of the parent For
        // loop.
        for (int n = 1; n < m - 1; n++) {
            // Print the inner loop iterator value with
            // space.
            cout << n << " ";
        }
        // Loop from m-1 to 0 in decreasing order using
        // another For loop(Nested For loop) where m is the
        // iterator value of the parent For loop.
        for (int n = m - 1; n > 0; n--) {
            // Print the inner loop iterator value with
            // space.
            cout << n << " ";
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }

    return 0;
}

Output:

1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 
1 2 3 4 5 6 5 4 3 2 1 
1 2 3 4 5 6 7 6 5 4 3 2 1

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows as static input and store it
    // in a variable.
    int numberOfRows = 7;
    // Loop from 1 to the number of rows using For loop.

    for (int m = 1; m <= numberOfRows; m++) {
        // Loop from 1 to m-1 using another For loop(Nested
        // For loop) where m is the iterator value of the
        // parent For loop.
        for (int n = 1; n < m - 1; n++) {
            // Print the inner loop iterator value with
            // space.
            printf("%d ", n);
        }
        // Loop from m-1 to 0 in decreasing order using
        // another For loop(Nested For loop) where m is the
        // iterator value of the parent For loop.
        for (int n = m - 1; n > 0; n--) {
            // Print the inner loop iterator value with
            // space.
            printf("%d ", n);
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 
1 2 3 4 5 6 5 4 3 2 1

Method #2: Using For Loop (User Input)

Approach:

  • Give the number of rows as user input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to m-1 using another For loop(Nested For loop) where m is the iterator value of the parent For loop.
  • Print the inner loop iterator value with space.
  • Loop from m-1 to 0 in decreasing order using another For loop(Nested For loop) where m is the iterator value of the parent For loop.
  • Print the inner loop iterator value with space.
  • Print the Newline character after the end of the inner loop.
  • The Exit of the Program.

1) Python Implementation

Give the number of rows as user input using int(input()) and store it in a variable.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numberOfRows = int(input('Enter some random number of rows = '))
# Loop from 1 to the number of rows using For loop.
for m in range(1, numberOfRows + 1):
    # Loop from 1 to m-1 using another For loop(Nested For loop)
    # where m is the iterator value of the parent For loop.
    for n in range(1, m - 1):
      # Print the inner loop iterator value with space.
        print(n, end=" ")
    # Loop from m-1 to 0 in decreasing order using another For loop(Nested For loop)
    # where m is the iterator value of the parent For loop.
    for n in range(m - 1, 0, -1):
      # Print the inner loop iterator value with space.
        print(n, end=" ")
    # Print the Newline character after the end of the inner loop.
    print()

Output:

Enter some random number of rows = 17
1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 
1 2 3 4 5 6 5 4 3 2 1 
1 2 3 4 5 6 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 10 11 12 11 10 9 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 10 11 12 13 12 11 10 9 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 13 12 11 10 9 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

2) C++ Implementation

Give the number of rows as user input using cin and store it in a variable.

Below is the implementation:

#include <iostream>
#include <math.h>
using namespace std;
int main()
{

    // Give the number of rows as user input using
    // int(input()) and store it in a variable.
    int numberOfRows;
    cin >> numberOfRows;
    // Loop from 1 to the number of rows using For loop.

    for (int m = 1; m <= numberOfRows; m++) {
        // Loop from 1 to m-1 using another For loop(Nested
        // For loop)
        // where m is the iterator value of the parent For
        // loop.
        for (int n = 1; n < m - 1; n++) {
            // Print the inner loop iterator value with
            // space.
            cout << n << " ";
        }
        // Loop from m-1 to 0 in decreasing order using
        // another For loop(Nested For loop) where m is the
        // iterator value of the parent For loop.
        for (int n = m - 1; n > 0; n--) {
            // Print the inner loop iterator value with
            // space.
            cout << n << " ";
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }

    return 0;
}

Output:

5
1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 

3) C Implementation

Give the number of rows as user input using scanf and store it in a variable.

Below is the implementation:

#include <stdio.h>
int main()
{

    // Give the number of rows as user input using scanf and
    // store it in a variable.
    int numberOfRows;
    scanf("%d", &numberOfRows);
    // Loop from 1 to the number of rows using For loop.

    for (int m = 1; m <= numberOfRows; m++) {
        // Loop from 1 to m-1 using another For loop(Nested
        // For loop) where m is the iterator value of the
        // parent For loop.
        for (int n = 1; n < m - 1; n++) {
            // Print the inner loop iterator value with
            // space.
            printf("%d ", n);
        }
        // Loop from m-1 to 0 in decreasing order using
        // another For loop(Nested For loop) where m is the
        // iterator value of the parent For loop.
        for (int n = m - 1; n > 0; n--) {
            // Print the inner loop iterator value with
            // space.
            printf("%d ", n);
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

6
1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1

Related Programs: