Python Program to Print the Equilateral Triangle Pattern of Star

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Given the number of rows, the task is to Print Equilateral triangle Pattern of Star in C, C++, and Python

Examples:

Example1:

Input:

Given number of rows = 8

Output:

              * 
             * * 
            * * * 
           * * * * 
          * * * * * 
         * * * * * * 
        * * * * * * * 
       * * * * * * * *

Example2:

Input:

Given number of rows = 10
Given Character to print ='$'

Output

                  $ 
                 $ $ 
                $ $ $ 
               $ $ $ $ 
              $ $ $ $ $ 
             $ $ $ $ $ $ 
            $ $ $ $ $ $ $ 
           $ $ $ $ $ $ $ $ 
          $ $ $ $ $ $ $ $ $ 
         $ $ $ $ $ $ $ $ $ $

Program to Print the Equilateral triangle Pattern of Star in C, C++, and Python

Below are the ways to Print the Equilateral triangle Pattern of Star in C, C++, and Python.

Method #1: Using For Loop (Star Character)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Take a variable to say g and initialize its value with (2*number of rows)-2.
  • Loop from 0 to the number of rows using For loop.
  • Loop from 0 to g using another For Loop(Inner For loop).
  • Print the space character in the inner For Loop.
  • Decrement the value of g by 1 after the end of the inner For loop.
  • Loop from 0 to m+1 using another For loop(Nested For loop) where m is the iterator value of the parent For loop.
  • Print the star character with space.
  • Print the Newline character after the end of the Two inner For loops.
  • 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
# Take a variable to say g and initialize its value with (2*number of rows)-2.
g = (2*numberOfRows)-2
# Loop from 0 to the number of rows using For loop.
for m in range(0, numberOfRows):
    # Loop from 0 to g using another For Loop(Inner For loop).
    for n in range(0, g):
      # Print the space character in the inner For Loop.
        print(end=" ")
    # Decrement the value of g by 1 after the end of the inner For loop.
    g = g-1
    # Loop from 0 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(0, m+1):
      # Print the star character with space.
        print('*', end=" ")
    # Print the Newline character after the end of the Two inner For loops.
    print()

Output:

              * 
             * * 
            * * * 
           * * * * 
          * * * * * 
         * * * * * * 
        * * * * * * * 
       * * * * * * * *

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 numberOfRows = 8;

    // Take a variable to say g and initialize its value
    // with (2*number of rows)-2.
    int g = (2 * numberOfRows) - 2;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < numberOfRows; m++) {
        // Loop from 0 to g using another For Loop(Inner For
        // loop).
        for (int n = 0; n < g; n++) {
            // Print the space character in the inner For
            // Loop.
            cout << " ";
        }
        // Decrement the value of g by 1 after the end of
        // the inner For loop.
        g = g - 1;
        // Loop from 0 to m+1 using another For loop(Nested
        // For loop) where m is the iterator value of the
        // parent For loop.
        for (int n = 0; n < m + 1; n++) {
            // Print the star character with space.
            cout << "* ";
        }
        // Print the Newline character after the end of the
        // Two inner For loops.
        cout << endl;
    }
    return 0;
}

Output:

              * 
             * * 
            * * * 
           * * * * 
          * * * * * 
         * * * * * * 
        * * * * * * * 
       * * * * * * * *

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 = 13;

    // Take a variable to say g and initialize its value
    // with (2*number of rows)-2.
    int g = (2 * numberOfRows) - 2;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < numberOfRows; m++) {
        // Loop from 0 to g using another For Loop(Inner For
        // loop).
        for (int n = 0; n < g; n++) {
            // Print the space character in the inner For
            // Loop.
            printf(" ");
        }
        // Decrement the value of g by 1 after the end of
        // the inner For loop.
        g = g - 1;
        // Loop from 0 to m+1 using another For loop(Nested
        // For loop) where m is the iterator value of the
        // parent For loop.
        for (int n = 0; n < m + 1; n++) {
            // Print the star character with space.
            printf("* ");
        }
        // Print the Newline character after the end of the
        // Two inner For loops.
        printf("\n");
    }
    return 0;
}

Output:

                        * 
                       * * 
                      * * * 
                     * * * * 
                    * * * * * 
                   * * * * * * 
                  * * * * * * * 
                 * * * * * * * * 
                * * * * * * * * * 
               * * * * * * * * * * 
              * * * * * * * * * * * 
             * * * * * * * * * * * * 
            * * * * * * * * * * * * *

Method #2: Using For Loop (User Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Scan the character to print as user input and store it in a variable.
  • Take a variable to say g and initialize its value with (2*number of rows)-2.
  • Loop from 0 to the number of rows using For loop.
  • Loop from 0 to g using another For Loop(Inner For loop).
  • Print the space character in the inner For Loop.
  • Decrement the value of g by 1 after the end of the inner For loop.
  • Loop from 0 to m+1 using another For loop(Nested For loop) where m is the iterator value of the parent For loop.
  • Print the star character with space.
  • Print the Newline character after the end of the Two inner For loops.
  • 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.
  • Give the Character as user input using input() and store it in another 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 = '))
# Give the character to print as user input using int(input()) and store it in another variable.
characte = input('Enter some random character to print = ')
# Take a variable to say g and initialize its value with (2*number of rows)-2.
g = (2*numberOfRows)-2
# Loop from 0 to the number of rows using For loop.
for m in range(0, numberOfRows):
    # Loop from 0 to g using another For Loop(Inner For loop).
    for n in range(0, g):
      # Print the space character in the inner For Loop.
        print(end=" ")
    # Decrement the value of g by 1 after the end of the inner For loop.
    g = g-1
    # Loop from 0 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(0, m+1):
      # Print the given character with space.
        print(characte, end=" ")
    # Print the Newline character after the end of the Two inner For loops.
    print()

Output

Enter some random number of rows = 10
Enter some random character to print = $
                  $ 
                 $ $ 
                $ $ $ 
               $ $ $ $ 
              $ $ $ $ $ 
             $ $ $ $ $ $ 
            $ $ $ $ $ $ $ 
           $ $ $ $ $ $ $ $ 
          $ $ $ $ $ $ $ $ $ 
         $ $ $ $ $ $ $ $ $ $

2) C++ Implementation

  • Give the number of rows as user input using cin and store it in a variable.
  • Give the Character as user input using cin and store it in another 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 numberOfRows;
    cin >> numberOfRows;
    // Create a character variable.
    char characte;
    // Give the character as user input using cin and store
    // it in another variable.
    cout << "Enter some random character to print = "
         << endl;
    cin >> characte;
    cout << endl;
    int g = (2 * numberOfRows) - 2;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < numberOfRows; m++) {
        // Loop from 0 to g using another For Loop(Inner For
        // loop).
        for (int n = 0; n < g; n++) {
            // Print the space character in the inner For
            // Loop.
            cout << " ";
        }
        // Decrement the value of g by 1 after the end of
        // the inner For loop.
        g = g - 1;
        // Loop from 0 to m+1 using another For loop(Nested
        // For loop) where m is the iterator value of the
        // parent For loop.
        for (int n = 0; n < m + 1; n++) {
            // Print the given character with space.
            cout << characte << " ";
        }
        // Print the Newline character after the end of the
        // Two inner For loops.
        cout << endl;
    }
    return 0;
}

Output

10
Enter some random character to print = $
                  $ 
                 $ $ 
                $ $ $ 
               $ $ $ $ 
              $ $ $ $ $ 
             $ $ $ $ $ $ 
            $ $ $ $ $ $ $ 
           $ $ $ $ $ $ $ $ 
          $ $ $ $ $ $ $ $ $ 
         $ $ $ $ $ $ $ $ $ $

3) C Implementation

  • Give the number of rows as user input using scanf and store it in a variable.
  • Give the Character as user input using scanf and store it in another 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);
    // Create a character variable.
    // Give the character as user input using scanf and
    // store it in another variable.
    char characte;
    scanf("%c", &characte);
    printf("\n");
    // Take a variable to say g and initialize its value
    // with (2*number of rows)-2.
    int g = (2 * numberOfRows) - 2;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < numberOfRows; m++) {
        // Loop from 0 to g using another For Loop(Inner For
        // loop).
        for (int n = 0; n < g; n++) {
            // Print the space character in the inner For
            // Loop.
            printf(" ");
        }
        // Decrement the value of g by 1 after the end of
        // the inner For loop.
        g = g - 1;
        // Loop from 0 to m+1 using another For loop(Nested
        // For loop) where m is the iterator value of the
        // parent For loop.
        for (int n = 0; n < m + 1; n++) {
            // Print the given character with space.
            printf("%c ",characte);
        }
        // Print the Newline character after the end of the
        // Two inner For loops.
        printf("\n");
    }
    return 0;
}

Output

10$
                  $ 
                 $ $ 
                $ $ $ 
               $ $ $ $ 
              $ $ $ $ $ 
             $ $ $ $ $ $ 
            $ $ $ $ $ $ $ 
           $ $ $ $ $ $ $ $ 
          $ $ $ $ $ $ $ $ $ 
         $ $ $ $ $ $ $ $ $ $

Related Programs: