CPP Programming

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:

Program to Print 1 and 0 in alternative rows in C,C++ and Python

Python Program to Print 1 and 0 in Alternative Rows

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output

Given the number of rows and columns, the task is to print 1 and 0 in alternative rows in C, C++, and Python.

Examples:

Example1:

Input:

given number of rows =4
given number of columns=3

Output:

1 1 1 
0 0 0 
1 1 1 
0 0 0

Example2:

Input:

given number of rows =7
given number of columns=15

Output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Program to Print 1 and 0 in Alternative Rows in C, C++, and Python

Below are the ways to print 1 and 0 in alternative rows in C, C++, and Python.

If you look closely at the pattern, you’ll see that for all odd rows, 1 is printed and for all even rows, 0 is printed. As a

result, before displaying integers inside the inner loop, you must check for even-odd conditions. If the current row is

an odd number, print 1 otherwise, print 0.

The following is a step-by-step description of the logic used to print a 1, 0 number pattern at alternate rows.

Method #1: Using For loop

Approach:

  • Give the number of rows and number of columns as static input.
  • Store them in two separate variables row numbers and column numbers.
  • Run an outer loop from 1 to rows to iterate through the rows using For loop.
  • Iterate through the columns from 1 to cols using another For inner loop.
  • Before printing any number, we must first check the condition inside the inner loop.
  • This means that for every odd row, 1 is displayed, and for every even row, 0 is displayed.
  • We check whether the row is odd or not using the if statement.
  • If it is true then print 1 else print 0.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows and number of columns as static input.
# Store them in two separate variables row numbers and column numbers.
rownumbs = 15
colnumbs = 11
# Run an outer loop from 1 to rows to iterate through the rows using For loop.
for m in range(1, rownumbs+1):
  # Iterate through the columns from 1 to cols using another For inner loop.
    for n in range(1, colnumbs+1):
      # Before printing any number, we must first check the condition inside the inner loop.
      # This means that for every odd row, 1 is displayed, and for every even row, 0 is displayed.
          # We check whether the row is odd or not using the if statement.
      # If it is true then print 1 else print 0.
        if(m % 2 == 1):
            print('1', end=' ')
        # If it is true then print 1 else print 0.
        else:
            print('0', end=' ')
    print()

Output:

1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1

2) C++ Implementation

It is the same as the python approach but just the change in syntax.

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{
    // Give the number of rows and number of columns as
    // static input.
    // Store them in two separate variables row numbers and
    // column numbers.
    int rownumbs = 15;
    int colnumbs = 11;
    // Run an outer loop from 1 to rows to iterate through
    // the rows using For loop.
    for (int i = 1; i <= rownumbs; i++) {
        // Iterate through the columns from 1 to cols using
        // another For inner loop.
        for (int j = 1; j <= colnumbs; j++) {
            // Before printing any number, we must
            // first check the condition inside the
            // inner loop.
            // This means that for every odd row, 1 is
            // displayed, and for every even row,
            // 0 is displayed.
            // We check whether the row is odd or not using
            // the if statement.
            // If it is true then print 1 else print 0.

            if (i % 2 == 1)
                cout << "1 ";
            // If it is true then print 1 else print 0.
            else
                cout << "0 ";
        }
        cout << endl;
    }
    return 0;
}

Output:

1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1

3) C Implementation

It is the same as the python approach but just the change in syntax.

Below is the implementation:

#include <stdio.h>

int main(void)
{
    // Give the number of rows and number of columns as
    // static input.
    // Store them in two separate variables row numbers and
    // column numbers.
    int rownumbs = 15;
    int colnumbs = 11;
    // Run an outer loop from 1 to rows to iterate through
    // the rows using For loop.
    for (int i = 1; i <= rownumbs; i++) {
        // Iterate through the columns from 1 to cols using
        // another For inner loop.
        for (int j = 1; j <= colnumbs;
             j++) { // Before printing any number, we must
            // first check the condition inside the
            // inner loop.
            // This means that for every odd row, 1 is
            // displayed, and for every even row, 0 is
            // displayed.
            /*We check whether the row is odd or not using
              the if statement. If it is true then print 1
              else print 0.*/
            if (i % 2 == 1)
                printf("1 ");
            // If it is true then print 1 else print 0.
            else
                printf("0 ");
        }
        printf("\n");
    }
    return 0;
}

Output:

1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1

Method #2: Using while loop

1) C Implementation

We just iterate till rownumbs and columumbs using a while loop.

Below is the implementation:

#include <stdio.h>

int main(void)
{
    // Give the number of rows and number of columns as
    // static input.
    // Store them in two separate variables row numbers and
    // column numbers.
    int rownumbs = 4;
    int colnumbs = 3;
    // Run an outer loop from 1 to rows to iterate through
    // the rows using For loop.
    int temprow = 1;
    while (temprow <= rownumbs) {
        // Iterate through the columns from 1 to cols using
        // another For inner loop.
        int tempcol = 1;
        while (tempcol <= colnumbs) { // Before printing any
                                      // number, we must
            // first check the condition inside the
            // inner loop. This means that for every odd
            // row, 1 is displayed, and for every even row,
            // 0 is displayed. # We check whether the row is
            // odd or not using the if statement. # If it is
            // true then print 1 else print 0.
            if (temprow % 2 == 1)
                printf("1 ");
            // If it is true then print 1 else print 0.
            else
                printf("0 ");
            tempcol++;
        }
        temprow++;
        printf("\n");
    }
    return 0;
}

Output:

1 1 1 
0 0 0 
1 1 1 
0 0 0

2) C++ Implementation:

We just iterate till rownumbs and columumbs using a while loop.

Below is the implementation:

#include <iostream>
using namespace std;

int main(void)
{
    // Give the number of rows and number of columns as
    // static input.
    // Store them in two separate variables row numbers and
    // column numbers.
    int rownumbs = 2;
    int colnumbs = 19;
    // Run an outer loop from 1 to rows to iterate through
    // the rows using For loop.
    int temprow = 1;
    while (temprow <= rownumbs) {
        // Iterate through the columns from 1 to cols using
        // another For inner loop.
        int tempcol = 1;
        while (tempcol <= colnumbs) { // Before printing any
                                      // number, we must
            // first check the condition inside the
            // inner loop. This means that for every odd
            // row, 1 is displayed, and for every even row,
            // 0 is displayed. # We check whether the row is
            // odd or not using the if statement. # If it is
            // true then print 1 else print 0.
            if (temprow % 2 == 1)
                cout << "1 ";
            // If it is true then print 1 else print 0.
            else
                cout << "0 ";
            tempcol++;
        }
        temprow++;
        cout << endl;
    }
    return 0;
}

Output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

3) Python Implementation

We just iterate till rownumbs and columumbs using a while loop.

Below is the implementation:

# Give the number of rows and number of columns as static input.
# Store them in two separate variables row numbers and column numbers.
rownumbs = 6
colnumbs = 11
# Run an outer loop from 1 to rows to iterate through the rows using For loop.
temprow = 1
while(temprow <= rownumbs):
  # Iterate through the columns from 1 to cols using another For inner loop.
    tempcol = 1
    while(tempcol <= colnumbs):
      # Before printing any number, we must first check the condition inside the inner loop.
      # This means that for every odd row, 1 is displayed, and for every even row, 0 is displayed.
      # We check whether the row is odd or not using the if statement.
        if(temprow % 2 == 1):
            print('1', end=' ')
        # If it is true then print 1 else print 0.
        else:
            print('0', end=' ')
        tempcol += 1
    temprow += 1
    print()

Output:

1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0

Related Programs:

Python Program to Print Inverted Pyramid Star Pattern

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Given the number of rows of the pyramid, the task is to print the Inverted Pyramid Star pattern in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows of the pyramid  =10

Output:

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

Example2:

Input:

Given number of rows of the pyramid  =10
Given character to print='&'

Output:

& & & & & & & & & & 
 & & & & & & & & & 
  & & & & & & & & 
   & & & & & & & 
    & & & & & & 
     & & & & & 
      & & & & 
       & & & 
        & & 
         &

Program to Print Inverted Pyramid Star Pattern in C, C++, and Python

Below are the ways to print Inverted Pyramid Star Pattern in C, C++, and Python.

Method #1: Using For Loop (Star Character)

Approach:

  • Give the number of rows of the pyramid as static input and store it in a variable.
  • Loop from the number of rows to 0 in decreasing order using For Loop.
  • Loop from 0 to the number of rows – iterator value of the parent For loop using another Nested For loop(Inner For loop).
  • Print the space character in the inner For loop.
  • Loop from 0 to the iterator value of the parent For loop using another Nested For loop(Inner For loop).
  • Print the star character with a space character in the inner For loop.
  • After the end of the two inner For loops print the newline character.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the pyramid as static input and store it in a variable.
pyRows = 10
# Loop from the number of rows to 0 in decreasing order using For Loop.
for m in range(pyRows, 0, -1):
    # Loop from 0 to the number of rows - iterator value of the parent
    # For loop using another Nested For loop(Inner For loop)..(0, rows - i):
    for n in range(0, pyRows-m):
        # Print the space character in the inner For loop.
        print(end=' ')
     # Loop from 0 to the iterator value of the parent For loop using another Nested For loop(Inner For loop).

    for l in range(0, m):
        # Print the star character with a space character in the inner For loop.
        print('*', end=' ')
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows of the pyramid as static
    // input and store it in a variable.
    int pyRows=10;
    // Loop from the number of rows to 0 in decreasing order
    // using For Loop.
    for (int m = pyRows; m > 0; m--) {
        //  Loop from 0 to the number of rows - iterator
        //  value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int n = 0; n < pyRows - m; n++) {
            //  Print the space character in the inner For
            //  loop.
            cout << " ";
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int l = 0; l < m; l++) {
            // Print the star character with a space
            // character in the inner For loop.
            cout << "* ";
        }

        // Print the Newline Character after the end of the
        // inner for loop.
        cout << endl;
    }
    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{
    // Give the number of rows of the pyramid as static
    // input and store it in a variable.
    int pyRows = 10;
    // Loop from the number of rows to 0 in decreasing order
    // using For Loop.
    for (int m = pyRows; m > 0; m--) {
        //  Loop from 0 to the number of rows - iterator
        //  value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int n = 0; n < pyRows - m; n++) {
            //  Print the space character in the inner For
            //  loop.
            printf(" ");
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int l = 0; l < m; l++) {
            // Print the star character with a space
            // character in the inner For loop.
            printf("* ");
        }

        // Print the Newline Character after the end of the
        // inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For Loop (User Character)

Approach:

  • Give the number of rows of the pyramid as user input and store it in a variable.
  • Give the character as static input and store it in a variable.
  • Loop from the number of rows to 0 in decreasing order using For Loop.
  • Loop from 0 to the number of rows – iterator value of the parent For loop using another Nested For loop(Inner For loop).
  • Print the space character in the inner For loop.
  • Loop from 0 to the iterator value of the parent For loop using another Nested For loop(Inner For loop).
  • Print the given character with a space character in the inner For loop.
  • After the end of the two inner For loops print the newline character.
  • The Exit of the Program.

1) Python Implementation

  • Give the number of rows of the pyramid 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 of the pyramid as user input using int(input()) and store it in a variable.
pyRows = int(input(
    'Enter some random number of rows of pyramid = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
# Loop from the number of rows to 0 in decreasing order using For Loop.
for m in range(pyRows, 0, -1):
    # Loop from 0 to the number of rows - iterator value of the parent
    # For loop using another Nested For loop(Inner For loop).
    for n in range(0, pyRows-m):
        # Print the space character in the inner For loop.
        print(end=' ')
     # Loop from 0 to the iterator value of the parent For loop using another Nested For loop(Inner For loop).

    for l in range(0, m):
        # Print the given character with a space character in the inner For loop.
        print(givencharacter, end=' ')
    print()

Output:

Enter some random number of rows of pyramid = 10
Enter some random character = &
& & & & & & & & & & 
 & & & & & & & & & 
  & & & & & & & & 
   & & & & & & & 
    & & & & & & 
     & & & & & 
      & & & & 
       & & & 
        & & 
         &

2) C++ Implementation

  • Give the number of rows of the pyramid as user input using scanf 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 pyRows;
    char givencharacter;
    cout << "Enter some random number of rows = " << endl;
    cin >> pyRows;
    // Give the Character as user input using cin and store
    // it in another variable.
    cout << "Enter some random character = " << endl;
    cin >> givencharacter;
    cout << endl;
    // Loop from the number of rows to 0 in decreasing order
    // using For Loop.
    for (int m = pyRows; m > 0; m--) {
        //  Loop from 0 to the number of rows - iterator
        //  value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int n = 0; n < pyRows - m; n++) {
            //  Print the space character in the inner For
            //  loop.
            cout << " ";
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int l = 0; l < m; l++) {
            // Print the star character with a space
            // character in the inner For loop.
            cout << givencharacter << " ";
        }

        // Print the Newline Character after the end of the
        // inner for loop.
        cout << endl;
    }
    return 0;
}

Output:

Enter some random number of rows of pyramid = 
10
Enter some random character = 
&
& & & & & & & & & & 
 & & & & & & & & & 
  & & & & & & & & 
   & & & & & & & 
    & & & & & & 
     & & & & & 
      & & & & 
       & & & 
        & & 
         &

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 pyRows;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.
    scanf("%d", &pyRows);
    scanf("%c", &givencharacter);
    // Loop from the number of rows to 0 in decreasing order
    // using For Loop.
    for (int m = pyRows; m > 0; m--) {
        //  Loop from 0 to the number of rows - iterator
        //  value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int n = 0; n < pyRows - m; n++) {
            //  Print the space character in the inner For
            //  loop.
            printf(" ");
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int l = 0; l < m; l++) {
            // Print the given character with a space
            // character in the inner For loop.
            printf("%c ", givencharacter);
        }

        // Print the Newline Character after the end of the
        // inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

10&
& & & & & & & & & & 
 & & & & & & & & & 
  & & & & & & & & 
   & & & & & & & 
    & & & & & & 
     & & & & & 
      & & & & 
       & & & 
        & & 
         &

Related Programs:

Python Program to Print Right Angled Triangle Star Pattern

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language

Given the number of rows of the right-angled triangle star pattern in C, C++, and python.

Examples:

Example1:

Input:

given number of rows of the right-angled triangle star pattern=19

Output:

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

Example2:

Input:

given number of rows of the right-angled triangle star pattern=11
given character to print =^

Output:

^ 
^ ^ 
^ ^ ^ 
^ ^ ^ ^ 
^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^

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

Below are the ways to print the right-angled triangle star pattern in C, C++, and Python.

Method #1: Using For loop (Star Character)

Approach:

  • Give the number of rows of the right-angled triangle star 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 star character with space in the inner For loop.
  • 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 star pattern
# as static input and store it in a variable.
trianglerows = 19
# Loop from 1 to the number of rows using For loop.
for m in range(1, trianglerows+1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
        # Print the star character with space in the inner For loop.
        print('*', end=' ')
    #Print the newline character after ending of inner For loop.
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows of the right-angled triangle
    // star pattern as static input and store it in a
    // variable.
    int trianglerows = 11;
    // 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 star character with space in the
            // inner For loop.
            cout << "* ";
        }
        // Print the newline character after ending of inner
        // For loop.
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows of the right-angled triangle
    // star 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 star character with space in the
            // inner For loop.
            printf("* ");
        }
        // Print the newline character after ending of inner
        // For loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For loop (User Character )

Approach:

  • Give the number of rows of the right-angled triangle star 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 star character with space in the inner For loop.
  • 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 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 of the right-angled triangle as user input using int(input()) and store it in a variable.
trianglerows = 17
# Give the Character as user input using input() and store it in another variable.
givencharacter = '{'
for m in range(1, trianglerows+1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
        # Print the star character with space in the inner For loop.
        print(givencharacter, end=' ')
    # Print the newline character after ending of inner For loop.
    print()

Output:

Enter some random number of rows of the right-angled triangle = 14
Enter some random character = |
| 
| | 
| | | 
| | | | 
| | | | | 
| | | | | | 
| | | | | | | 
| | | | | | | | 
| | | | | | | | | 
| | | | | | | | | | 
| | | | | | | | | | | 
| | | | | | | | | | | | 
| | | | | | | | | | | | | 
| | | | | | | | | | | | | |

2) C++ Implementation

  • Give the number of rows of the right-angled triangle 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()
{
    int trianglerows;
    char givencharacter;

    // Give the number of rows of the right-angled triangle
    // as user input using cin and store it in a variable.
    cout << "Enter some random number of rows of the "
            "right-angled triangle = "
         << endl;
    cin >> trianglerows;
    // Give the Character as user input using cin and store
    // it in another variable.
    cout << "Enter some random character = " << endl;
    cin >> givencharacter;
    cout << endl;
    // 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 star character with space in the
            // inner For loop.
            cout << givencharacter << " ";
        }
        // Print the newline character after ending of inner
        // For loop.
        cout << endl;
    }
    return 0;
}

Output:

Enter some random number of rows of the right-angled triangle = 
7
Enter some random character = 
&
& 
& & 
& & & 
& & & & 
& & & & & 
& & & & & & 
& & & & & & & 

3) C Implementation

  • Give the number of rows of the right-angled triangle 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()
{

    int trianglerows;
    char givencharacter;
    // Give the number of rows of the right-angled triangle
    // as user input using cin and store it in a variable.
    scanf("%d", &trianglerows);

    // Give the Character as user input using cin and store
    // it in another variable.
    scanf("%c", &givencharacter);
    printf("\n");
    // 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 star character with space in the
            // inner For loop.
            printf("%c ", givencharacter);
        }
        // Print the newline character after ending of inner
        // For loop.
        printf("\n");
    }
    return 0;
}

Output:

11^
^ 
^ ^ 
^ ^ ^ 
^ ^ ^ ^ 
^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^

Related Programs:

Python Program to Print Hollow Diamond Star Pattern

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Given the number of rows of the diamond pattern, the task is to print the Hollow diamond star pattern in C, C++, and Python

Examples:

Example1:

Input:

given number of rows of diamond =6

Output:

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

Example2:

Input:

given number of rows of diamond =10
given character to print ='^'

Output:

          ^
        ^ ^
       ^   ^
      ^     ^
     ^       ^
    ^         ^
   ^           ^
  ^             ^
 ^               ^
^                 ^
 ^               ^
  ^             ^
   ^           ^
    ^         ^
     ^       ^
      ^     ^
       ^   ^
        ^ ^
         ^

Program to Print Diamond Star Pattern in C, C++, and Python

Below are the ways to print Diamond Star Pattern in C, C++, and Python.

Method #1: Using For loop (Star Character)

Approach:

  • Give the number of rows of the diamond 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 the number of rows -iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the space character in the inner For loop.
  • Loop from 1 to the 2* iterator value of the parent For loop using another For loop(Nested For loop).
  • If the value of the iterator is equal to 1 or 2* iterator value of the parent loop -1 then print star character.
  • Else print space character.
  • After the end of the inner for Loops print the Newline Character.
  • loop from the number of rows -1 to 0 in decreasing order using For loop.
  • Loop from 1 to number of rows- iterator value of the parent loop using another For loop(Nested For loop)
  • Print the space character in the inner For loop.
  • Loop from 1 to the 2* iterator value of the parent For loop using another For loop(Nested For loop).
  • If the value of the iterator is equal to 1 or 2* iterator value of the parent loop -1 then print star character.
  • Else print space character.
  • After the end of the inner for Loops 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.
diamondrows = 6

# Loop from 1 to the number of rows using For Loop.
for m in range(1, diamondrows+1):
    # Loop from 1 to the number of rows -iterator value of the parent
    # For loop using another For loop(Nested For loop).
    for n in range(1, diamondrows - m + 1):
        # Print the space character in the inner For loop.
        print(end=' ')
    # Loop from 1 to the 2* iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(1, (2 * m)):
        # If the value of the iterator is equal to 1 or 2* iterator value
        # of the parent loop -1 then print star character.
        if l == 1 or l == m * 2 - 1:
            print('*', end='')
        # Else print space character.
        else:
            print(' ', end='')
    # After the end of the inner for Loops print the Newline Character.
    print()

# loop from number of rows -1 to 0 in decreasing order using For loop.
for m in range(diamondrows - 1, 0, -1):
    # Loop from 1 to number of rows- iterator value
    # of the parent loop using another For loop(Nested For loop)
    for n in range(1, diamondrows - m + 1):
        # Print the space character in the inner For loop.
        print(end=' ')
    # Loop from 1 to the 2* iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(1, (2 * m)):
        # If the value of the iterator is equal to 1 or 2* iterator value
        # of the parent loop -1 then print star character.
        if l == 1 or l == m * 2 - 1:
            print('*', end='')
        # Else print space character.
        else:
            print(' ', end='')

    # After the end of the inner for Loops print the Newline Character.
    print(

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows of the diamond pattern
    // as static input and store it in a variable.
    int diamondrows = 6;
    char givencharacter = '*';
    // Loop from 1 to the number of rows using For Loop.
    for (int m = 1; m <= diamondrows; m++) {

        // Loop from 1 to the number of rows- iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 1; n <= (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 1 to the 2* iterator value of the
        // parent For loop
        // using another For loop(Nested For loop).
        for (int l = 1; l < (2 * m); l++) {
            // If the value of the iterator is equal to 1 or
            // 2* iterator value of the parent loop -1 then
            // print givencharacter .
            if (l == 1 || l == m * 2 - 1)
                cout << givencharacter;
            // Else print space character.
            else
                cout << " ";
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        cout << endl;
    }
  // loop from number of rows -1 to 0 in decreasing order using For loop.

    for (int m = diamondrows-1; m > 0; m--) {
    //Loop from 1 to number of rows- iterator value
    // of the parent loop using another For loop(Nested For loop)
        for (int n = 1; n <= diamondrows-m; n++) {
            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 1 to the 2* iterator value of the
        // parent For loop
        // using another For loop(Nested For loop).
        for (int l = 1; l < (2 * m); l++) {
            // If the value of the iterator is equal to 1 or
            // 2* iterator value of the parent loop -1 then
            // print givencharacter .
            if (l == 1 || l == m * 2 - 1)
                cout << givencharacter;
            // Else print space character.
            else
                cout << " ";
        }

        // After the end of the inner for Loops print the
        // Newline    Character.
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{
    char givencharacter = '*';
    // Give the number of rows of the diamond pattern
    // as static input and store it in a variable.
    int diamondrows = 6;
    // Loop from 1 to the number of rows using For Loop.
    for (int m = 1; m <= diamondrows; m++) {

        // Loop from 1 to the number of rows- iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 1; n <= (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 1 to the 2* iterator value of the
        // parent For loop
        // using another For loop(Nested For loop).
        for (int l = 1; l < (2 * m); l++) {
            // If the value of the iterator is equal to 1 or
            // 2* iterator value of the parent loop -1 then
            // print givencharacter .
            if (l == 1 || l == m * 2 - 1)
                printf("%c", givencharacter);
            // Else print space character.
            else
                printf(" ");
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        printf("\n");
    }
    // loop from number of rows -1 to 0 in decreasing order
    // using For loop.

    for (int m = diamondrows - 1; m > 0; m--) {
        // Loop from 1 to number of rows- iterator value
        // of the parent loop using another For loop(Nested
        // For loop)
        for (int n = 1; n <= diamondrows - m; n++) {
            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 1 to the 2* iterator value of the
        // parent For loop
        // using another For loop(Nested For loop).
        for (int l = 1; l < (2 * m); l++) {
            // If the value of the iterator is equal to 1 or
            // 2* iterator value of the parent loop -1 then
            // print givencharacter .
            if (l == 1 || l == m * 2 - 1)
                printf("%c", givencharacter);
            // Else print space character.
            else
                printf(" ");
        }

        // After the end of the inner for Loops print the
        // Newline    Character.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For loop (User Character)

Approach:

  • Give the number of rows of the diamond pattern as user input and store it in a variable.
  • Give the character to print 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 rows -iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the space character in the inner For loop.
  • Loop from 1 to the 2* iterator value of the parent For loop using another For loop(Nested For loop).
  • If the value of the iterator is equal to 1 or 2* iterator value of the parent loop -1 then print given character.
  • Else print space character.
  • After the end of the inner for Loops print the Newline Character.
  • loop from the number of rows -1 to 0 in decreasing order using For loop.
  • Loop from 1 to number of rows- iterator value of the parent loop using another For loop(Nested For loop)
  • Print the space character in the inner For loop.
  • Loop from 1 to the 2* iterator value of the parent For loop using another For loop(Nested For loop).
  • If the value of the iterator is equal to 1 or 2* iterator value of the parent loop -1 then print given character.
  • Else print space character.
  • After the end of the inner for Loops 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.
  • 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.
diamondrows = int(input(
    'Enter some random number of rows  = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
# Loop from 1 to the number of rows using For Loop.
for m in range(1, diamondrows+1):
    # Loop from 1 to the number of rows -iterator value of the parent
    # For loop using another For loop(Nested For loop).
    for n in range(1, diamondrows - m + 1):
        # Print the space character in the inner For loop.
        print(end=' ')
    # Loop from 1 to the 2* iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(1, (2 * m)):
        # If the value of the iterator is equal to 1 or 2* iterator value
        # of the parent loop -1 then print givencharacter .
        if l == 1 or l == m * 2 - 1:
            print(givencharacter, end='')
        # Else print space character.
        else:
            print(' ', end='')
    # After the end of the inner for Loops print the Newline Character.
    print()

# loop from number of rows -1 to 0 in decreasing order using For loop.
for m in range(diamondrows - 1, 0, -1):
    # Loop from 1 to number of rows- iterator value
    # of the parent loop using another For loop(Nested For loop)
    for n in range(1, diamondrows - m + 1):
        # Print the space character in the inner For loop.
        print(end=' ')
    # Loop from 1 to the 2* iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(1, (2 * m)):
        # If the value of the iterator is equal to 1 or 2* iterator value
        # of the parent loop -1 then print givencharacter .
        if l == 1 or l == m * 2 - 1:
            print(givencharacter, end='')
        # Else print space character.
        else:
            print(' ', end='')

    # After the end of the inner for Loops print the Newline Character.
    print()

Output:

Enter some random number of rows = 10
Enter some random character = ^ 
          ^
        ^ ^
       ^   ^
      ^     ^
     ^       ^
    ^         ^
   ^           ^
  ^             ^
 ^               ^
^                 ^
 ^               ^
  ^             ^
   ^           ^
    ^         ^
     ^       ^
      ^     ^
       ^   ^
        ^ ^
         ^

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 diamondrows;
    char givencharacter;
    cout << "Enter some random number of rows = " << endl;
    cin >> diamondrows;
    // Give the Character as user input using cin and store
    // it in another variable.
    cout << "Enter some random character = " << endl;
    cin >> givencharacter;
    cout << endl;
    // Loop from 1 to the number of rows using For Loop.
    for (int m = 1; m <= diamondrows; m++) {

        // Loop from 1 to the number of rows- iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 1; n <= (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 1 to the 2* iterator value of the
        // parent For loop
        // using another For loop(Nested For loop).
        for (int l = 1; l < (2 * m); l++) {
            // If the value of the iterator is equal to 1 or
            // 2* iterator value of the parent loop -1 then
            // print givencharacter .
            if (l == 1 || l == m * 2 - 1)
                cout << givencharacter;
            // Else print space character.
            else
                cout << " ";
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        cout << endl;
    }
  // loop from number of rows -1 to 0 in decreasing order using For loop.

    for (int m = diamondrows-1; m > 0; m--) {
    //Loop from 1 to number of rows- iterator value
    // of the parent loop using another For loop(Nested For loop)
        for (int n = 1; n <= diamondrows-m; n++) {
            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 1 to the 2* iterator value of the
        // parent For loop
        // using another For loop(Nested For loop).
        for (int l = 1; l < (2 * m); l++) {
            // If the value of the iterator is equal to 1 or
            // 2* iterator value of the parent loop -1 then
            // print givencharacter .
            if (l == 1 || l == m * 2 - 1)
                cout << givencharacter;
            // Else print space character.
            else
                cout << " ";
        }

        // After the end of the inner for Loops print the
        // Newline    Character.
        cout << endl;
    }
    return 0;
}

Output:

Enter some random number of rows = 
10
Enter some random character = 
^ 
          ^
        ^ ^
       ^   ^
      ^     ^
     ^       ^
    ^         ^
   ^           ^
  ^             ^
 ^               ^
^                 ^
 ^               ^
  ^             ^
   ^           ^
    ^         ^
     ^       ^
      ^     ^
       ^   ^
        ^ ^
         ^

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 diamondrows;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.
    scanf("%d", &diamondrows);
    scanf("%c", &givencharacter);
    // Loop from 1 to the number of rows using For Loop.
    for (int m = 1; m <= diamondrows; m++) {

        // Loop from 1 to the number of rows- iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 1; n <= (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 1 to the 2* iterator value of the
        // parent For loop
        // using another For loop(Nested For loop).
        for (int l = 1; l < (2 * m); l++) {
            // If the value of the iterator is equal to 1 or
            // 2* iterator value of the parent loop -1 then
            // print givencharacter .
            if (l == 1 || l == m * 2 - 1)
                printf("%c", givencharacter);
            // Else print space character.
            else
                printf(" ");
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        printf("\n");
    }
    // loop from number of rows -1 to 0 in decreasing order
    // using For loop.

    for (int m = diamondrows - 1; m > 0; m--) {
        // Loop from 1 to number of rows- iterator value
        // of the parent loop using another For loop(Nested
        // For loop)
        for (int n = 1; n <= diamondrows - m; n++) {
            // Print the space character in the inner For
            // loop.
           printf(" ");
        }
        // Loop from 1 to the 2* iterator value of the
        // parent For loop
        // using another For loop(Nested For loop).
        for (int l = 1; l < (2 * m); l++) {
            // If the value of the iterator is equal to 1 or
            // 2* iterator value of the parent loop -1 then
            // print givencharacter .
            if (l == 1 || l == m * 2 - 1)
                printf("%c", givencharacter);
            // Else print space character.
            else
                printf(" ");
        }

        // After the end of the inner for Loops print the
        // Newline    Character.
        printf("\n");
    }
    return 0;
}

Output:

10^
          ^
        ^ ^
       ^   ^
      ^     ^
     ^       ^
    ^         ^
   ^           ^
  ^             ^
 ^               ^
^                 ^
 ^               ^
  ^             ^
   ^           ^
    ^         ^
     ^       ^
      ^     ^
       ^   ^
        ^ ^
         ^

Related Programs:

Python Program to Print Hollow Box Pattern of Numbers

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Given the number of rows and number of columns of the box, the task is to print the Hollow Box Number pattern in C, C++, and python.

Examples:

Example1:

Input:

given number of rows of the box =11
given number of columns of the box =19

Output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Example2:

Input:

given number of rows of the box =11
given number of columns of the box =19
given character to print ='&'

Output:

& & & & & & & & & & & & & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
& & & & & & & & & & & & &

Program to Print Hollow Box Number Pattern in C, C++, and Python

Below are the ways to Print Hollow Box Number Pattern in C, C++, and python.

Method #1: Using For Loop (Static Character)

Approach:

  • Give the number of rows and number of columns of the box as static or user input and store them in two separate variables.
  • Loop from 1 to the number of rows of the box +1 using a For loop.
  • Loop from 1 to the number of columns of the box +1 using another Nested For loop.
  • Check if the parent iterator value is equal to 1 or the number of rows of the box using the If statement.
  • Check if the inner loop iterator value is equal to 1 or the number of columns of the box using the If statement.
  • Merge these two conditions in a single If statement using or operator.
  • If the condition is true then print 1 with a space character.
  • Else print space character.
  • Print the newline character after the end of the inner For loop.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows and number of columns of the box as static or user input
# and store them in two separate variables.
boxrows = 11
boxcolumns = 19
# Loop from 1 to the number of rows of the box +1 using a For loop.
for m in range(1, boxrows+1):
    # Loop from 1 to the number of columns of the box +1 using another Nested For loop.
    for n in range(1, boxcolumns+1):
        '''Check if the parent iterator value is equal to 1 or the number of rows of the box using the If statement.
            Check if the inner loop iterator value is equal to 1 or the number of columns of the box using the If statement.
            Merge these two conditions in a single If statement using or operator.       
                  '''
        if(m == 1 or m == boxrows or n == 1 or n == boxcolumns):
            # If the condition is true then print 1 with a space character.
            print("1", end=" ")
        else:
            # Else print space character.
            print(" ", end=" ")
    # Print the newline character after the end of the inner For loop.
    print()

Output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{
    // Give the number of rows and number of columns of the
    // box as static or user input and store them in two
    // separate variables.
    int boxrows = 11;
    int boxcolumns = 19;
    // Loop from 1 to the number of rows of the box +1 using
    // a For loop.
    for (int m = 1; m <= boxrows; m++) {
        // Loop from 1 to the number of columns of the box
        // +1 using another Nested For loop.
        for (int n = 1; n <= boxcolumns; n++) {
            /*Check if the parent iterator value is equal to
               1 or the number of rows of the box using the
               If statement. Check if the inner loop
               iterator value is equal to 1 or the number of
               columns of the box using the If statement.
                Merge these two conditions in a single If
               statement using or operator. */
            if (m == 1 || m == boxrows || n == 1
                || n == boxcolumns) {
                // If the condition is true then print 1
                // with a space character.
                cout << "1 ";
            }
            else {
                //  Else print space character.
                cout << "  ";
            }
        }
        // Print the newline character after the end of the
        // inner For loop.
        cout << endl;
    }

    return 0;
}

Output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows and number of columns of the
    // box as static or user input and store them in two
    // separate variables.
    int boxrows = 11;
    int boxcolumns = 19;
    // Loop from 1 to the number of rows of the box +1 using
    // a For loop.
    for (int m = 1; m <= boxrows; m++) {
        // Loop from 1 to the number of columns of the box
        // +1 using another Nested For loop.
        for (int n = 1; n <= boxcolumns; n++) {
            /*Check if the parent iterator value is equal to
               1 or the number of rows of the box using the
               If statement. Check if the inner loop
               iterator value is equal to 1 or the number of
               columns of the box using the If statement.
                Merge these two conditions in a single If
               statement using or operator. */
            if (m == 1 || m == boxrows || n == 1
                || n == boxcolumns) {
                // If the condition is true then print 1
                // with a space character.
                printf("1 ");
            }
            else {
                // Else print space character.
                printf("  ");
            }
        }
        // Print the newline character after the end of the
        // inner For loop.
        printf("\n");
    }
    return 0;
}

Output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Method #2: Using For Loop (User Character)

Approach:

  • Give the number of rows and number of columns of the box as user input and store them in two separate variables.
  • Give the character to print as a box pattern as user input and store it in a variable.
  • Loop from 1 to the number of rows of the box +1 using a For loop.
  • Loop from 1 to the number of columns of the box +1 using another Nested For loop.
  • Check if the parent iterator value is equal to 1 or the number of rows of the box using the If statement.
  • Check if the inner loop iterator value is equal to 1 or the number of columns of the box using the If statement.
  • Merge these two conditions in a single If statement using or operator.
  • If the condition is true then print the given character with a space character.
  • Else print space character.
  • Print the newline character after the end of the inner For loop.
  • The Exit of the Program.

1) Python Implementation

  • Give the number of rows of the box as user input using int(input()) and store it in a variable.
  • Give the number of columns of the box as user input using int(input()) and store it in another variable.
  • Give the Character as user input using input() and store it in another variable.

Below is the implementation:

# Give the number of rows and number of columns of the box as user input and store them in two separate variables.
boxrows = int(input("Enter some random number of rows of the box = "))
boxcolumns = int(input("Enter some random number of columns of the box = "))
# Give the character to print as a box pattern as user input and store it in a variable.
userchar = input('Enter some random character to print = ')
# Loop from 1 to the number of rows of the box +1 using a For loop.
for m in range(1, boxrows+1):
    # Loop from 1 to the number of columns of the box +1 using another Nested For loop.
    for n in range(1, boxcolumns+1):
        '''Check if the parent iterator value is equal to 1 or the number of rows of the box using the If statement.
            Check if the inner loop iterator value is equal to 1 or the number of columns of the box using the If statement.
            Merge these two conditions in a single If statement using or operator.       
                  '''
        if(m == 1 or m == boxrows or n == 1 or n == boxcolumns):
            # If the condition is true then print the given character with a space character.
            print(userchar, end=" ")
        else:
            # Else print space character.
            print(" ", end=" ")
    # Print the newline character after the end of the inner For loop.
    print()

Output:

Enter some random number of rows of the box = 9
Enter some random number of columns of the box = 13
Enter some random character to print = &
& & & & & & & & & & & & & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
& & & & & & & & & & & & &

2) C++ Implementation

  • Give the number of rows of the box as user input using cin and store it in a variable.
  • Give the number of columns of the box as user input using cin and store it in another 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 of the box as user input
    // using cin and store it in a variable.
    // Give the number of columns of the box as user input
    // using cin and store it in another variable.
    int boxrows;
    int boxcolumns;
    cin >> boxrows;
    cin >> boxcolumns;
    // Give the Character as user input using cin and store
    // it in another variable.
    char userchar;
    cin >> userchar;
    cout << endl;
    // Loop from 1 to the number of rows of the box +1 using
    // a For loop.
    for (int m = 1; m <= boxrows; m++) {
        // Loop from 1 to the number of columns of the box
        // +1 using another Nested For loop.
        for (int n = 1; n <= boxcolumns; n++) {
            /*Check if the parent iterator value is equal to
               1 or the number of rows of the box using the
               If statement. Check if the inner loop
               iterator value is equal to 1 or the number of
               columns of the box using the If statement.
                Merge these two conditions in a single If
               statement using or operator. */
            if (m == 1 || m == boxrows || n == 1
                || n == boxcolumns) {
                // If the condition is true then print user
                // character with a space character.
                cout << userchar << " ";
            }
            else {
                // Else print space character.
                cout << "  ";
            }
        }
        // Print the newline character after the end of the
        // inner For loop.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows of the box = 9
Enter some random number of columns of the box = 13
Enter some random character to print = &
& & & & & & & & & & & & & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
& & & & & & & & & & & & &

3) C Implementation

  • Give the number of rows of the box as user input using scanf and store it in a variable.
  • Give the number of columns of the box as user input using scanf and store it in another 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 of the box as user input
    // using scanf and store it in a variable.
    // Give the number of columns of the box as user input
    // using scanf and store it in another variable.
    int boxrows;
    int boxcolumns;
    // Give the Character as user input using cin and store
    // it in another variable.
    char userchar;
    scanf("%d%d%c",&boxrows,&boxcolumns,&userchar);
    printf("\n");
    // Loop from 1 to the number of rows of the box +1 using
    // a For loop.
    for (int m = 1; m <= boxrows; m++) {
        // Loop from 1 to the number of columns of the box
        // +1 using another Nested For loop.
        for (int n = 1; n <= boxcolumns; n++) {
            /*Check if the parent iterator value is equal to
               1 or the number of rows of the box using the
               If statement. Check if the inner loop
               iterator value is equal to 1 or the number of
               columns of the box using the If statement.
                Merge these two conditions in a single If
               statement using or operator. */
            if (m == 1 || m == boxrows || n == 1
                || n == boxcolumns) {
                // If the condition is true then print user
                // character with a space character.
                printf("%c ", userchar);
            }
            else {
                // Else print space character.
                printf("  ");
            }
        }
        // Print the newline character after the end of the
        // inner For loop.
        printf("\n");
    }
    return 0;
}

Output:

Enter some random number of rows of the box = 9
Enter some random number of columns of the box = 13
Enter some random character to print = &
& & & & & & & & & & & & & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
& & & & & & & & & & & & &

Related Programs:

 

Python Program to Print Hollow Right Triangle Star Pattern

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output

Given the number of rows of the right-angled Hollow triangle star pattern in C, C++, and python.

Examples:

Example1:

Input:

given number of rows of the hollow right-angled triangle star pattern=11

Output:

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

Example2:

Input:

given number of rows of the hollow right-angled triangle star pattern=8
given character to print =^

Output

^ 
^  ^ 
^      ^ 
^          ^ 
^             ^ 
^                ^ 
^                   ^ 
^ ^ ^ ^ ^ ^ ^ ^

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

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

Method #1: Using For loop (Star Character)

Approach:

  • Give the number of rows of the hollow right-angled triangle star 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.
  • If you closely examine the pattern, you will notice that the star is available on the first or last column or row. So, for the first or last column or row, print a star, otherwise, print space.
  • 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 hollow right-angled triangle star pattern
# as static input and store it in a variable.
trianglerows = 7
# Loop from 1 to the number of rows using For loop.
for m in range(1, trianglerows+1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
        # If you closely examine the pattern, you will notice that the
        # star is available on the first or last column or row.
        # So, for the first or last column or row, print a star, otherwise, print space.
        if(m == 1 or m == trianglerows or n == 1 or n == m):
           print('*', end=' ')
        else:
           print(' ', end=' ')
    # Print the newline character after ending of inner For loop.
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows of the right-angled triangle
    // star pattern as static input and store it in a
    // variable.
    int trianglerows = 11;
    // 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++) {
            // If you closely examine the pattern, you will
            // notice that the
            // star is available on the first or last column
            // or row.
            // So, for the first or last column or row,
            // print a star, otherwise, print space.
            if (m == 1 || m == trianglerows || n == 1
                || n == m)
                cout << "* ";
            else
                cout << "  ";
        }
        // Print the newline character after ending of inner
        // For loop.
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows of the right-angled triangle
    // star pattern as static input and store it in a
    // variable.
    int trianglerows = 7;
    // 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++) {
            // If you closely examine the pattern, you will
            // notice that the
            // star is available on the first or last column
            // or row.
            // So, for the first or last column or row,
            // print a star, otherwise, print space.
            if (m == 1 || m == trianglerows || n == 1
                || n == m)
                printf("* ");
            else
                printf("  ");
        }
        // Print the newline character after ending of inner
        // For loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For loop (User Character )

Approach:

  • Give the number of rows of the right-angled triangle star 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.
  • If you closely examine the pattern, you will notice that the star is available on the first or last column or row. So, for the first or last column or row, print a star, otherwise, print space.
  • 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 hollow right-angled triangle 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 of the right-angled triangle as user input using int(input()) and store it in a variable.
trianglerows = int(
    input('Enter some random number of rows of the right-angled triangle = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
for m in range(1, trianglerows+1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
         # If you closely examine the pattern, you will notice that the
        # star is available on the first or last column or row.
        # So, for the first or last column or row, print a star, otherwise, print space.
        if(m == 1 or m == trianglerows or n == 1 or n == m):
            print(givencharacter, end=' ')
        else:
            print(' ', end=' ')
    # Print the newline character after ending of inner For loop.
    print()

Output

Enter some random number of rows of the right-angled triangle = 8
Enter some random character = ^
^ 
^  ^ 
^      ^ 
^          ^ 
^             ^ 
^                ^ 
^                   ^ 
^ ^ ^ ^ ^ ^ ^ ^

2) C++ Implementation

  • Give the number of rows of the right-angled triangle 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()
{
    int trianglerows;
    char givencharacter;

    // Give the number of rows of the right-angled triangle
    // as user input using cin and store it in a variable.
    cout << "Enter some random number of rows of the "
            "right-angled triangle = "
         << endl;
    cin >> trianglerows;
    // Give the Character as user input using cin and store
    // it in another variable.
    cout << "Enter some random character = " << endl;
    cin >> givencharacter;
    cout << endl;
    // 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++) {
            // If you closely examine the pattern, you will
            // notice that the
            // star is available on the first or last column
            // or row.
            // So, for the first or last column or row,
            // print a star, otherwise, print space.
            if (m == 1 || m == trianglerows || n == 1
                || n == m)
                cout << givencharacter << " ";
            else
                cout << "  ";
        }
        // Print the newline character after ending of inner
        // For loop.
        cout << endl;
    }
    return 0;
}

Output

Enter some random number of rows of the right-angled triangle = 8
Enter some random character = ^
^ 
^  ^ 
^      ^ 
^          ^ 
^             ^ 
^                ^ 
^                   ^ 
^ ^ ^ ^ ^ ^ ^ ^

3) C Implementation

  • Give the number of rows of the right-angled triangle 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()
{

    int trianglerows;
    char givencharacter;
    // Give the number of rows of the right-angled triangle
    // as user input using cin and store it in a variable.
    scanf("%d", &trianglerows);

    // Give the Character as user input using cin and store
    // it in another variable.
    scanf("%c", &givencharacter);
    printf("\n");
    // 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++) {
            // If you closely examine the pattern, you will
            // notice that the
            // star is available on the first or last column
            // or row.
            // So, for the first or last column or row,
            // print a star, otherwise, print space.
            if (m == 1 || m == trianglerows || n == 1
                || n == m)
                printf("%c ", givencharacter);
            else
                printf("  ");
        }
        // Print the newline character after ending of inner
        // For loop.
        printf("\n");
    }
    return 0;
}

Output

8^
^ 
^  ^ 
^      ^ 
^          ^ 
^             ^ 
^                ^ 
^                   ^ 
^ ^ ^ ^ ^ ^ ^ ^

Related Programs:

Python Program to Print Plus Star Pattern

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Given the number of rows, the task is to print plus star Patterns in C, C++, and Python.

Examples:

Example1:

Input:

given number of rows = 9

Output:

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

Example2:

Input:

given number of rows =5
given character to print ='<'

Output:

5<
              < 
              < 
              < 
              < 
< < < < < < < < < 
              < 
              < 
              < 
              <

Program to Print Plus Star Pattern in C, C++, and Python

Below are the ways to Print Plus Star Pattern 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.
  • Loop from 1 to 2*number of rows using For loop.
  • Loop from 1 to 2*number of rows using another For loop(Nested For loop).
  • Check if the parent loop iterator value is equal to the number of rows using the If statement.
  • Check if the inner loop iterator value is equal to the number of rows using the If statement.
  • Merge these both statements using or operator.
  • If this statement is true then print star character.
  • Else print space character.
  • Print the newline Character after the end of the inner for 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.
numberows = 9
# Loop from 1 to 2*number of rows using For loop.
for m in range(1, 2*numberows):
    # Loop from 1 to 2*number of rows using another For loop(Nested For loop).
    for n in range(1, 2*numberows):
        '''Check if the parent loop iterator value is equal to the number of rows using the If statement.
        Check if the inner loop iterator value is equal to the number of rows using the If statement.
        Merge these both statements using or operator.'''
        if(m == numberows or n == numberows):
            # If this statement is true then print star character.
            print('*', end=' ')
        else:
            # Else print space character.
            print(' ', end=' ')

    # Print the newline character after the end of the inner For loop.
    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 numberows = 9;

    // # Loop from 1 to 2*number of rows using For loop.
    for (int m = 1; m < 2 * numberows; m++) {
        // Loop from 1 to 2*number of rows using another For
        // loop(Nested For loop).
        for (int n = 1; n < 2 * numberows; n++) {
            /*Check if the parent loop iterator value is
         equal to the number of rows using the If statement.
         Check if the inner loop iterator value is equal to
         the number of rows using the If statement. Merge
         these both statements using or operator.*/
            if (m == numberows || n == numberows)
                // If this statement is true then print star
                // character.
                cout << "* ";
            else
                // Else print space character.
                cout << "  ";
        }
        // Print the newline character after the end of
        // the inner For loop.
        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 numberows = 9;

    // # Loop from 1 to 2*number of rows using For loop.
    for (int m = 1; m < 2 * numberows; m++) {
        // Loop from 1 to 2*number of rows using another For
        // loop(Nested For loop).
        for (int n = 1; n < 2 * numberows; n++) {
            /*Check if the parent loop iterator value is
         equal to the number of rows using the If statement.
         Check if the inner loop iterator value is equal to
         the number of rows using the If statement. Merge
         these both statements using or operator.*/
            if (m == numberows || n == numberows)
                // If this statement is true then print star
                // character.
                printf("* ");
            else
                // Else print space character.
                printf("  ");
        }
        // Print the newline character after the end of
        // the inner For loop.
        printf("\n");
    }

    return 0;
}

Output:

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

Method #2: Using For Loop (User Character)

Approach:

  • Give the number of rows as user input and store it in a variable.
  • Give the character as user input and store it in a variable.
  • Loop from 1 to 2*number of rows using For loop.
  • Loop from 1 to 2*number of rows using another For loop(Nested For loop).
  • Check if the parent loop iterator value is equal to the number of rows using the If statement.
  • Check if the inner loop iterator value is equal to the number of rows using the If statement.
  • Merge these both statements using or operator.
  • If this statement is true then print the given character.
  • Else print space character.
  • Print the newline Character after the end of the inner for 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 word as user input using int(input()) and store it in a variable.
numberows = int(input('Enter some random number of rows = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
# Loop from 1 to 2*number of rows using For loop.
for m in range(1, 2*numberows):
    # Loop from 1 to 2*number of rows using another For loop(Nested For loop).
    for n in range(1, 2*numberows):
        '''Check if the parent loop iterator value is equal to the number of rows using the If statement.
        Check if the inner loop iterator value is equal to the number of rows using the If statement.
        Merge these both statements using or operator.'''
        if(m == numberows or n == numberows):
            # If this statement is true then print givencharacter
            print(givencharacter, end=' ')
        else:
            # Else print space character.
            print(' ', end=' ')

    # Print the newline character after the end of the inner For loop.
    print()

Output:

Enter some random number of rows = 5
Enter some random character = <
              < 
              < 
              < 
              < 
< < < < < < < < < 
              < 
              < 
              < 
              <

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;
    char givencharacter;
    cout << "Enter some random number of rows = " << endl;
    cin >> numberows;
    // Give the Character as user input using cin and store
    // it in another variable.
    cout << "Enter some random character = " << endl;
    cin >> givencharacter;
    cout << endl;

    // # Loop from 1 to 2*number of rows using For loop.
    for (int m = 1; m < 2 * numberows; m++) {
        // Loop from 1 to 2*number of rows using another For
        // loop(Nested For loop).
        for (int n = 1; n < 2 * numberows; n++) {
            /*Check if the parent loop iterator value is
         equal to the number of rows using the If statement.
         Check if the inner loop iterator value is equal to
         the number of rows using the If statement. Merge
         these both statements using or operator.*/
            if (m == numberows || n == numberows)
                // If this statement is true then print
                // givencharacter

                cout << givencharacter << " ";
            else
                // Else print space character.
                cout << "  ";
        }
        // Print the newline character after the end of
        // the inner For loop.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows = 5
Enter some random character = <
              < 
              < 
              < 
              < 
< < < < < < < < < 
              < 
              < 
              < 
              <

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 numberows;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.
    scanf("%d", &numberows);
    scanf("%c", &givencharacter);
    printf("\n");

    // # Loop from 1 to 2*number of rows using For loop.
    for (int m = 1; m < 2 * numberows; m++) {
        // Loop from 1 to 2*number of rows using another For
        // loop(Nested For loop).
        for (int n = 1; n < 2 * numberows; n++) {
            /*Check if the parent loop iterator value is
         equal to the number of rows using the If statement.
         Check if the inner loop iterator value is equal to
         the number of rows using the If statement. Merge
         these both statements using or operator.*/
            if (m == numberows || n == numberows)
                // If this statement is true then print
                // givencharacter
                printf("%c ", givencharacter);
            else
                // Else print space character.
                printf("  ");
        }
        // Print the newline character after the end of
        // the inner For loop.
        printf("\n");
    }

    return 0;
}

Output:

5<
              < 
              < 
              < 
              < 
< < < < < < < < < 
              < 
              < 
              < 
              <

Related Programs:

Python Program to Print Exponentially Increasing Star 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, the task is to print Exponentially Increasing Star Pattern in C, C++, and Python

Examples:

Example1:

Input:

given number of rows =5

Output:

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

Example2:

Input:

given number of rows =4
given character to print =@

Output:

@ 
@ @ 
@ @ @ @ 
@ @ @ @ @ @ @ @ 
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @

Program to Print Exponentially Increasing Star Pattern in C, C++, and Python

Below are the ways to print Exponentially Increasing Star Pattern in C, C++, and python.

Method #1: Using For loop (Star Character)

Approach:

  • Give the number of rows of the pattern as static input and store it in a variable.
  • Loop till a given number of rows using For loop.
  • Calculate the exponential value of the first loop iterator value using 2**(iterator value of the first loop).
  • Loop till the exponential value using another For loop(Nested For loop).
  • Print the star character in the inner for loop.
  • Print the newline character after the end of the inner for loop.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the pattern as static input and store it in a variable.
rowsnumb = 5
# Loop till a given number of rows using For loop.
for m in range(rowsnumb+1):
  # Calculate the exponential value of the first loop iterator
  # value using 2**(iterator value of the first loop).
    expvalue = 2**m
    # Loop till the exponential value using another For loop(Nested For loop).
    for n in range(expvalue):
      # Print the star character in the inner for loop.
        print("*", end=" ")
    # Print the newline character after the end of the inner for loop.
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

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

int main()
{
    // Give the number of rows of the pattern as static
    // input and store it in a variable.
    int rowsnumb = 5;
    // Loop till a given number of rows using For loop.
    for (int m = 0; m <= rowsnumb; m++) {
        // Calculate the exponential value of the first loop
        // iterator value using 2**(iterator value of the
        // first loop).
        int expvalue = pow(2, m);
        // Loop till the exponential value using another For
        // loop(Nested For loop).
        for (int n = 1; n <= expvalue; n++) {
            // Print the star character in the inner for
            // loop.
            cout << "* ";
        }
        // Print the newline character after the end of the
        // inner for loop.
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <math.h>
#include <stdio.h>
int main()
{
    // Give the number of rows of the pattern as static
    // input and store it in a variable.
    int rowsnumb = 5;
    // Loop till a given number of rows using For loop.
    for (int m = 0; m <= rowsnumb; m++) {
        // Calculate the exponential value of the first loop
        // iterator value using 2**(iterator value of the
        // first loop).
        int expvalue = pow(2, m);
        // Loop till the exponential value using another For
        // loop(Nested For loop).
        for (int n = 1; n <= expvalue; n++) {
            // Print the star character in the inner for
            // loop.
            printf("* ");
        }
        // Print the newline character after the end of the
        // inner for loop.
        printf("\n");
    }

    return 0;
}

Output:

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

Method #2: Using For loop (User Character)

Approach:

  • Give the number of rows of the pattern as user input and store it in a variable.
  • Give the character to print as user input and store it in a variable.
  • Loop till a given number of rows using For loop.
  • Calculate the exponential value of the first loop iterator value using 2**(iterator value of the first loop).
  • Loop till the exponential value using another For loop(Nested For loop).
  • Print the user character in the inner for loop.
  • Print the newline character after the end of the inner for loop.
  • The Exit of the Program.

1) Python Implementation

  • Give the number of rows 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 using int(input()) and store it in a variable.
rowsnumb = int(input('Enter some random number of rows = '))
# Give the Character as user input using input() and store it in another variable.
usercharact = input('Enter some random character to print = ')
for m in range(rowsnumb+1):
  # Calculate the exponential value of the first loop iterator
  # value using 2**(iterator value of the first loop).
    expvalue = 2**m
    # Loop till the exponential value using another For loop(Nested For loop).
    for n in range(expvalue):
      # Print the user character in the inner for loop.
        print(usercharact, end=" ")
    # Print the newline character after the end of the inner for loop.
    print()

Output:

Enter some random number of rows = 4
Enter some random character to print = @
@ 
@ @ 
@ @ @ @ 
@ @ @ @ @ @ @ @ 
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @

2) C++ Implementation

  • Give the number of rows 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>
#include <math.h>
using namespace std;

int main()
{
    // Give the number of rows of the pattern as user input
    // and store it in a variable.
    int rowsnumb;
    cin >> rowsnumb;
    // Give the character to print as user input and store
    // it in a variable.
    char usercharact;
    cin >> usercharact;
    cout<<endl;
    // Loop till a given number of rows using For loop.
    for (int m = 0; m <= rowsnumb; m++) {
        // Calculate the exponential value of the first loop
        // iterator value using 2**(iterator value of the
        // first loop).
        int expvalue = pow(2, m);
        // Loop till the exponential value using another For
        // loop(Nested For loop).
        for (int n = 1; n <= expvalue; n++) {
            // Print the user character in the inner for
            // loop.
            cout << usercharact << " ";
        }
        // Print the newline character after the end of the
        // inner for loop.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows = 4
Enter some random character to print = @
@ 
@ @ 
@ @ @ @ 
@ @ @ @ @ @ @ @ 
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @

3) C Implementation

  • Give the number of rows 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 <math.h>
#include <stdio.h>
int main()
{ // Give the number of rows of the pattern as user input
    // and store it in a variable.
    int rowsnumb;
    scanf("%d", &rowsnumb);
    // Give the character to print as user input and store
    // it in a variable.
    char usercharact;
    scanf("%c", &usercharact);
    // Loop till a given number of rows using For loop.
    for (int m = 0; m <= rowsnumb; m++) {
        // Calculate the exponential value of the first loop
        // iterator value using 2**(iterator value of the
        // first loop).
        int expvalue = pow(2, m);
        // Loop till the exponential value using another For
        // loop(Nested For loop).
        for (int n = 1; n <= expvalue; n++) {
            // Print the user character in the inner for
            // loop.
            printf("%c ", usercharact);
        }
        // Print the newline character after the end of the
        // inner for loop.
        printf("\n");
    }

    return 0;
}

Output:

@ 
@ @ 
@ @ @ @ 
@ @ @ @ @ @ @ @ 
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @

Related Programs:

Python Program to Print Mirrored Half Diamond Star Pattern

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Given the number of rows of the diamond pattern, the task is to print the Mirrored Half diamond Pattern in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows =9

Output:

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

Example2:

Input:

Given number of rows =7
Given Character to print ='+'

Output:

               +
            ++
          +++
        ++++
     +++++
   ++++++
+++++++
   ++++++
     +++++
       ++++
          +++
            ++
              +

Program to Print Mirrored Half Diamond Star Pattern in C, C++, and Python

Below are the ways to print Mirrored Half Diamond Star Pattern in C, C++, and python.

Method #1: Using For loop (Star Character)

Approach:

  • Give the number of rows of the number of diamond patterns as static input and store it in a variable.
  • Loop from 0 to the number of rows using For loop.
  • Loop from 0 to the number of rows -iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the space character in the inner For loop.
  • Loop from 0 to the iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the star character.
  • After the end of the inner for Loops print the Newline Character.
  • Loop from the number of rows to 0 in decreasing order using For loop.
  • Loop from 0 to the number of rows -iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the space character in the inner For loop.
  • Loop from 0 to the iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the star character.
  • After the end of the inner for Loops print the Newline Character.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the diamond pattern as static input and store it in a variable.
diamondrows = 9
# Loop from 0 to the number of rows using For Loop.
for m in range(0, diamondrows):
    # Loop from 0 to the number of rows -iterator value of the parent
    # For loop using another For loop(Nested For loop).
    for n in range(0, diamondrows - m):
        # Print the space character in the inner For loop.
        print(' ', end='')
    # Loop from 0 to the iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(0, m):
        # Print the star character
        print('*', end='')
    # After the end of the inner for Loops print the Newline Character.
    print()
# Loop from number of rows to 0 in decreasing order using For loop.
for m in range(diamondrows, 0, -1):
    # Loop from 0 to the number of rows -iterator value of the parent
    # For loop using another For loop(Nested For loop).
    for n in range(0, diamondrows - m):
        # Print the space character in the inner For loop.
        print(' ', end='')
    # Loop from 0 to the iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(0, m):
        # Print the star character
        print('*', end='')

    # After the end of the inner for Loops print the Newline Character.
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{
    // Give the number of rows of the diamond pattern
    // as static input and store it in a variable.

    int diamondrows = 9;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < diamondrows; m++) {

        // Loop from 0 to the number of rows -iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 0; n < (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another For loop(Nested For loop).
        for (int l = 0; l < m; l++) {
            // Print the star character
            cout << "*";
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        cout << endl;
    }
    // Loop from the number of rows to 0 in decreasing order
    // using For loop.
    for (int m = diamondrows; m > 0; m--) {
        // Loop from 0 to the number of rows -iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 0; n < (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another For loop(Nested For loop).
        for (int l = 0; l < m; l++) {
            // Print the star character
            cout << "*";
        }

        // After the end of the inner for Loops print the
        // Newline    Character.
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows of the diamond pattern
    // as static input and store it in a variable.

    int diamondrows = 9;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < diamondrows; m++) {

        // Loop from 0 to the number of rows -iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 0; n < (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another For loop(Nested For loop).
        for (int l = 0; l < m; l++) {
            // Print the star character
            printf("*");
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        printf("\n");
    }
    // Loop from the number of rows to 0 in decreasing order
    // using For loop.
    for (int m = diamondrows; m > 0; m--) {
        // Loop from 0 to the number of rows -iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 0; n < (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another For loop(Nested For loop).
        for (int l = 0; l < m; l++) {
            // Print the star character
            printf("*");
        }

        // After the end of the inner for Loops print the
        // Newline    Character.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For loop (User Character)

Approach:

  • Give the number of rows of the number of diamond patterns as static input and store it in a variable.
  • Give the character as user input and store it in a variable.
  • Loop from 0 to the number of rows using For loop.
  • Loop from 0 to the number of rows -iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the space character in the inner For loop.
  • Loop from 0 to the iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the star character.
  • After the end of the inner for Loops print the Newline Character.
  • Loop from the number of rows to 0 in decreasing order using For loop.
  • Loop from 0 to the number of rows -iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the space character in the inner For loop.
  • Loop from 0 to the iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the star character.
  • After the end of the inner for Loops 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.
  • 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.
diamondrows = int(input(
    'Enter some random number of rows  = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
# Loop from 0 to the number of rows using For Loop.
for m in range(0, diamondrows):
    # Loop from 0 to the number of rows -iterator value of the parent
    # For loop using another For loop(Nested For loop).
    for n in range(0, diamondrows - m):
        # Print the space character in the inner For loop.
        print(' ', end='')
    # Loop from 0 to the iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(0, m):
        # Print the givencharacter
        print(givencharacter, end='')
    # After the end of the inner for Loops print the Newline Character.
    print()
# Loop from number of rows to 0 in decreasing order using For loop.
for m in range(diamondrows, 0, -1):
    # Loop from 0 to the number of rows -iterator value of the parent
    # For loop using another For loop(Nested For loop).
    for n in range(0, diamondrows - m):
        # Print the space character in the inner For loop.
        print(' ', end='')
    # Loop from 0 to the iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(0, m):
         # Print the givencharacter
        print(givencharacter, end='')

    # After the end of the inner for Loops print the Newline Character.
    print()

Output:

Enter some random number of rows = 7
Enter some random character = +
               +
            ++
          +++
        ++++
     +++++
   ++++++
+++++++
   ++++++
     +++++
       ++++
          +++
            ++
              +

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 diamondrows;
    char givencharacter;
    cout << "Enter some random number of rows = " << endl;
    cin >> diamondrows;
    // Give the Character as user input using cin and store
    // it in another variable.
    cout << "Enter some random character = " << endl;
    cin >> givencharacter;
    cout << endl;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < diamondrows; m++) {

        // Loop from 0 to the number of rows -iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 0; n < (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another For loop(Nested For loop).
        for (int l = 0; l < m; l++) {
            // Print the givencharacter
            cout << givencharacter;
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        cout << endl;
    }
    // Loop from the number of rows to 0 in decreasing order
    // using For loop.
    for (int m = diamondrows; m > 0; m--) {
        // Loop from 0 to the number of rows -iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 0; n < (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another For loop(Nested For loop).
        for (int l = 0; l < m; l++) {
            // Print the givencharacter
            cout << givencharacter;
        }

        // After the end of the inner for Loops print the
        // Newline    Character.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows = 7
Enter some random character = +
               +
            ++
          +++
        ++++
     +++++
   ++++++
+++++++
   ++++++
     +++++
       ++++
          +++
            ++
              +

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 diamondrows;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.
    scanf("%d", &diamondrows);
    scanf("%c", &givencharacter);
    printf("\n");
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < diamondrows; m++) {

        // Loop from 0 to the number of rows -iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 0; n < (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another For loop(Nested For loop).
        for (int l = 0; l < m; l++) {
            // Print the givencharacter
            printf("%c", givencharacter);
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        printf("\n");
    }
    // Loop from the number of rows to 0 in decreasing order
    // using For loop.
    for (int m = diamondrows; m > 0; m--) {
        // Loop from 0 to the number of rows -iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 0; n < (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another For loop(Nested For loop).
        for (int l = 0; l < m; l++) {
            // Print the givencharacter
            printf("%c", givencharacter);
        }

        // After the end of the inner for Loops print the
        // Newline    Character.
        printf("\n");
    }
    return 0;
}

Output:

7+
               +
            ++
          +++
        ++++
     +++++
   ++++++
+++++++
   ++++++
     +++++
       ++++
          +++
            ++
              +

Related Programs: