Python Program to Print Right Triangle Number Pattern

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Given the number of rows(largest number) the task is to print the Right Triangle Number pattern in C, C++, and Python.

Examples:

Example1:

Input:

given number of rows of the right triangle = 11

Output:

1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 
6 6 6 6 6 6 
7 7 7 7 7 7 7 
8 8 8 8 8 8 8 8 
9 9 9 9 9 9 9 9 9 
10 10 10 10 10 10 10 10 10 10 
11 11 11 11 11 11 11 11 11 11 11

Example2:

Input:

given number of rows of the right triangle = 9

Output:

1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 
6 6 6 6 6 6 
7 7 7 7 7 7 7 
8 8 8 8 8 8 8 8 
9 9 9 9 9 9 9 9 9

Program to Print Right Triangle Number Pattern in C, C++, and Python

Below are the ways to print the Right Triangle Number Pattern in C, C++, and Python.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number of rows of the right-angled triangle Number pattern as static input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the iterator value of the parent loop with space in the inner For loop. (This prints the same number parent loop number of times)
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the right-angled triangle Number pattern
# as static input and store it in a variable.
triangleNum = 11
# Loop from 1 to the number of rows using For loop.
for m in range(1, triangleNum+1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
        # Print the iterator value of the nested loop with space in the inner For loop.
        # (This prints the same number parent loop number of times)
        print(m, end=' ')
    # Print the newline character after ending of inner For loop.
    print()

Output:

1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 
6 6 6 6 6 6 
7 7 7 7 7 7 7 
8 8 8 8 8 8 8 8 
9 9 9 9 9 9 9 9 9 
10 10 10 10 10 10 10 10 10 10 
11 11 11 11 11 11 11 11 11 11 11

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows of the right-angled triangle
    // Number pattern
    // as static input and store it in a variable.
    int triangleNum = 19;
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= triangleNum;
         m++) { // Loop from 1 to first loop iterator value
                // using another Nested For loop.
        for (int n = 1; n <= m; n++) {
            // Print the star character with space in the
            // inner For loop.
            cout << m << " ";
        }
        // Print the iterator value of the parent loop with
        // space in the inner For loop.
        /*(This prints the same number parent loop number of
        times*/
        cout << endl;
    }

    return 0;
}

Output:

1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 
6 6 6 6 6 6 
7 7 7 7 7 7 7 
8 8 8 8 8 8 8 8 
9 9 9 9 9 9 9 9 9 
10 10 10 10 10 10 10 10 10 10 
11 11 11 11 11 11 11 11 11 11 11 
12 12 12 12 12 12 12 12 12 12 12 12 
13 13 13 13 13 13 13 13 13 13 13 13 13 
14 14 14 14 14 14 14 14 14 14 14 14 14 14 
15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 
16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 
17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 
18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 
19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows of the right-angled triangle
    // Number pattern
    // as static input and store it in a variable.
    int trianglerows = 9;
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= trianglerows;
         m++) { // Loop from 1 to first loop iterator value
                // using another Nested For loop.
        for (int n = 1; n <= m; n++) {
            // Print the iterator value of the parent loop
            // with space in the inner For loop.
            //(This prints the same number parent loop
            //number of times)
            printf("%d ", m);
        }
        // Print the newline character after ending of inner
        // For loop.
        printf("\n");
    }
    return 0;
}

Output:

1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 
6 6 6 6 6 6 
7 7 7 7 7 7 7 
8 8 8 8 8 8 8 8 
9 9 9 9 9 9 9 9 9

Method #2: Using For Loop (User Input)

Approach:

  • Give the number of rows of the right-angled triangle Number pattern as user input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the iterator value of the parent loop with space in the inner For loop. (This prints the same number parent loop number of times)
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

1) Python Implementation

  • Give the number of rows of the right-angled triangle Number pattern as user input using int(input()) and store it in a variable.

Below is the implementation:

# Give the number of rows of the right-angled triangle Number pattern as user input using int(input())
# and store it in a variable.
triangleNum = int(
    input('Enter some random number of rows of the triangle Number pattern ='))
# Loop from 1 to the number of rows using For loop.
for m in range(1, triangleNum+1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
        # Print the iterator value of the nested loop with space in the inner For loop.
        # (This prints the same number parent loop number of times)
        print(m, end=' ')
    # Print the newline character after ending of inner For loop.
    print()

Output:

Enter some random number of rows of the triangle Number pattern =12
1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 
6 6 6 6 6 6 
7 7 7 7 7 7 7 
8 8 8 8 8 8 8 8 
9 9 9 9 9 9 9 9 9 
10 10 10 10 10 10 10 10 10 10 
11 11 11 11 11 11 11 11 11 11 11 
12 12 12 12 12 12 12 12 12 12 12 12

2) C++ Implementation

  • Give the number of rows of the right-angled triangle Number pattern 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 of the right-angled triangle
    // Number pattern
    // as user input using cin and store it in a variable.
    int triangleNum;
    cout << "Enter some random number of rows of the "
            "triangle Number pattern = "
         << endl;
    cin >> triangleNum;
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= triangleNum;
         m++) { // Loop from 1 to first loop iterator value
                // using another Nested For loop.
        for (int n = 1; n <= m; n++) {
            // Print the star character with space in the
            // inner For loop.
            cout << m << " ";
        }
        // Print the iterator value of the parent loop with
        // space in the inner For loop.
        /*(This prints the same number parent loop number of
        times*/
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows of the triangle Number pattern = 
5
1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5

3) C Implementation

  • Give the number of rows of the right-angled triangle Number pattern 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 of the right-angled triangle
    // Number pattern
    // as user input using scanf and store it in a variable.
    int trianglerows;
    scanf("%d", &trianglerows);
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= trianglerows;
         m++) { // Loop from 1 to first loop iterator value
                // using another Nested For loop.
        for (int n = 1; n <= m; n++) {
            // Print the iterator value of the parent loop
            // with space in the inner For loop.
            //(This prints the same number parent loop
            // number of times)
            printf("%d ", m);
        }
        // Print the newline character after ending of inner
        // For loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Related Programs: