Python Program to Print the Pyramid of Horizontal Number Tables

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

Given the number of rows, the task is to Print the Pyramid of Horizontal Number Tables in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows =9

Output:

1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
6 12 18 24 30 36 
7 14 21 28 35 42 49 
8 16 24 32 40 48 56 64 
9 18 27 36 45 54 63 72 81

Example2:

Input:

Given number of rows =13

Output:

1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
6 12 18 24 30 36 
7 14 21 28 35 42 49 
8 16 24 32 40 48 56 64 
9 18 27 36 45 54 63 72 81 
10 20 30 40 50 60 70 80 90 100 
11 22 33 44 55 66 77 88 99 110 121 
12 24 36 48 60 72 84 96 108 120 132 144 
13 26 39 52 65 78 91 104 117 130 143 156 169

Program to Print the Pyramid of Horizontal Number Tables in C, C++, and Python

Below are the ways to Print the Pyramid of Horizontal Number Tables 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 the number of 1 to parent loop iterator value using another For loop(Nested For Loop).
  • Print the value of the product of parent loop iterator value and inner loop iterator value with space.
  • After the end of the inner For loop print the Newline Character.
  • 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.
numberows = 9
# Loop from 1 to the number of rows using For loop.
for m in range(1, numberows+1):
    # Loop from 1 to the number of 1 to parent loop iterator value
    # using another For loop(Nested For Loop).
    for n in range(1, m+1):
        # Print the value of the product of parent loop iterator value
        # and inner loop iterator value with space.
        print(m*n, end=" ")
     # Print the newline character after the end of the inner For loop.
    print()

Output:

1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
6 12 18 24 30 36 
7 14 21 28 35 42 49 
8 16 24 32 40 48 56 64 
9 18 27 36 45 54 63 72 81

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{
    // Give the number of rows as static input and store it
    // in a variable.
    int numberows = 9;

    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= numberows; m++) {
        // Loop from 1 to the number of 1 to parent loop
        // iterator value
        // using another For loop(Nested For Loop).
        for (int n = 1; n <= m; n++) {
            // Print the value of the product of parent loop
            // iterator value and inner loop iterator value
            // with space.
            cout << m * n << " ";
        }
        // Print the newline character after the end of
        // the inner For loop.
        cout << endl;
    }

    return 0;
}

Output:

1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
6 12 18 24 30 36 
7 14 21 28 35 42 49 
8 16 24 32 40 48 56 64 
9 18 27 36 45 54 63 72 81

3) C Implementation

Below is the implementation:

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

    // Give the number of rows as static input and store it
    // in a variable.
    int numberows = 9;

    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= numberows; m++) {
        // Loop from 1 to the number of 1 to parent loop
        // iterator value
        // using another For loop(Nested For Loop).
        for (int n = 1; n <= m; n++) {
            // Print the value of the product of parent loop
            // iterator value and inner loop iterator value
            // with space.
            printf("%d ", m * n);
        }
        // Print the newline character after the end of
        // the inner For loop.
        printf("\n");
    }
    return 0;
}

Output:

1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
6 12 18 24 30 36 
7 14 21 28 35 42 49 
8 16 24 32 40 48 56 64 
9 18 27 36 45 54 63 72 81

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 the number of 1 to parent loop iterator value using another For loop(Nested For Loop).
  • Print the value of the product of the parent loop iterator value and inner loop iterator value with space.
  • After the end of the inner For loop print the Newline Character.
  • 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 word as user input using int(input()) and store it in a variable.
numberows = int(input('Enter some random number of rows = '))
# Loop from 1 to the number of rows using For loop.
for m in range(1, numberows+1):
    # Loop from 1 to the number of 1 to parent loop iterator value
    # using another For loop(Nested For Loop).
    for n in range(1, m+1):
        # Print the value of the product of parent loop iterator value
        # and inner loop iterator value with space.
        print(m*n, end=" ")
     # Print the newline character after the end of the inner For loop.
    print()

Output:

Enter some random number of rows = 13
1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
6 12 18 24 30 36 
7 14 21 28 35 42 49 
8 16 24 32 40 48 56 64 
9 18 27 36 45 54 63 72 81 
10 20 30 40 50 60 70 80 90 100 
11 22 33 44 55 66 77 88 99 110 121 
12 24 36 48 60 72 84 96 108 120 132 144 
13 26 39 52 65 78 91 104 117 130 143 156 169

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>
using namespace std;

int main()
{
    // Give the number of rows as user input using cin and
    // store it in a variable.
    int numberows;
    cin >> numberows;

    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= numberows; m++) {
        // Loop from 1 to the number of 1 to parent loop
        // iterator value
        // using another For loop(Nested For Loop).
        for (int n = 1; n <= m; n++) {
            // Print the value of the product of parent loop
            // iterator value and inner loop iterator value
            // with space.
            cout << m * n << " ";
        }
        // Print the newline character after the end of
        // the inner For loop.
        cout << endl;
    }

    return 0;
}

Output:

13
1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
6 12 18 24 30 36 
7 14 21 28 35 42 49 
8 16 24 32 40 48 56 64 
9 18 27 36 45 54 63 72 81 
10 20 30 40 50 60 70 80 90 100 
11 22 33 44 55 66 77 88 99 110 121 
12 24 36 48 60 72 84 96 108 120 132 144 
13 26 39 52 65 78 91 104 117 130 143 156 169

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>
#include <string.h>
int main()
{

    // Give the number of rows as user input using scanf and
    // store it in a variable.
    int numberows;
    scanf("%d", &numberows);

    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= numberows; m++) {
        // Loop from 1 to the number of 1 to parent loop
        // iterator value
        // using another For loop(Nested For Loop).
        for (int n = 1; n <= m; n++) {
            // Print the value of the product of parent loop
            // iterator value and inner loop iterator value
            // with space.
            printf("%d ", m * n);
        }
        // Print the newline character after the end of
        // the inner For loop.
        printf("\n");
    }
    return 0;
}

Output:

13
1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
6 12 18 24 30 36 
7 14 21 28 35 42 49 
8 16 24 32 40 48 56 64 
9 18 27 36 45 54 63 72 81 
10 20 30 40 50 60 70 80 90 100 
11 22 33 44 55 66 77 88 99 110 121 
12 24 36 48 60 72 84 96 108 120 132 144 
13 26 39 52 65 78 91 104 117 130 143 156 169

Related Programs: