CPP Programming

Python Program to Print Mirrored Right Triangle Star Pattern

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Given the number of rows of the right triangle, the task is to print Mirrored Right Triangle Star Pattern in C, C++, and Python.

Examples:

Example1:

Input:

given number of rows of the right triangle =6

Output:

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

Example2:

Input:

given number of rows of the right triangle =
Given character to print ='$'

Output:

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

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

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

Method #1: Using For loop (Star Character)

Approach:

  • Give the number of rows of the right triangle pattern as static input and store it in a variable.
  • Iterate from 1 to given rows using the First For loop.
  • Iterate from 1 to given rows using another for loop(Nested For loop)
  • Check if the iterator value of the inner for loop is less than or equal to given rows – first iterator value using If statement.
  • If the statement is true then print space.
  • Else print the star character with space.
  • Print the newline character after the exit of the inner for loop.
  • The Exit of the Program.

1) Python Implementation:

Below is the implementation:

# Give the number of rows of the right triangle pattern as static input and store it in a variable.
triNumRows = 6
# Iterate from 1 to given rows using the First for loop.
for m in range(1, triNumRows+1):
    # Iterate from 1 to given rows using another for loop(Nested For loop)
    for n in range(1, triNumRows+1):
        # Check if the iterator value of the inner for loop is less than or equal to given rows - first iterator value using If statement.
        if(n <= triNumRows - m):
            # If the statement is true then print space.
            print(' ', end=' ')
        else:
            # Else print star character with space.
            print('*', end=' ')
    #Print the newline character after the exit 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 of the right triangle pattern
    //  as static input and store it in a variable.
    int triNumRows = 6;
    // Iterate from 1 to given rows using First for loop.
    for (int m = 1; m <= triNumRows; m++) {
        // Iterate from 1 to given rows using another for
        // loop(Nested For loop)
        for (int n = 1; n <= triNumRows; n++) {
            // Check if the iterator value of the inner
            // for loop is less
            //       than
            //  or equal to given rows
            // first iterator value using If statement.
            if (n <= triNumRows - m) {
                // If the statement is true then print
                // space.
                cout << "  ";
            }
            else {
                // Else print star character with space.
                cout << "* ";
            }
        }
        // Print the newline character after the exit 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 right triangle pattern
    //  as static input and store it in a variable.
    int triNumRows = 6;
    // Iterate from 1 to given rows using First for loop.
    for (int m = 1; m <= triNumRows; m++) {
        // Iterate from 1 to given rows using another for
        // loop(Nested For loop)
        for (int n = 1; n <= triNumRows; n++) {
            // Check if the iterator value of the inner
            // for loop is less
            //       than
            //  or equal to given rows
            // first iterator value using If statement.
            if (n <= triNumRows - m) {
                // If the statement is true then print
                // space.
                printf("  ");
            }
            else {
                // Else print star character with space.
                printf("* ");
            }
        }
        // Print the newline character after the exit 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 right triangle pattern as user input and store it in a variable.
  • Give the character to print as user input and store it in a variable.
  • Iterate from 1 to given rows using the First For loop.
  • Iterate from 1 to given rows using another for loop(Nested For loop)
  • Check if the iterator value of the inner for loop is less than or equal to given rows – first iterator value using If statement.
  • If the statement is true then print space.
  • Else print the star character with space.
  • Print the newline character after the exit of the inner for loop.
  • The Exit of the Program.

1) Python Implementation:

  • Give the number of rows of the Right 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 Triangle as user input using int(input()) and store it in a variable.
triNumRows = int(input(
    'Enter some random number of rows of the Right Triangle Pattern = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
# Iterate from 1 to given rows using First for loop.
for m in range(1, triNumRows+1):
    # Iterate from 1 to given rows using another for loop(Nested For loop)
    for n in range(1, triNumRows+1):
        # Check if the iterator value of the inner for loop is less than or equal to given rows - first iterator value using If statement.
        if(n <= triNumRows - m):
            # If the statement is true then print space.
            print(' ', end=' ')
        else:
            # Else print star character with space.
            print(givencharacter, end=' ')
    print()

Output:

Enter some random number of rows of the Right Triangle Pattern = 6
Enter some random character = $
               $ 
            $ $ 
         $ $ $ 
      $ $ $ $ 
   $ $ $ $ $ 
$ $ $ $ $ $

2) C++ Implementation

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

    // Give the number of rows of the Right
    // Triangle as user input using cin and store it in a
    // variable.
    int triNumRows;
    char givencharacter;
    cout << "Enter some random number of rows of the "
            "Inverted Right Triangle Pattern = "
         << endl;
    cin >> triNumRows;
    // Give the Character as user input using cin and store
    // it in another variable.
    cout << "Enter some random character = " << endl;
    cin >> givencharacter;
    cout << endl;
    // Iterate from 1 to given rows using First for loop.
    for (int m = 1; m <= triNumRows; m++) {
        // Iterate from 1 to given rows using another for
        // loop(Nested For loop)
        for (int n = 1; n <= triNumRows; n++) {
            // Check if the iterator value of the inner
            // for loop is less
            //       than
            //  or equal to given rows
            // first iterator value using If statement.
            if (n <= triNumRows - m) {
                // If the statement is true then print
                // space.
                cout << "  ";
            }
            else {
                // Else print star character with space.
                cout <<givencharacter<<" ";
            }
        }
        // Print the newline character after the exit of the
        // inner for loop.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows of the Right Triangle Pattern = 
6
Enter some random character = 
$
               $ 
            $ $ 
         $ $ $ 
      $ $ $ $ 
   $ $ $ $ $ 
$ $ $ $ $ $

3) C Implementation

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

    // Give the number of rows of the  Right
    // Triangle as user input using scanf and store it in a
    // variable.
    int triNumRows;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.
    scanf("%d", &triNumRows);
    scanf("%c", &givencharacter);
    printf("\n");
    // Iterate from 1 to given rows using First for loop.
    for (int m = 1; m <= triNumRows; m++) {
        // Iterate from 1 to given rows using another for
        // loop(Nested For loop)
        for (int n = 1; n <= triNumRows; n++) {
            // Check if the iterator value of the inner
            // for loop is less
            //       than
            //  or equal to given rows
            // first iterator value using If statement.
            if (n <= triNumRows - m) {
                // If the statement is true then print
                // space.
                printf("  ");
            }
            else {
                // Else print star character with space.
                printf("%c ", givencharacter);
            }
        }
        // Print the newline character after the exit of the
        // inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

6$
               $ 
            $ $ 
         $ $ $ 
      $ $ $ $ 
   $ $ $ $ $ 
$ $ $ $ $ $

Related Programs:

Python Program to Print the Pyramid of Horizontal Number Tables

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

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

Examples:

Example1:

Input:

Given number of rows =9

Output:

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

Example2:

Input:

Given number of rows =13

Output:

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

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

Below are the ways to Print the Pyramid of Horizontal Number Tables in C, C++, and Python.

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to the number of 1 to parent loop iterator value using another For loop(Nested For Loop).
  • Print the value of the product of parent loop iterator value and inner loop iterator value with space.
  • After the end of the inner For loop print the Newline Character.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numberows = 9
# Loop from 1 to the number of rows using For loop.
for m in range(1, numberows+1):
    # Loop from 1 to the number of 1 to parent loop iterator value
    # using another For loop(Nested For Loop).
    for n in range(1, m+1):
        # Print the value of the product of parent loop iterator value
        # and inner loop iterator value with space.
        print(m*n, end=" ")
     # Print the newline character after the end of the inner For loop.
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

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

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

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

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

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

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

Output:

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

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to the number of 1 to parent loop iterator value using another For loop(Nested For Loop).
  • Print the value of the product of the parent loop iterator value and inner loop iterator value with space.
  • After the end of the inner For loop print the Newline Character.
  • The Exit of the Program.

1) Python Implementation

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

Below is the implementation:

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

Output:

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

2) C++ Implementation

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

Below is the implementation:

#include <iostream>
using namespace std;

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

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

    return 0;
}

Output:

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

3) C Implementation

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

Below is the implementation

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

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

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

Output:

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

Related Programs:

Python Program to Print Rhombus 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 rhombus, the task is to print the Rhombus star pattern in C, C++, and Python.

Examples:

Example1:

Input:

given number of rows of rhombus =19

Output:

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

Example2:

Input:

given number of rows of rhombus =9
given character to print =-

Output:

        ---------
       ---------
      ---------
     ---------
    ---------
   ---------
  ---------
 ---------
---------

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

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

Method #1: Using For Loop (Star Character)

Approach:

  • Give the number of rows of the rhombus as static input and store it in a variable.
  • Using Nested For loops print the rhombus star pattern.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the rhombus as static input and store it in a variable.
rhombusrows = 19
# Using Nested For loops print the rhombus star pattern.
for m in range(rhombusrows, 0, -1):
    for n in range(1, m):
        print(' ', end='')
    for k in range(0, rhombusrows):
        print('*', end='')
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main(void)
{
    // Give the number of rows of the rhombus as static
    // input and store it in a variable.
    int rhombusrows = 6;
    // Using Nested For loops print the rhombus star pattern.
    for (int m = rhombusrows; m > 0; m--) {
        for (int n = 1; n < m; n++) {
            cout << "  ";
        }
        for (int k = 0; k < rhombusrows; k++) {
            cout << "* ";
        }
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{
    // Give the number of rows of the rhombus as static
    // input and store it in a variable.
    int rhombusrows = 9;
    // Using Nested For loops print the rhombus star pattern.
    for (int m = rhombusrows; m > 0; m--) {
        for (int n = 1; n < m; n++) {
            printf("  ");
        }
        for (int k = 0; k < rhombusrows; k++) {
            printf("* ");
        }
        printf("\n");
    }
}

Output:

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

Method #2: Using For Loop (User Character)

Approach:

    • Give the number of rows of the rhombus as static input and store it in a variable.
    • Scan the character to print as user input and store it in a variable.
    • Using Nested For loops print the rhombus star pattern.
    • The Exit of the Program.

1) Python Implementation

  • Give the number of sides of the rhombus 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 sides of the rhombus as user input using int(input()) and store it in a variable.
# Give the Element as user input using int(input()) and store it in another variable.
rhombusrows = int(input('Enter some random number of rows of the rhombus= '))
characte = input('Enter some random character to print = ')
for m in range(rhombusrows, 0, -1):
    for n in range(1, m):
        print(' ', end='')
    for k in range(0, rhombusrows):
        print(characte, end='')
    print()

Output:

Enter some random number of rows of the rhombus= 3
Enter some random character to print = @
    @ @ @ 
  @ @ @ 
@ @ @

2) C++Implementation

  • Give the number of sides of the rhombus as user input using cin and store it in a variable.
  • Create a character 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(void)
{
    int rhombusrows;
    char characte;
    // Give the number of rows of the rhombus as user input
    // using cin and
    // store it in a variable.
    cout << "Enter some random number of rows of the "
            "rhombus = "
         << endl;
    cin >> rhombusrows;
    // Create a character variable.
    // Give the character as user input using cin and store
    // it in another variable.
    cout << "Enter some random character to print = "
         << endl;
    cin >> characte;
    cout << endl;
    // Using Nested For loops print the rhombus star pattern.
    for (int m = rhombusrows; m > 0; m--) {
        for (int n = 1; n < m; n++) {
            cout << "  ";
        }
        for (int k = 0; k < rhombusrows; k++) {
            cout << characte << " ";
        }
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows of the rhombus = 
19
Enter some random character to print = 
=
                  ===================
                 ===================
                ===================
               ===================
              ===================
             ===================
            ===================
           ===================
          ===================
         ===================
        ===================
       ===================
      ===================
     ===================
    ===================
   ===================
  ===================
 ===================
===================

3) C Implementation

  • Give the number of sides of the rhombus as user input using scanf and store it in a variable.
  • Create a character 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 rhombusrows;
    char characte;
    // Give the number of rows of the rhombus as user input
    // using scanf and
    // store it in a variable.
    // Create a character variable.
    // Give the character as user input using scanf and
    // store it in another variable.
    // Using Nested For loops print the rhombus pattern.
    scanf("%d%c", &rhombusrows, &characte);
    printf("\n");
    // Using Nested For loops print the rhombus star
    // pattern.
    for (int m = rhombusrows; m > 0; m--) {
        for (int n = 1; n < m; n++) {
            printf("  ");
        }
        for (int k = 0; k < rhombusrows; k++) {
            printf("%c ",characte);
        }
        printf("\n");
    }
}

Output:

9-
        ---------
       ---------
      ---------
     ---------
    ---------
   ---------
  ---------
 ---------
---------

Related Programs:

Python Program to Print Pattern to Display Letters of the Word

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Given a word the task is to print the letters of the word in C, C++, and Python.

Examples:

Example1:

Input:

Given string ="BTechGeeks"

Output:

B
BT
BTe
BTec
BTech
BTechG
BTechGe
BTechGee
BTechGeek
BTechGeeks

Example2:

Input:

Given string ="Aplustopper"

Output:

A
Ap
Apl
Aplu
Aplus
Aplust
Aplusto
Aplustop
Aplustopp
Aplustoppe
Aplustopper

Pattern to display letters of the word in C, C++, and Python.

Below are the ways to display letters of the word in C, C++, and Python.

Method #1: Using For loop (Static Input)

Approach:

  • Give the word as static input and store it in a variable.
  • Take an empty string and store it in a variable say sampstrng.
  • Traverse the given word(string) using For loop.
  • Concatenate the iterator character to the sampstrng using string Concatenation.
  • Print the sampstrng.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the word as static input and store it in a variable.
givenword = 'BTechGeeks'
# Take an empty string and store it in a variable say sampstrng.
sampstrng = ""
# Traverse the given word(string) using For loop.
for wordchar in givenword:
    # Concatenate the iterator character to the sampstrng using string Concatenation.
    sampstrng = sampstrng+wordchar
    # Print the sampstrng.
    print(sampstrng)

Output:

B
BT
BTe
BTec
BTech
BTechG
BTechGe
BTechGee
BTechGeek
BTechGeeks

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the word as static input and store it in a
    // variable.
    string givenword = "BTechGeeks";
    // Take an empty string and store it in a variable say
    // sampstrng.
    string sampstrng = "";
    // Traverse the given word(string) using For loop.
    for (int wordchar = 0; wordchar < givenword.length();
         wordchar++) {
        // Concatenate the iterator character to the
        // sampstrng using string Concatenation.

        sampstrng = sampstrng + givenword[wordchar];
        // Print the sampstrng.
        cout << sampstrng << endl;
    }
    return 0;
}

Output:

B
BT
BTe
BTec
BTech
BTechG
BTechGe
BTechGee
BTechGeek
BTechGeeks

3) C Implementation

Below is the implementation:

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

    // Give the word as static input and store it in a
    // variable.
    char givenword[100] = "BTechGeeks";
    // Take an empty string and store it in a variable say
    // sampstrng.
    char sampstrng[100] = "";
    // Traverse the given word(string) using For loop.
    for (int wordchar = 0; wordchar < strlen(givenword);
         wordchar++) {
        // Concatenate the iterator character to the
        // sampstrng using string Concatenation.

        sampstrng[wordchar] = givenword[wordchar];
        // Print the sampstrng.
        printf("%s", sampstrng);
        printf("\n");
    }
    return 0;
}

Output:

B
BT
BTe
BTec
BTech
BTechG
BTechGe
BTechGee
BTechGeek
BTechGeeks

Method #2: Using For loop (User Input)

Approach:

  • Give the word as user input and store it in a variable.
  • Take an empty string and store it in a variable say sampstrng.
  • Traverse the given word(string) using For loop.
  • Concatenate the iterator character to the sampstrng using string Concatenation.
  • Print the sampstrng.
  • The Exit of the Program.

1) Python Implementation

Give the word as user input using input() and store it in a variable.

Below is the implementation:

# Give the word as user input and store it in a variable.
givenword = input('Enter some random word = ')
# Take an empty string and store it in a variable say sampstrng.
sampstrng = ""
# Traverse the given word(string) using For loop.
for wordchar in givenword:
    # Concatenate the iterator character to the sampstrng using string Concatenation.
    sampstrng = sampstrng+wordchar
    # Print the sampstrng.
    print(sampstrng)

Output:

Enter some random word = Aplustopper
A
Ap
Apl
Aplu
Aplus
Aplust
Aplusto
Aplustop
Aplustopp
Aplustoppe
Aplustopper

2) C++ Implementation

Give the word as user input using cin and store it in a variable.

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the word as user input using cin and store it in
    // a variable.
    string givenword;
    cin >> givenword;
    // Take an empty string and store it in a variable say
    // sampstrng.
    string sampstrng = "";
    // Traverse the given word(string) using For loop.
    for (int wordchar = 0; wordchar < givenword.length();
         wordchar++) {
        // Concatenate the iterator character to the
        // sampstrng using string Concatenation.

        sampstrng = sampstrng + givenword[wordchar];
        // Print the sampstrng.
        cout << sampstrng << endl;
    }
    return 0;
}

Output:

Aplustopper
A
Ap
Apl
Aplu
Aplus
Aplust
Aplusto
Aplustop
Aplustopp
Aplustoppe
Aplustopper

3) C Implementation

Give the word as user input using scanf and store it in a variable.

Below is the implementation

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

    // Give the word as user input using scanfand store it
    // in a variable.
    char givenword[100];
    scanf("%s", givenword);
    // Take an empty string and store it in a variable say
    // sampstrng.
    char sampstrng[100] = "";
    // Traverse the given word(string) using For loop.
    for (int wordchar = 0; wordchar < strlen(givenword);
         wordchar++) {
        // Concatenate the iterator character to the
        // sampstrng using string Concatenation.

        sampstrng[wordchar] = givenword[wordchar];
        // Print the sampstrng.
        printf("%s", sampstrng);
        printf("\n");
    }
    return 0;
}

Output:

Aplustopper
A
Ap
Apl
Aplu
Aplus
Aplust
Aplusto
Aplustop
Aplustopp
Aplustoppe
Aplustopper

Related Programs:

Python Program to Print Even Number Pattern

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

Given the number of rows, the task is to print Even Number Pattern in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows = 10

Output:

20 
20 18 
20 18 16 
20 18 16 14 
20 18 16 14 12 
20 18 16 14 12 10 
20 18 16 14 12 10 8 
20 18 16 14 12 10 8 6 
20 18 16 14 12 10 8 6 4 
20 18 16 14 12 10 8 6 4 2

Example2:

Input:

Given number of rows = 6

Output:

12 
12 10 
12 10 8 
12 10 8 6 
12 10 8 6 4 
12 10 8 6 4 2

Program to Print Even Number Pattern in C, C++, and Python

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

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Take a variable and initialize it with 2*double the number of rows say lastnumb.
  • Take another variable and initialize it with the lastnumb say evennumb.
  • Loop from 1 to the number of rows using For loop.
  • Initialize the evennumb with the lastnumb.
  • Loop from 0 to the iterator value of the parent For loop using another for loop(Nested For loop).
  • Print the evennumb.
  • Reduce the evennumb by 2.
  • Print the Newline character after the end of the inner loop.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numberOfRows = 10
# Take a variable and initialize it with 2*double the number of rows say lastnumb.
lastnumb = 2*numberOfRows
# Take another variable and initialize it with the lastnumb say evennumb.
evennumb = lastnumb
# Loop from 1 to the number of rows using For loop.
for m in range(1, numberOfRows+1):
    # Initialize the evennumb with the lastnumb.
    evennumb = lastnumb
    # Loop from 0 to the iterator value of the parent For loop using
    # another for loop(Nested For loop).
    for n in range(0, m):
        # Print the evennumb.
        print(evennumb, end=' ')
        # Reduce the evennumb by 2.
        evennumb = evennumb-2

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

Output:

20 
20 18 
20 18 16 
20 18 16 14 
20 18 16 14 12 
20 18 16 14 12 10 
20 18 16 14 12 10 8 
20 18 16 14 12 10 8 6 
20 18 16 14 12 10 8 6 4 
20 18 16 14 12 10 8 6 4 2

2) C++ Implementation

Below is the implementation:

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

    // Give the number of rows as static input and store it
    // in a variable.
    int numberOfRows = 8;
    // Take a variable and initialize it with 2*double the
    // number of rows say lastnumb.
    int lastnumb = 2 * numberOfRows;

    // Take another variable and initialize it with the
    // lastnumb say evennumb.
    int evennumb = lastnumb;
    //  Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= numberOfRows; m++) {
        // Initialize the evennumb with the lastnumb.
        evennumb = lastnumb;
        // Loop from 0 to the iterator value of the parent
        // For loop using
        // another for loop(Nested For loop).
        for (int n = 0; n < m; n++) {
            // Print the evennumb.
            cout << evennumb << " ";

            // Reduce the evennumb by 2.
            evennumb = evennumb - 2;
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }

    return 0;
}

Output:

16 
16 14 
16 14 12 
16 14 12 10 
16 14 12 10 8 
16 14 12 10 8 6 
16 14 12 10 8 6 4 
16 14 12 10 8 6 4 2

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows as static input and store it
    // in a variable.
    int numberOfRows = 8;
    // Take a variable and initialize it with 2*double the
    // number of rows say lastnumb.
    int lastnumb = 2 * numberOfRows;

    // Take another variable and initialize it with the
    // lastnumb say evennumb.
    int evennumb = lastnumb;
    //  Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= numberOfRows; m++) {
        // Initialize the evennumb with the lastnumb.
        evennumb = lastnumb;
        // Loop from 0 to the iterator value of the parent
        // For loop using
        // another for loop(Nested For loop).
        for (int n = 0; n < m; n++) {
            // Print the evennumb.
            printf("%d ", evennumb);

            // Reduce the evennumb by 2.
            evennumb = evennumb - 2;
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

16 
16 14 
16 14 12 
16 14 12 10 
16 14 12 10 8 
16 14 12 10 8 6 
16 14 12 10 8 6 4 
16 14 12 10 8 6 4 2

Method #2: Using For Loop (User Input)

Approach:

  • Give the number of rows as user input and store it in a variable.
  • Take a variable and initialize it with 2*double the number of rows say lastnumb.
  • Take another variable and initialize it with the lastnumb say evennumb.
  • Loop from 1 to the number of rows using For loop.
  • Initialize the evennumb with the lastnumb.
  • Loop from 0 to the iterator value of the parent For loop using another for loop(Nested For loop).
  • Print the evennumb.
  • Reduce the evennumb by 2.
  • Print the Newline character after the end of the inner loop.
  • The Exit of the Program.

1) Python Implementation

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

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numberOfRows = int(input('Enter some random number of rows = '))
# Take a variable and initialize it with 2*double the number of rows say lastnumb.
lastnumb = 2*numberOfRows
# Take another variable and initialize it with the lastnumb say evennumb.
evennumb = lastnumb
# Loop from 1 to the number of rows using For loop.
for m in range(1, numberOfRows+1):
    # Initialize the evennumb with the lastnumb.
    evennumb = lastnumb
    # Loop from 0 to the iterator value of the parent For loop using
    # another for loop(Nested For loop).
    for n in range(0, m):
        # Print the evennumb.
        print(evennumb, end=' ')
        # Reduce the evennumb by 2.
        evennumb = evennumb-2

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

Output:

Enter some random number of rows = 10
20 
20 18 
20 18 16 
20 18 16 14 
20 18 16 14 12 
20 18 16 14 12 10 
20 18 16 14 12 10 8 
20 18 16 14 12 10 8 6 
20 18 16 14 12 10 8 6 4 
20 18 16 14 12 10 8 6 4 2

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
    // int(input()) and store it in a variable.
    int numberOfRows;
    cin >> numberOfRows;
    // Take a variable and initialize it with 2*double the
    // number of rows say lastnumb.
    int lastnumb = 2 * numberOfRows;

    // Take another variable and initialize it with the
    // lastnumb say evennumb.
    int evennumb = lastnumb;
    //  Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= numberOfRows; m++) {
        // Initialize the evennumb with the lastnumb.
        evennumb = lastnumb;
        // Loop from 0 to the iterator value of the parent
        // For loop using
        // another for loop(Nested For loop).
        for (int n = 0; n < m; n++) {
            // Print the evennumb.
            cout << evennumb << " ";

            // Reduce the evennumb by 2.
            evennumb = evennumb - 2;
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }

    return 0;
}

Output:

6
12 
12 10 
12 10 8 
12 10 8 6 
12 10 8 6 4 
12 10 8 6 4 2 

3) C Implementation

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

Below is the implementation:

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

    // Give the number of rows as user input using scanf and
    // store it in a variable.
    int numberOfRows;
    scanf("%d", &numberOfRows);
    // Take a variable and initialize it with 2*double the
    // number of rows say lastnumb.
    int lastnumb = 2 * numberOfRows;

    // Take another variable and initialize it with the
    // lastnumb say evennumb.
    int evennumb = lastnumb;
    //  Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= numberOfRows; m++) {
        // Initialize the evennumb with the lastnumb.
        evennumb = lastnumb;
        // Loop from 0 to the iterator value of the parent
        // For loop using
        // another for loop(Nested For loop).
        for (int n = 0; n < m; n++) {
            // Print the evennumb.
            printf("%d ", evennumb);

            // Reduce the evennumb by 2.
            evennumb = evennumb - 2;
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

6
12 
12 10 
12 10 8 
12 10 8 6 
12 10 8 6 4 
12 10 8 6 4 2

Related Programs:

Python Program to Print Reverse Number Pattern

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Given the number of rows, the task is to print Reverse Number Pattern in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows = 7

Output:

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

Example2:

Input:

Given number of rows = 10

Output:

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

Program to Print Reverse Number Pattern in C, C++, and Python

Method #1: Using For Loop (Static Input)

Approach:

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

1) Python Implementation

Below is the implementation:

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

Output:

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

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 numbrrows=10;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m <= numbrrows; m++) {
        // Loop from the number of rows - iterator value of
        // the first parent For loop to 0 in decreasing
        // order using another For loop(Nested For loop).
        for (int n = numbrrows - m; n > 0; n--) {
            // Print the iterator value of the inner for
            // loop.
            cout << n << " ";
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }
    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows as static input and store it
    // in a variable.
    int numbrrows = 10;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m <= numbrrows; m++) {
        // Loop from the number of rows - iterator value of
        // the first parent For loop to 0 in decreasing
        // order using another For loop(Nested For loop).
        for (int n = numbrrows - m; n > 0; n--) {
            // Print the iterator value of the inner for
            // loop.
            printf("%d ", n);
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For Loop (User Input)

1) Python Implementation

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

Below is the implementation:

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

Output:

Enter some random number of rows = 11
11 10 9 8 7 6 5 4 3 2 1 
10 9 8 7 6 5 4 3 2 1 
9 8 7 6 5 4 3 2 1 
8 7 6 5 4 3 2 1 
7 6 5 4 3 2 1 
6 5 4 3 2 1 
5 4 3 2 1 
4 3 2 1 
3 2 1 
2 1 
1

2) C++ Implementation

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

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows as user input using
    // int(input()) and store it in a variable.
    int numbrrows;
    cin >> numbrrows;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m <= numbrrows; m++) {
        // Loop from the number of rows - iterator value of
        // the first parent For loop to 0 in decreasing
        // order using another For loop(Nested For loop).
        for (int n = numbrrows - m; n > 0; n--) {
            // Print the iterator value of the inner for
            // loop.
            cout << n << " ";
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

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

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows as user input using scanf and
    // store it in a variable.
    int numbrrows;
    scanf("%d", &numbrrows);
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m <= numbrrows; m++) {
        // Loop from the number of rows - iterator value of
        // the first parent For loop to 0 in decreasing
        // order using another For loop(Nested For loop).
        for (int n = numbrrows - m; n > 0; n--) {
            // Print the iterator value of the inner for
            // loop.
            printf("%d ", n);
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Related Programs:

Python Program to Print a Unique Pyramid Pattern of Digits

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

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

Examples:

Example1:

Input:

Given number of rows = 8

Output:

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

Example2:

Input:

Given number of rows = 7

Output:

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

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

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

Method #1: Using For Loop (Static Input)

Approach:

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

1) Python Implementation

Below is the implementation:

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

Output:

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

2) C++ Implementation

Below is the implementation:

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

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

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

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

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

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

Output:

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

Method #2: Using For Loop (User Input)

Approach:

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

1) Python Implementation

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

Below is the implementation:

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

Output:

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

2) C++ Implementation

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

Below is the implementation:

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

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

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

    return 0;
}

Output:

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

3) C Implementation

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

Below is the implementation:

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

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

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

Output:

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

Related Programs:

Python Program to Print Number Reduction Pattern

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Given the number of rows, the task is to print Number Reduction Pattern in C, C++, and Python

Examples:

Example1:

Input:

Given number of rows = 4

Output:

1 2 3 4 
2 3 4 
3 4 
4

Example2:

Input:

Given number of rows = 14

Output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 
2 3 4 5 6 7 8 9 10 11 12 13 14 
3 4 5 6 7 8 9 10 11 12 13 14 
4 5 6 7 8 9 10 11 12 13 14 
5 6 7 8 9 10 11 12 13 14 
6 7 8 9 10 11 12 13 14 
7 8 9 10 11 12 13 14 
8 9 10 11 12 13 14 
9 10 11 12 13 14 
10 11 12 13 14 
11 12 13 14 
12 13 14 
13 14 
14 

Program to Print Number Reduction Pattern in C, C++, and Python

Below are the ways to print Number Reduction Pattern in C, C++, and Python

Method #1: Using For Loop (Static Input)

Approach:

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

1) Python Implementation

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbrrows = 9
# Loop from 0 to the number of rows using For loop.
for m in range(0, numbrrows+1):
    # Loop from iterator value of the parent For loop +1 to
    # the number of rows using another for loop(Nested For loop).
    for n in range(m + 1, numbrrows+1):
        # Print the iterator value of the inner for loop.
        print(n, end=' ')
    # Print the Newline character after the end of the inner loop.
    print()

Output:

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

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 numbrrows = 10;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m <= numbrrows; m++) {
        /* Loop from iterator value of the parent For loop+1
           to the number of rows using another for
           loop(Nested For loop).*/
        for (int n = m + 1; n <= numbrrows; n++) {
            // Print the iterator value of the inner for
            // loop.
            cout << n << " ";
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }
    return 0;
}

Output:

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

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 numbrrows = 14;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m <= numbrrows; m++) {
        /* Loop from iterator value of the parent For loop+1
           to the number of rows using another for
           loop(Nested For loop).*/
        for (int n = m + 1; n <= numbrrows; n++) {
            // Print the iterator value of the inner for
            // loop.
            printf("%d ", n);
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 
2 3 4 5 6 7 8 9 10 11 12 13 14 
3 4 5 6 7 8 9 10 11 12 13 14 
4 5 6 7 8 9 10 11 12 13 14 
5 6 7 8 9 10 11 12 13 14 
6 7 8 9 10 11 12 13 14 
7 8 9 10 11 12 13 14 
8 9 10 11 12 13 14 
9 10 11 12 13 14 
10 11 12 13 14 
11 12 13 14 
12 13 14 
13 14 
14 

Method #2: Using For Loop (User Input)

Approach:

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

1) Python Implementation

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

Below is the implementation:

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

Output:

Enter some random number of rows = 6
1 2 3 4 5 6 
2 3 4 5 6 
3 4 5 6 
4 5 6 
5 6 
6

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
    // int(input()) and store it in a variable.
    int numbrrows;
    cin >> numbrrows;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m <= numbrrows; m++) {
        /* Loop from iterator value of the parent For loop+1
           to the number of rows using another for
           loop(Nested For loop).*/
        for (int n = m + 1; n <= numbrrows; n++) {
            // Print the iterator value of the inner for
            // loop.
            cout << n << " ";
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }

    return 0;
}

Output:

4
1 2 3 4 
2 3 4 
3 4 
4

3) C Implementation

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

Below is the implementation:

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

    // Give the number of rows as user input using scanf and
    // store it in a variable.
    int numbrrows;
    scanf("%d", &numbrrows);
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m <= numbrrows; m++) {
        /* Loop from iterator value of the parent For loop+1
           to the number of rows using another for
           loop(Nested For loop).*/
        for (int n = m + 1; n <= numbrrows; n++) {
            // Print the iterator value of the inner for
            // loop.
            printf("%d ", n);
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

4
1 2 3 4 
2 3 4 
3 4 
4

Related Programs:

Python Program to Print Reverse Pyramid of Numbers

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

Given the number of rows of the pyramid, the task is to print a Reverse Pyramid of Numbers in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows = 10

Output:

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

Example2:

Input:

Given number of rows = 6

Output:

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

Program to Print Reverse Pyramid of Numbers in C,C++, and Python

Method #1: Using For Loop (Static Input)

Approach:

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

1) Python Implementation

Below is the implementation:

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

Output:

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

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 numbrrows = 10;
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m < numbrrows; m++) {
        // Loop from the parent loop iterator value to 0 in
        // decreasing order using another For loop(Nested
        // For Loop).
        for (int n = m; n > 0; n--) {
            // Print the iterator value of the inner for
            // loop.
            cout << n << " ";
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows as static input and store it
    // in a variable.
    int numbrrows = 10;
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m < numbrrows; m++) {
        // Loop from the parent loop iterator value to 0 in
        // decreasing order using another For loop(Nested
        // For Loop).
        for (int n = m; n > 0; n--) {
            // Print the iterator value of the inner for
            // loop.
            printf("%d ", n);
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For Loop (User Input)

Approach:

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

1) Python Implementation

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

Below is the implementation:

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

Output:

Enter some random number of rows = 6
1 
2 1 
3 2 1 
4 3 2 1 
5 4 3 2 1

2) C++ Implementation

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

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows as user input using
    // cin and store it in a variable.
    int numbrrows;
    cin >> numbrrows;
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m < numbrrows; m++) {
        // Loop from the parent loop iterator value to 0 in
        // decreasing order using another For loop(Nested
        // For Loop).
        for (int n = m; n > 0; n--) {
            // Print the iterator value of the inner for
            // loop.
            cout << n << " ";
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

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

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows as user input using scanf and
    // store it in a variable.
    int numbrrows;
    scanf("%d", &numbrrows);
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m < numbrrows; m++) {
        // Loop from the parent loop iterator value to 0 in
        // decreasing order using another For loop(Nested
        // For Loop).
        for (int n = m; n > 0; n--) {
            // Print the iterator value of the inner for
            // loop.
            printf("%d ", n);
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Related Programs:

Python Program to Print Hollow Mirrored Rhombus 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 of the Rhombus, the task is to print the Hollow Mirrored Rhombus Star pattern in C, C++, and Python.

Examples:

Example1:

Input:

given number of rows of the rhombus =8

Output:

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

Example2:

Input:

Given number of rows of the rhombus =6
Given character to print ='~'

Output:

6~
~ ~ ~ ~ ~ ~ 
  ~               ~ 
    ~               ~ 
      ~               ~ 
        ~               ~ 
          ~ ~ ~ ~ ~ ~

Program to Print the Hollow Mirrored Rhombus Star Pattern in C, C++, and Python

Below are the ways to print the Hollow mirrored Rhonbus star Patterns in C, C++, and Python.

Method #1: Using For loop (Star Character)

Approach:

  • Give the number of rows of the Hollow rhombus as static input and store it in a variable.
  • Loop till the number of rows of the rhombus using For loop.
  • Iterate till the first iterator using another For loop(Nested For loop).
  • Print the space character in the inner for loop.
  • Loop till the number of rows of the rhombus using For loop(Second Nested For loop).
  • If the parent For loop iterator value is equal to 0 or the number of rows-1, then print star with a space character.
  • If the current iterator’s value is equal to 0 or the number of rows-1, the print star with a space character.
  • Check the above two statements true or false using the If condition statement.
  • If it is true then print star with a space character.
  • Else print space.
  • Print the newline character after the end of two nested for loops.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the rhombus as static input and store it in a variable.
rhombusrows = 8
# Loop till the number of rows of the rhombus using For loop.
for m in range(rhombusrows):
  # Iterate till the first iterator using another For loop(Nested For loop).
    for n in range(m):
      # Print the space character in the inner for loop.
        print(' ', end='')
    # Loop till the number of rows of the rhombus using For loop(Second Nested For loop).
    for o in range(rhombusrows):
      # If the parent For loop iterator value is equal to 0 or the number of rows-1, then print star with a space character.
      # If the current iterator's value is equal to 0 or the number of rows-1, the print star with a space character.
      # Check the above two statements true or false using the If condition statement.
      # If it is true then print star with a space character.
      # Else print space.
        if(m == 0 or m == rhombusrows - 1 or o == 0 or o == rhombusrows - 1):
            print('*', end=' ')
        else:
            print(' ', end=' ')
    # Print the newline character after the end of two nested for loops.
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows of the rhombus as static
    // input and store it in a variable.
    int rhombusrows = 8;
    // Loop till the number of rows of the rhombus using For
    // loop.
    for (int m = 0; m < rhombusrows; m++) {
        // Iterate till the first iterator using another For
        // loop(Nested For loop).

        for (int n = 0; n < m; n++) {
            // Print the space character in the inner for
            // loop.

            cout << "  ";
        }
        // Loop till the number of rows of the rhombus using
        // For loop(Second Nested For loop).
        for (int r = 0; r < rhombusrows; r++) {
            /*If the parent For loop iterator value is equal
              to 0 or the number of rows-1, then print star
              with a space character. If the current
              iterator's value is equal to 0 or the number
              of rows-1, the print star with a space
              character. Check the above two statements true
              or false using the If condition statement. If
              it is true then print star with a space
              character. Else print space.*/
            if (m == 0 || m == rhombusrows - 1 || r == 0 || r == rhombusrows - 1)
                cout << "* ";
            else
                cout << "  ";
        }

        // Print the newline character after the end of two
        // nested for loops.
        cout << endl;
    }
    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows of the rhombus as static
    // input and store it in a variable.
    int rhombusrows = 8;
    // Loop till the number of rows of the rhombus using For
    // loop.
    for (int m = 0; m < rhombusrows; m++) {
        // Iterate till the first iterator using another For
        // loop(Nested For loop).

        for (int n = 0; n < m; n++) {
            // Print the space character in the inner for
            // loop.

            printf("  ");
        }
        // Loop till the number of rows of the rhombus using
        // For loop(Second Nested For loop).
        for (int r = 0; r < rhombusrows; r++) {
            /*If the parent For loop iterator value is equal
              to 0 or the number of rows-1, then print star
              with a space character. If the current
              iterator's value is equal to 0 or the number
              of rows-1, the print star with a space
              character. Check the above two statements true
              or false using the If condition statement. If
              it is true then print star with a space
              character. Else print space.*/
            if (m == 0 || m == rhombusrows - 1 || r == 0
                || r == rhombusrows - 1)
                printf("* ");
            else
                printf("  ");
        }

        // Print the newline character after the end of two
        // nested for loops.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For loop (User Character)

Approach:

  • Give the number of rows of the rhombus 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 the number of rows of the rhombus using For loop.
  • Iterate till the first iterator using another For loop(Nested For loop).
  • Print the space character in the inner for loop.
  • Loop till the number of rows of the rhombus using For loop(Second Nested For loop).
  • If the parent For loop iterator value is equal to 0 or the number of rows-1, then print star with a space character.
  • If the current iterator’s value is equal to 0 or the number of rows-1, the print star with a space character.
  • Check the above two statements true or false using the If condition statement.
  • If it is true then print the given character with a space character.
  • Else print space.
  • Print the newline character after the end of two nested for loops.
  • The Exit of the Program.

1) Python Implementation

  • Give the number of rows of the rhombus 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 rhombus as user input using int(input()) and store it in a variable.
rhombusrows = int(input(
    'Enter some random number of rows of rhombus = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
# Loop till the number of rows of the rhombus using For loop.
for m in range(rhombusrows):
  # Iterate till the first iterator using another For loop(Nested For loop).
    for n in range(m):
      # Print the space character in the inner for loop.
        print(' ', end='')
    # Loop till the number of rows of the rhombus using For loop(Second Nested For loop).
    for o in range(rhombusrows):
      # If the parent For loop iterator value is equal to 0 or the number of rows-1, then print star with a space character.
      # If the current iterator's value is equal to 0 or the number of rows-1, the print star with a space character.
      # Check the above two statements true or false using the If condition statement.
      # If it is true then print given character with a space character.
      # Else print space.
        if(m == 0 or m == rhombusrows - 1 or o == 0 or o == rhombusrows - 1):
            print(givencharacter, end=' ')
        else:
            print(' ', end=' ')
    # Print the newline character after the end of two nested for loops.
    print()

Output:

Enter some random number of rows of rhombus = 6
Enter some random character = ~
~ ~ ~ ~ ~ ~ 
  ~               ~ 
    ~               ~ 
      ~               ~ 
        ~               ~ 
          ~ ~ ~ ~ ~ ~

2) C++ Implementation

  • Give the number of rows of the rhombus 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 of the Right
    // Rhombbus as user input using cin and store it in a
    // variable.
    int rhombusrows;
    char givencharacter;
    cout << "Enter some random number of rows of the "
            "Rhombus = "
         << endl;
    cin >> rhombusrows;
    // 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 till the number of rows of the rhombus using For
    // loop.
    for (int m = 0; m < rhombusrows; m++) {
        // Iterate till the first iterator using another For
        // loop(Nested For loop).

        for (int n = 0; n < m; n++) {
            // Print the space character in the inner for
            // loop.

            cout << "  ";
        }
        // Loop till the number of rows of the rhombus using
        // For loop(Second Nested For loop).
        for (int r = 0; r < rhombusrows; r++) {
            /*If the parent For loop iterator value is equal
              to 0 or the number of rows-1, then print star
              with a space character. If the current
              iterator's value is equal to 0 or the number
              of rows-1, the print star with a space
              character. Check the above two statements true
              or false using the If condition statement. If
              it is true then print given character with a
              space character. Else print space.*/
            if (m == 0 || m == rhombusrows - 1 || r == 0
                || r == rhombusrows - 1)
                cout << givencharacter << " ";
            else
                cout << "  ";
        }

        // Print the newline character after the end of two
        // nested for loops.
        cout << endl;
    }
    return 0;
}

Output:

Enter some random number of rows of rhombus = 6
Enter some random character = ~
~ ~ ~ ~ ~ ~ 
  ~               ~ 
    ~               ~ 
      ~               ~ 
        ~               ~ 
          ~ ~ ~ ~ ~ ~

3) C Implementation

  • Give the number of rows of the Rhombus 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 of the  Rhombus
    //  as user input using scanf and store it in a
    // variable.
    int rhombusrows;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.
    scanf("%d", &rhombusrows);
    scanf("%c", &givencharacter);
    printf("\n");
    // Loop till the number of rows of the rhombus using For
    // loop.
    for (int m = 0; m < rhombusrows; m++) {
        // Iterate till the first iterator using another For
        // loop(Nested For loop).

        for (int n = 0; n < m; n++) {
            // Print the space character in the inner for
            // loop.

            printf("  ");
        }
        // Loop till the number of rows of the rhombus using
        // For loop(Second Nested For loop).
        for (int r = 0; r < rhombusrows; r++) {
            /*If the parent For loop iterator value is equal
              to 0 or the number of rows-1, then print star
              with a space character. If the current
              iterator's value is equal to 0 or the number
              of rows-1, the print star with a space
              character. Check the above two statements true
              or false using the If condition statement. If
              it is true then print given character with a
              space character. Else print space.*/
            if (m == 0 || m == rhombusrows - 1 || r == 0
                || r == rhombusrows - 1)
                printf("%c ", givencharacter);
            else
                printf("  ");
        }

        // Print the newline character after the end of two
        // nested for loops.
        printf("\n");
    }
    return 0;
}

Output:

6~
~ ~ ~ ~ ~ ~ 
  ~               ~ 
    ~               ~ 
      ~               ~ 
        ~               ~ 
          ~ ~ ~ ~ ~ ~

Related Programs: