Python Program to Print Rectangle Star Pattern

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

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 length, breadth of the rectangle the task is to print the rectangle star pattern in C, C++, and Python.

Examples:

Example1:

Input:

given length of rectangle =8
given breadth of rectangle =29

Output:

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

Example2:

Input:

given length of rectangle =19
given breadth of rectangle =5

Output:

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

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

Below are the ways to print the rectangle star pattern in C, C++, and Python.

Method #1: Using For loop (Static Input)

Approach:

  • Give the length and breadth as static input and store them in two variables.
  • Loop till the length of the rectangle using For loop.
  • Loop till the breadth of the rectangle using another nested For loop.
  • Print the * character inside the nested For loop.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the length and breadth as static input and store them in two variables.
lengthnum = 8
breadthnum = 11
# Loop till the length of the rectangle using For loop.
for m in range(lengthnum):
    # Loop till the breadth of the rectangle using another nested For loop.
    for n in range(breadthnum):
        # Print the * character inside the nested For loop.
        print('* ', end=' ')
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the length and breadth as static input and store
    // them in two variables.
    int lengthnum = 11;
    int breadthnum = 23;
    // Loop till the length of the rectangle using For loop.
    for (int m = 0; m < lengthnum; m++) {
        // Loop till the breadth of the rectangle using
        // another nested For loop.
        for (int n = 0; n < breadthnum;n++) { // Print the * character inside the
                    // nested For loop.
            cout << "* ";
        }
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{
    // Give the length and breadth as static input and store
    // them in two variables.
    int lengthnum = 9;
    int breadthnum = 29;
    // Loop till the length of the rectangle using For loop.
    for (int m = 0; m < lengthnum; m++) {
        // Loop till the breadth of the rectangle using
        // another nested For loop.
        for (int n = 0; n < breadthnum; n++) {
            // Print the * character inside the
            // nested For loop.
            printf("* ");
        }
        printf("\n");
    }
}

Output:

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

Method #2: Using For loop (User Input)

Approach:

  • Give the length and breadth as user input and store them in two variables.
  • Loop till the length of the rectangle using For loop.
  • Loop till the breadth of the rectangle using another nested For loop.
  • Print the * character inside the nested For loop.
  • The Exit of the Program.

1) Python Implementation

Give the length and breadth of the rectangle as user input using the int(input()) function and store them in two separate variables.

Below is the implementation:

# Give the length and breadth of the rectangle as user input using the int(input()) function
# and store them in two separate variables.
lengthnum = int(input('Enter some random length value of the rectangle = '))

breadthnum=int(input('Enter some random breadth value of the rectangle = '))
# Loop till the length of the rectangle using For loop.
for m in range(lengthnum):
    # Loop till the breadth of the rectangle using another nested For loop.
    for n in range(breadthnum):
        # Print the * character inside the nested For loop.
        print('* ', end=' ')
    print()

Output:

Enter some random length value of the rectangle = 9
Enter some random breadth value of the rectangle = 4
* * * * 
* * * * 
* * * * 
* * * * 
* * * * 
* * * * 
* * * * 
* * * * 
* * * * 

2) C++ Implementation

Give the length and breadth of the rectangle as user input using the cin and store them in two separate variables.

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the length and breadth of the rectangle as user
    // input using the cin
    // and store them in two separate variables.
    int lengthnum, breadthnum;
    cout << "Enter some random length and breadth of "
            "rectangle separated by spaces"
         << endl;
    cin >> lengthnum >> breadthnum;
    // Loop till the length of the rectangle using For loop.
    for (int m = 0; m < lengthnum; m++) {
        // Loop till the breadth of the rectangle using
        // another nested For loop.
        for (int n = 0; n < breadthnum;
             n++) { // Print the * character inside the
            // nested For loop.
            cout << "* ";
        }
        cout << endl;
    }

    return 0;
}

Output:

Enter some random length and breadth of rectangle separated by spaces
4 16
* * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * 

3) C Implementation

Give the length and breadth of the rectangle as user input using the scanf and store them in two separate variables.

Below is the implementation:

#include <stdio.h>

int main()
{
    //    Give the length and breadth of the rectangle as
    // user input using the scanf
    // and store them in two separate variables.
    int lengthnum,breadthnum ;
    printf("Enter some random length and breadth of rectangle separated by spaces \n");
    scanf("%d%d", &lengthnum, &breadthnum);
    // Loop till the length of the rectangle using For loop.
    for (int m = 0; m < lengthnum; m++) {
        // Loop till the breadth of the rectangle using
        // another nested For loop.
        for (int n = 0; n < breadthnum; n++) {
            // Print the * character inside the
            // nested For loop.
            printf("* ");
        }
        printf("\n");
    }
}

Output:

Enter some random length and breadth of rectangle separated by spaces
8 15
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * *

Related Programs:

Python Program to Print Hollow Inverted Right Triangle

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 Triangle, the task is to Print a Hollow Inverted Right Triangle Star Pattern in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows of the Hollow Inverted Right Triangle Pattern =7

Output:

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

 

Example2:

Input:

Given number of rows of the Hollow Inverted Right Triangle Pattern =9
Given character to print ='^'

Output:

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

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

Below are the ways to print a Hollow Inverted 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 Inverted Right Triangle as static input and store it in a variable.
  • Loop from the given number of rows to 0 using For loop and take iterator value as m.
  • Loop from the iterator value of the first loop to 0 using another nested For loop.
  • If m equals zero, rows, n, or n equals one value, the if statement prints stars.
  • Else print space character.
  • Print the newline character after inner for loop.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the Hollow Inverted Right Triangle as static input and store it in a variable.
triNumRows = 7
# Loop from the given number of rows to 0 using For loop and take iterator value as m.
for m in range(triNumRows, 0, -1):
    # Loop from the  iterator value of the first loop to 0 using another nested For loop.
    for n in range(m, 0, -1):
        # If m equals zero, rows, n, or n equals one value, the if statement prints stars.
        if m == 1 or m == triNumRows or n == 1 or n == m:
            print('*', end='')
        else:
            print(' ', end='')
    # Print the newline character after 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 Inverted Right
    // Triangle as static input and store it in a variable.
    int triNumRows = 7;
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m > 0; m--) {

        // Loop from the iterator value of the first loop to
        // 0 using another nested For loop.
        for (int n = m; n > 0; n--) {
            // If m equals zero, rows, n, or n equals one
            // value, the if statement prints stars.
            if (m == 1 || m == triNumRows || n == 1
                || n == m)
                cout << "* ";
            else
                cout << "  ";
        }
        // Print the newline character after 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 Inverted Right
    // Triangle as static input and store it in a variable.
    int triNumRows = 7;
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m > 0; m--) {

        // Loop from the iterator value of the first loop to
        // 0 using another nested For loop.
        for (int n = m; n > 0; n--) {
            // If m equals zero, rows, n, or n equals one
            // value, the if statement prints stars.
            if (m == 1 || m == triNumRows || n == 1
                || n == m)
                printf("* ");
            else
                printf("  ");
        }
        // Print the newline character after inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For Loop(User Character)

Approach:

  • Give the number of rows of the Hollow Inverted Right Triangle as static input and store it in a variable.
  • Give the character to print as user input and store it in a variable.
  • Loop from the given number of rows to 0 using For loop and take iterator value as m.
  • Loop from the iterator value of the first loop to 0 using another nested For loop.
  • If m equals zero, rows, n, or n equals one value, the if statement prints stars.
  • Else print space character.
  • Print the newline character after inner for loop.
  • The Exit of the Program.

1) Python Implementation

  • Give the number of rows of the Hollow Inverted 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 Inverted 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 Inverted Right Triangle Pattern = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
# Loop from the given number of rows to 0 using For loop and take iterator value as m.
for m in range(triNumRows, 0, -1):
    # Loop from the  iterator value of the first loop to 0 using another nested For loop.
    for n in range(m, 0, -1):
        # If m equals zero, rows, n, or n equals one value, the if statement prints stars.
        if m == 1 or m == triNumRows or n == 1 or n == m:
            print(givencharacter, end='')
        else:
            print(' ', end='')
    # Print the newline character after inner for loop.
    print()

Output:

Enter some random number of rows of the Inverted Right Triangle Pattern = 9
Enter some random character = ^
^^^^^^^^^
^               ^
^            ^
^          ^
^        ^
^      ^
^    ^
^ ^
^

2) C++ Implementation

  • Give the number of rows of the Hollow Inverted 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 Inverted 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;
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m > 0; m--) {

        // Loop from the iterator value of the first loop to
        // 0 using another nested For loop.
        for (int n = m; n > 0; n--) {
            // If m equals zero, rows, n, or n equals one
            // value, the if statement prints stars.
            if (m == 1 || m == triNumRows || n == 1
                || n == m)
                cout << givencharacter << " ";
            else
                cout << "  ";
        }
        // Print the newline character after inner for loop.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows of the Inverted Right Triangle Pattern = 9
Enter some random character = ^
^^^^^^^^^
^               ^
^            ^
^          ^
^        ^
^      ^
^    ^
^ ^
^

3) C Implementation

  • Give the number of rows of the Hollow Inverted 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 Inverted 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");
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m > 0; m--) {

        // Loop from the iterator value of the first loop to
        // 0 using another nested For loop.
        for (int n = m; n > 0; n--) {
            // If m equals zero, rows, n, or n equals one
            // value, the if statement prints stars.
            if (m == 1 || m == triNumRows || n == 1
                || n == m)
                printf("%c ", givencharacter);
            else
                printf("  ");
        }
        // Print the newline character after inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

9^
^^^^^^^^^
^               ^
^            ^
^          ^
^        ^
^      ^
^    ^
^ ^
^

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:

Python Program to Print Hollow Square Star With Diagonals

Python Program to Print Hollow Square Star Pattern with Diagonals Using For Loop

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Given the sides of the square, the task is to print the hollow Square star pattern with diagonals in C, C++, and Python.

Examples:

Example1:

Input:

given number of sides of square =10

Output:

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

Example2:

Input:

given number of sides of square =10
given character to print =$

Output:

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

Program to Print Hollow Square Star Pattern with Diagonals in C, C++, and Python

Below are the ways to print the hollow square star with Diagonals pattern in C, C++, and Python.

Method #1: Using For loop (Star Character)

Approach:

  • Give the side of the square as static input and store it in a variable.
  • Loop till the side length of the square using For loop.
  • Loop till the side length of the square using another nested For loop.
  • We use the If Else statement to check If the side length is 0 or maximum – 1. (For the Outer Boundary of the square)
  • We can say it is diagonal if a row equals a column, or a row equals N-i+1 (where i is the current row number).
  • We merge these two conditions using if and or operator.
  • We have or operator in Python,|| operator in C, C++, and Java
  • If it is true then print * else print space.
  • The Exit of the Program.

1)  Python Implementation

Below is the implementation:

Python Program to Print Hollow Square Star Pattern with Diagonals Using For Loop

# Give the side of the square as static input and store it in a variable.
squareside = 10
# Loop till the side length of the square using For loop.
for m in range(squareside):
    # Loop till the side length of the square using another nested For loop.
    for n in range(squareside):
        # We use the If Else statement to check If the side length is 0 or maximum – 1. (For the Outer Boundary of the square)
        # We can say it is diagonal if a row equals a column, or a row equals N-i+1 (where i is the current row number).
        # We merge these two conditions using if and or operator.
        # We have or operator in Python,|| operator in C, C++, and Java
        # If it is true then print * else print space.
        if(m == 0 or m == squareside - 1 or n == 0 or n == squareside - 1 or m == n or n == (squareside - 1 - m)):
            print('*', end=' ')
        else:
            print(' ', end=' ')
    print()

Output:

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

2) C++Implementation

Below is the implementation:

CPP Program to Print Hollow Square Star Pattern with Diagonals Using For Loop

#include <iostream>
using namespace std;

int main()
{

    // Give the side of the square as static input and store
    // it in a variable.
    int squareside = 10;
    // Loop till the side length of the square using For
    // loop.
    for (int m = 0; m < squareside; m++) {
        // Loop till the side length of the square using
        // another nested For loop.
        for (int n = 0; n < squareside; n++) {
            // We use the If Else statement to check If the
            // side length is 0 or maximum – 1.
            if (m == 0 || m == squareside - 1 || n == 0
                || n == squareside - 1)
                cout << "* ";
            else
                cout << "  ";
        }
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

C Program to Print Hollow Square Star Pattern with Diagonals Using For Loop

#include <stdio.h>

int main()
{
    // Give the side of the square as static input and store
    // it in a variable.
    int squareside = 10;
    // Loop till the side length of the square using For
    // loop.
    for (int m = 0; m < squareside; m++) {
        // Loop till the side length of the square using
        // another nested For loop.
        for (int n = 0; n < squareside; n++) {
            /*We use the If Else statement to check If the
                side length is 0 or maximum – 1. (For the
               Outer Boundary of the square) We can say it
               is diagonal if a row equals a column, or a
               row equals N-i+1 (where i is the current row
                number). We merge these two conditions using
               if and or operator. We have or operator in
               Python,|| operator in C,
                C++, and Java If it is true then print *
               else print space.*/
            if (m == 0 || m == squareside - 1 || n == 0
                || n == squareside - 1 || m == n
                || n == (squareside - 1 - m))
                printf("* ");
            else
                printf("  ");
        }
        printf("\n");
    }
}

Output:

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

Method #2: Using For Loop (User Character)

Approach:

  • Give the side of the square as user input and store it in a variable.
  • Scan the character to print as user input and store it in a variable.
  • Loop till the side length of the square using For loop.
  • Loop till the side length of the square using another nested For loop.
  • We use the If Else statement to check If the side length is 0 or maximum – 1.
  • We can say it is diagonal if a row equals a column, or a row equals N-i+1 (where i is the current row number).
  • We merge these two conditions using if and or operator.
  • We have or operator in Python,|| operator in C, C++, and Java
  • If it is true then print given character else print space.
  • The Exit of the Program.

1) Python Implementation

  • Give the number of sides of the square 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:

Python Program to Print Hollow Square Star Pattern with Diagonals Using For Loop -User Character

# Give the side of the square as user input and store it in a variable.
squareside = int(input('Enter some random number of sides of square = '))
# Scan the character to print as user input and store it in a variable.
characte = input('Enter some random character to print = ')
# Loop till the side length of the square using For loop.
for m in range(squareside):
    # Loop till the side length of the square using another nested For loop.
    for n in range(squareside):
        # We use the If Else statement to check If the side length is 0 or maximum – 1. (For the Outer Boundary of the square)
        # We can say it is diagonal if a row equals a column, or a row equals N-i+1 (where i is the current row number).
        # We merge these two conditions using if and or operator.
        # We have or operator in Python,|| operator in C, C++, and Java
        # If it is true then print * else print space.
        if(m == 0 or m == squareside - 1 or n == 0 or n == squareside - 1 or m == n or n == (squareside - 1 - m)):
            print(characte, end=' ')
        else:
            print(' ', end=' ')
    print()

Output:

Enter some random number of sides of square = 10
Enter some random character to print = $
$ $ $ $ $ $ $ $ $ $ 
$ $                   $ $ 
$   $               $   $ 
$       $        $      $ 
$          $   $        $ 
$           $ $         $ 
$           $   $       $ 
$       $          $    $ 
$   $                $  $ 
$ $ $ $ $ $ $ $ $ $

2) C++Implementation

  • Give the number of sides of the square 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:

CPP Program to Print Hollow Square Star Pattern with Diagonals Using For Loop User Character

#include <iostream>
using namespace std;

int main(void)
{
    int sidesnum;
    char characte;
    // Give the number of sides of the square as user input
    // using cin and
    // store it in a variable.
    cout << "Enter some random number of sides of the "
            "square = "
         << endl;
    cin >> sidesnum;
    // 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 square pattern.
    for (int m = 0; m < sidesnum; m++) {
        for (int n = 0; n < sidesnum; n++) {
            /*We use the If Else statement to check If the
             side length is 0 or maximum – 1. (For the
            Outer Boundary of the square) We can say it
            is diagonal if a row equals a column, or a
            row equals N-i+1 (where i is the current row
             number). We merge these two conditions using
            if and or operator. We have or operator in
            Python,|| operator in C,
             C++, and Java If it is true then print *
            else print space.*/
            if (m == 0 || m == sidesnum - 1 || n == 0
                || n == sidesnum - 1 || m == n
                || n == (sidesnum - 1 - m))
                cout << characte << " ";
            else
                cout << "  ";
        }
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of sides of the square = 
10
Enter some random character to print = 
$
$ $ $ $ $ $ $ $ $ $ 
$ $                   $ $ 
$   $               $   $ 
$       $        $      $ 
$          $   $        $ 
$           $ $         $ 
$           $   $       $ 
$       $          $    $ 
$   $                $  $ 
$ $ $ $ $ $ $ $ $ $

3) C Implementation

  • Give the number of sides of the square 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:

C Program to Print Hollow Square Star Pattern with Diagonals Using For Loop User Character

#include <stdio.h>

int main()
{
    int sidesnum;
    char characte;
    // Give the number of sides of the square 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 square pattern.
    scanf("%d%c", &sidesnum, &characte);
    printf("\n");

    for (int m = 0; m < sidesnum; m++) {
        for (int n = 0; n < sidesnum; n++) {
            /*We use the If Else statement to check If the
                side length is 0 or maximum – 1. (For the
               Outer Boundary of the square) We can say it
               is diagonal if a row equals a column, or a
               row equals N-i+1 (where i is the current row
                number). We merge these two conditions using
               if and or operator. We have or operator in
               Python,|| operator in C,
                C++, and Java If it is true then print *
               else print space.*/
            if (m == 0 || m == sidesnum - 1 || n == 0
                || n == sidesnum - 1 || m == n
                || n == (sidesnum - 1 - m))
                printf("%c ", characte);
            else
                printf("  ");
        }
        printf("\n");
    }
}

Output:

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

Related Programs:

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

Examples:

Example1:

Input:

given number of sides of square =13

Output:

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

Example2:

Input:

given number of sides of square =7
given character to print =@

Output:

Enter some random number of sides of the square = 
7
Enter some random character to print = 
@
@ @ @ @ @ @ @ 
@ @ @ @ @ @ @ 
@ @ @ @ @ @ @ 
@ @ @ @ @ @ @ 
@ @ @ @ @ @ @ 
@ @ @ @ @ @ @ 
@ @ @ @ @ @ @

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

Below are the ways to print square star pattern in C, C++, and Python.

Method #1: Using For Loop (Star Character)

Approach:

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

1) Python Implementation

Below is the implementation:

# Give the number of sides of the square as static input and store it in a variable.
squareside = 13
# Using two Nested For loops print the square star pattern.
for m in range(squareside):
    for n in range(squareside):
      # printing the star character
        print('*', end=' ')
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main(void)
{
    // Give the number of sides of the square as static
    // input
    // and store it in a variable.
    int squareside = 20;
    // Using Nested For loops print the square pattern.
    for (int m = 1; m <= squareside; m++) {
        for (int n = 1; n <= squareside; n++) {
            // printing the star character
            cout << "* ";
        }
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{
    // Give the number of sides of the square as static
    // input and store it in a variable.
    int squareside = 5, m, n;
    // Using Nested For loops print the square pattern.
    for (m = 0; m < squareside; m++) {
        for (n = 0; n < squareside; n++) {
            // printing the star character
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For Loop (User Character)

Approach:

  • Give the number of sides of the square as user input and store it in a variable.
  • Scan the character to print and store it in a variable.
  • Using Nested For loops.
  • Print the given character
  • The Exit of the Program.

1) Python Implementation

  • Give the number of sides of the square 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 square 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.
sidesnum = int(input('Enter some random number of sides of the square= '))
characte = input('Enter some random character to print = ')
# Using Nested For loops print the square pattern.
for m in range(sidesnum):
    for n in range(sidesnum):
        print(characte, end=' ')
    print()

Output:

Enter some random number of sides of the square= 13
Enter some random character to print = %
% % % % % % % % % % % % % 
% % % % % % % % % % % % % 
% % % % % % % % % % % % % 
% % % % % % % % % % % % % 
% % % % % % % % % % % % % 
% % % % % % % % % % % % % 
% % % % % % % % % % % % % 
% % % % % % % % % % % % % 
% % % % % % % % % % % % % 
% % % % % % % % % % % % % 
% % % % % % % % % % % % % 
% % % % % % % % % % % % % 
% % % % % % % % % % % % % 

2) C++Implementation

  • Give the number of sides of the square 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 sidesnum;
    char characte;
    // Give the number of sides of the square as user input
    // using cin and
    // store it in a variable.
    cout << "Enter some random number of sides of the "
            "square = "
         << endl;
    cin >> sidesnum;
    // 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 square pattern.
    for (int m = 1; m <= sidesnum; m++) {
        for (int n = 1; n <= sidesnum; n++) {
            // print the given character
            cout << characte << " ";
        }
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of sides of the square = 
7
Enter some random character to print = 
@
@ @ @ @ @ @ @ 
@ @ @ @ @ @ @ 
@ @ @ @ @ @ @ 
@ @ @ @ @ @ @ 
@ @ @ @ @ @ @ 
@ @ @ @ @ @ @ 
@ @ @ @ @ @ @ 

3) C Implementation

  • Give the number of sides of the square 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 sidesnum;
    char characte;
    // Give the number of sides of the square 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 square pattern.
    scanf("%d%c", &sidesnum,&characte);
    printf("\n");
    
    for (int m = 1; m <= sidesnum; m++) {
        for (int n = 1; n <= sidesnum; n++) {
            // print the given character
            printf("%c ", characte);
        }
        printf("\n");
    }
}

Output:

5$
$ $ $ $ $ 
$ $ $ $ $ 
$ $ $ $ $ 
$ $ $ $ $ 
$ $ $ $ $

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 Mirrored Rhombus 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 Rhombus, the task is to print 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 =8
Given character to print ='<'

Output:

<<<<<<<<
 <<<<<<<<
  <<<<<<<<
   <<<<<<<<
    <<<<<<<<
     <<<<<<<<
      <<<<<<<<
       <<<<<<<<

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

Below are the ways to print mirrored Rhonbus star Patterns 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.
  • 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).
  • Print the star character in this inner loop with the space character.
  • 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):
      # Print the star character in this inner loop with the space character.
        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++) { // Print the star character in this
                    // inner loop with the space character.
            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++) { // Print the star character in this
                    // inner loop with the space character.
            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).
  • Print the given character in this inner loop with the space character.
  • 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):
      # Print the givencharacter in this inner loop with the space character.
        print(givencharacter, end='')
    # Print the newline character after the end of two nested for loops.
    print()

Output:

Enter some random number of rows of rhombus = 8
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++) { // Print the givencharacter in this
                    // inner loop with the space character.
            cout << givencharacter<<" ";
        }

        // 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 = 
8
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++) { // Print the givencharacter in this
                    // inner loop with the space character.
            printf("%c ", givencharacter);
        }

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

Output:

8<
<<<<<<<<
 <<<<<<<<
  <<<<<<<<
   <<<<<<<<
    <<<<<<<<
     <<<<<<<<
      <<<<<<<<
       <<<<<<<<

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 Square Number Pattern

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

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 and a number, the task is to print a square number pattern in C, C++, and Python.

Examples:

Example1:

Input:

given number of sides = 5
given element of square =3

Output:

3 3 3 3 3 
3 3 3 3 3 
3 3 3 3 3 
3 3 3 3 3 
3 3 3 3 3

Example2:

Input:

given number of sides = 7
given element of square =9

Output:

9 9 9 9 9 9 9 
9 9 9 9 9 9 9 
9 9 9 9 9 9 9 
9 9 9 9 9 9 9 
9 9 9 9 9 9 9 
9 9 9 9 9 9 9 
9 9 9 9 9 9 9

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

Below are the ways to print square number pattern in C, C++, and Python.

Method #1: Using For loop (Element is of static input)

Approach:

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

1) Python Implementation

Below is the implementation:

# Give the number of sides of the square as static input and store it in a variable.
sidesnum = 10
# Using Nested For loops print the square pattern.
for m in range(sidesnum):
    for n in range(sidesnum):
        print('1', end=' ')
    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 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(void)
{
    // Give the number of sides of the square as static
    // input
    // and store it in a variable.
    int sidesnum = 7;
    // Using Nested For loops print the square pattern.
    for (int m = 1; m <= sidesnum; m++) {
        for (int n = 1; n <= sidesnum; n++) {
            cout << "1 ";
        }
        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

3) C Implementation

Below is the implementation:

#include<stdio.h>
 
int main()
{
    // Give the number of sides of the square as static input
    // and store it in a variable.
    int sidesnum = 7, m, n;
   // Using Nested For loops print the square pattern.
    for(m = 0; m < sidesnum; m++)
    {
    	for(n = 0; n < sidesnum; n++)
        {
           	printf("1 ");
        }
        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 

Method #2: Using For loop (Element is of User input)

Approach:

  • Give the number of sides of the square as user input and store it in a variable.
  • Give the Element as user input and store it in another variable.
  • Using Nested For loops print the square pattern.
  • The Exit of the Program.

1) Python Implementation

  • Give the number of sides of the square 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.

Below is the implementation:

# Give the number of sides of the square 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.
sidesnum = int(input('Enter some random number of sides = '))
eleme = int(input('Enter some random element = '))
# Using Nested For loops print the square pattern.
for m in range(sidesnum):
    for n in range(sidesnum):
        print(eleme, end=' ')
    print()

Output:

Enter some random number of sides = 11
Enter some random element = 23
23 23 23 23 23 23 23 23 23 23 23 
23 23 23 23 23 23 23 23 23 23 23 
23 23 23 23 23 23 23 23 23 23 23 
23 23 23 23 23 23 23 23 23 23 23 
23 23 23 23 23 23 23 23 23 23 23 
23 23 23 23 23 23 23 23 23 23 23 
23 23 23 23 23 23 23 23 23 23 23 
23 23 23 23 23 23 23 23 23 23 23 
23 23 23 23 23 23 23 23 23 23 23 
23 23 23 23 23 23 23 23 23 23 23 
23 23 23 23 23 23 23 23 23 23 23

2) C++Implementation

  • Give the number of sides of the square as user input using cin and store it in a variable.
  • Give the Element as user input using cin and store it in another variable.

Below is the implementation:

#include <iostream>
using namespace std;

int main(void)
{
    int sidesnum, eleme;
    // Give the number of sides of the square as user input
    // using cin and
    // store it in a variable.
    cin >> sidesnum;
    // Give the Element as user input using cin and store it
    // in another variable.
    cin >> eleme;

    // Using Nested For loops print the square pattern.
    for (int m = 1; m <= sidesnum; m++) {
        for (int n = 1; n <= sidesnum; n++) {
            cout << eleme << " ";
        }
        cout << endl;
    }

    return 0;
}

3) C Implementation

  • Give the number of sides of the square as user input using scanf and store it in a variable.
  • Give the Element as user input using scanf and store it in another variable.

Below is the implementation:

#include<stdio.h>
 
int main()
{  int sidesnum, eleme;
    // Give the number of sides of the square as user input
    // using cin and
    // store it in a variable.
    scanf("%d",&sidesnum);
    // Give the Element as user input using cin and store it
    // in another variable.
   scanf("%d",&eleme);
    // Using Nested For loops print the square pattern.
    for (int m = 1; m <= sidesnum; m++) {
        for (int n = 1; n <= sidesnum; n++) {
            printf("%d ",eleme);
        }
        printf("\n");
    }

}

Output:

22
5
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5

Related Programs: