CPP Programming

Python Program to Print Hollow Rhombus Using For Loop Star Pattern

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

Examples:

Example1:

Input:

given number of rows of rhombus =11

Output:

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

Example2:

Input:

given number of rows of rhombus =6
given character to print =<

Output:

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

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

Below are the ways to print Hollow Rhombus 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.
  • Using Nested For loops print the rhombus star pattern.
  • We use the If Else statement to check If the side length of the rhombus(rows) is 0 or maximum – 1.
  • 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 Rhombus Star Pattern

# Give the number of rows of the rhombus as static input and store it in a variable.
rhombusrows = 11
# Using Nested For loops print the rhombus star pattern.
for m in range(rhombusrows, 0, -1):
    for n in range(1, m):
        print(' ', end='')
    for k in range(0, rhombusrows):
      # We use the If Else statement to check If the side length of the rhombus(rows) is 0 or maximum – 1.
       # If it is true then print * else print space.
        if(m == 1 or m == rhombusrows or k == 0 or k == rhombusrows - 1):
            print('*', end=' ')
        else:
            print(' ', end=' ')
    print()

Output:

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

2) C++Implementation

Below is the implementation:

CPP Program to Print Hollow Rhombus Using For Loop Star Pattern

#include <iostream>
using namespace std;

int main(void)
{
    // Give the number of rows of the rhombus as static
    // input and store it in a variable.
    int rhombusrows = 6;
    // Using Nested For loops print the rhombus star
    // pattern.
    for (int m = rhombusrows; m > 0; m--) {
        for (int n = 1; n < m; n++) {
            cout << "  ";
        }
        for (int k = 0; k < rhombusrows; k++) {
            // We use the If Else statement to check If the
            // side length of the rhombus(rows) is 0 or
            // maximum – 1. If it is true then print * else
            // print space.
            if (m == 1 || m == rhombusrows || k == 0
                || k == rhombusrows - 1)
            cout << "* ";
            else
                cout << "  ";
        }
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

C Program to Print Hollow Rhombus Using For Loop Star Pattern

#include <stdio.h>

int main()
{
    // Give the number of rows of the rhombus as static
    // input and store it in a variable.
    int rhombusrows = 9;
    // Using Nested For loops print the rhombus star
    // pattern.
    for (int m = rhombusrows; m > 0; m--) {
        for (int n = 1; n < m; n++) {
            printf("  ");
        }
        for (int k = 0; k < rhombusrows; k++) {
            // We use the If Else statement to check If the
            // side length of the rhombus(rows) is 0 or
            // maximum – 1. If it is true then print * else
            // print space.
            if (m == 1 || m == rhombusrows || k == 0
                || k == rhombusrows - 1)
                printf("* ");
            else
                printf("  ");
        }
        printf("\n");
    }
}

Output:

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

Method #2: Using For Loop (User Character)

Approach:

  • Give the number of rows of the rhombus as static input and store it in a variable.
  • Scan the character to print as user input and store it in a variable.
  • Using Nested For loops print the rhombus star pattern.
  • We use the If Else statement to check If the side length of the rhombus(rows) is 0 or maximum – 1.
  • If it is true then print * else print space.
  • The Exit of the Program.

1) Python Implementation

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

Below is the implementation:

D:\Content\Shivam\selenium\Py Images\Python Program to Print Hollow Rhombus Using For Loop User Character.png

# Give the number of sides of the rhombus as user input using int(input()) and store it in a variable.
# Give the Element as user input using int(input()) and store it in another variable.
rhombusrows = int(input('Enter some random number of rows of the rhombus= '))
characte = input('Enter some random character to print = ')
# Using Nested For loops print the rhombus star pattern.
for m in range(rhombusrows, 0, -1):
    for n in range(1, m):
        print(' ', end='')
    for k in range(0, rhombusrows):
      # We use the If Else statement to check If the side length of the rhombus(rows) is 0 or maximum – 1.
       # If it is true then print * else print space.
        if(m == 1 or m == rhombusrows or k == 0 or k == rhombusrows - 1):
            print(characte, end=' ')
        else:
            print(' ', end=' ')
    print()

Output:

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

2) C++Implementation

  • Give the number of sides of the rhombus as user input using cin and store it in a variable.
  • Create a character variable.
  • Give the character as user input using cin and store it in another variable.

Below is the implementation:

CPP Program to Print Hollow Rhombus Using For Loop User Character

#include <iostream>
using namespace std;

int main(void)
{
    int rhombusrows;
    char characte;
    // Give the number of rows of the rhombus as user input
    // using cin and
    // store it in a variable.
    cout << "Enter some random number of rows of the "
            "rhombus = "
         << endl;
    cin >> rhombusrows;
    // Create a character variable.
    // Give the character as user input using cin and store
    // it in another variable.
    cout << "Enter some random character to print = "
         << endl;
    cin >> characte;
    cout << endl;
    for (int m = rhombusrows; m > 0; m--) {
        for (int n = 1; n < m; n++) {
            cout << "  ";
        }
        for (int k = 0; k < rhombusrows; k++) {
            // We use the If Else statement to check If the
            // side length of the rhombus(rows) is 0 or
            // maximum – 1. If it is true then print * else
            // print space.
            if (m == 1 || m == rhombusrows || k == 0
                || k == rhombusrows - 1)
                cout << characte << " ";
            else
                cout << "  ";
        }
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

  • Give the number of sides of the rhombus as user input using scanf and store it in a variable.
  • Create a character variable.
  • Give the character as user input using scanf and store it in another variable.

Below is the implementation:

D:\Content\Shivam\selenium\Py Images\Python Program to Print Hollow Rhombus Using For Loop User Character.png

#include <stdio.h>

int main()
{
    int rhombusrows;
    char characte;
    // Give the number of rows of the rhombus as user input
    // using scanf and
    // store it in a variable.
    // Create a character variable.
    // Give the character as user input using scanf and
    // store it in another variable.
    // Using Nested For loops print the rhombus pattern.
    scanf("%d%c", &rhombusrows, &characte);
    printf("\n");
    // Using Nested For loops print the rhombus star
    // pattern.
    for (int m = rhombusrows; m > 0; m--) {
        for (int n = 1; n < m; n++) {
            printf("  ");
        }
        for (int k = 0; k < rhombusrows; k++) {
            // We use the If Else statement to check If the
            // side length of the rhombus(rows) is 0 or
            // maximum – 1. If it is true then print * else
            // print space.
            if (m == 1 || m == rhombusrows || k == 0
                || k == rhombusrows - 1)
                printf("%c ", characte);
            else
                printf("  ");
        }
        printf("\n");
    }
}

Output:

5 :
    : : : : : 
   :       : 
  :       : 
 :       : 
: : : : :

Related Programs:

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

Python Program to Print Hollow Square Star With Diagonals

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 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 Double the Number Pattern

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

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

Examples:

Example1:

Input:

Given number of rows = 10

Output:

1 
2 1 
4 2 1 
8 4 2 1 
16 8 4 2 1 
32 16 8 4 2 1 
64 32 16 8 4 2 1 
128 64 32 16 8 4 2 1 
256 128 64 32 16 8 4 2 1

Example2:

Input:

Given number of rows = 7

Output:

  1 
  2   1 
  4   2   1 
  8   4   2   1 
 16   8   4   2   1 
 32  16   8   4   2   1

Program to Print Double the Number Pattern in C, C++, and Python

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

Method #1: Using For Loop (Static Input)

Approach:

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

1) Python Implementation

Below is the implementation:

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

Output:

  1 
  2   1 
  4   2   1 
  8   4   2   1 
 16   8   4   2   1 
 32  16   8   4   2   1

2) C++ Implementation

Below is the implementation:

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

int main()
{

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

Output:

1 
2 1 
4 2 1 
8 4 2 1 
16 8 4 2 1 
32 16 8 4 2 1 
64 32 16 8 4 2 1 
128 64 32 16 8 4 2 1 
256 128 64 32 16 8 4 2 1

3) C Implementation

Below is the implementation:

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

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

Output:

1 
2 1 
4 2 1

Method #2: Using For Loop (User Input)

Approach:

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

1) Python Implementation

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

Below is the implementation:

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

Output:

Enter some random number of rows = 8
1 
2 1 
4 2 1 
8 4 2 1 
16 8 4 2 1 
32 16 8 4 2 1 
64 32 16 8 4 2 1

2) C++ Implementation

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

Below is the implementation:

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

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

    return 0;
}

Output:

5
1 
2 1 
4 2 1 
8 4 2 1

3) C Implementation

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

Below is the implementation:

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

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

Output:

5
1 
2 1 
4 2 1 
8 4 2 1

Related Programs:

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

Python Program to Print Square Number Pattern

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

Given the number of rows 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:

Python Program to Print the Equilateral Triangle Pattern of Star

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

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

Examples:

Example1:

Input:

Given number of rows = 8

Output:

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

Example2:

Input:

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

Output

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

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

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

Method #1: Using For Loop (Star Character)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Take a variable to say g and initialize its value with (2*number of rows)-2.
  • Loop from 0 to the number of rows using For loop.
  • Loop from 0 to g using another For Loop(Inner For loop).
  • Print the space character in the inner For Loop.
  • Decrement the value of g by 1 after the end of the inner For loop.
  • Loop from 0 to m+1 using another For loop(Nested For loop) where m is the iterator value of the parent For loop.
  • Print the star character with space.
  • Print the Newline character after the end of the Two inner For loops.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

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

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

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

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

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

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

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

Output:

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

Method #2: Using For Loop (User Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Scan the character to print as user input and store it in a variable.
  • Take a variable to say g and initialize its value with (2*number of rows)-2.
  • Loop from 0 to the number of rows using For loop.
  • Loop from 0 to g using another For Loop(Inner For loop).
  • Print the space character in the inner For Loop.
  • Decrement the value of g by 1 after the end of the inner For loop.
  • Loop from 0 to m+1 using another For loop(Nested For loop) where m is the iterator value of the parent For loop.
  • Print the star character with space.
  • Print the Newline character after the end of the Two inner For loops.
  • The Exit of the Program.

1) Python Implementation

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

Below is the implementation:

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

Output

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

2) C++ Implementation

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

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

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

Output

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

3) C Implementation

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

Below is the implementation:

#include <stdio.h>

int main()
{

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

Output

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

Related Programs:

Python Program to Print Reverse Mirrored Right Triangle Star Pattern

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 number of rows of the right triangle, the task is to Reverse print Mirrored Right Triangle Star Pattern in C, C++, and Python.

Examples:

Example1:

Input:

given number of rows of the right triangle =8

Output:

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

Example2:

Input:

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

Output:

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

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

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

Method #1: Using For loop (Star Character)

Approach:

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

1) Python Implementation:

Below is the implementation:

# Give the number of rows of the right triangle pattern as static input and store it in a variable.
triNumRows = 8
# Iterate from 1 to given rows using First for loop.
for m in range(1, triNumRows+1):
    # Iterate from 1 to given rows using another for loop(Nested For loop)
    for n in range(1, triNumRows+1):
        # Check if the iterator value of the inner for loop is less than the given first iterator value using the If statement.
        if(n < m):
            # If the statement is true then print space.
            print(' ', end=' ')
        else:
            # Else print star character with space.
            print('*', end=' ')
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows of the right triangle pattern
    //  as static input and store it in a variable.
    int triNumRows = 8;
    // Iterate from 1 to given rows using First for loop.
    for (int m = 1; m <= triNumRows; m++) {
        // Iterate from 1 to given rows using another for
        // loop(Nested For loop)
        for (int n = 1; n <= triNumRows; n++) {
            /* Check if the iterator value of the inner for
             * loop is less than the given first iterator
             * value using the If statement.*/
            if (n < m) {
                // If the statement is true then print
                // space.
                cout << "  ";
            }
            else {
                // Else print star character with space.
                cout << "* ";
            }
        }
        // Print the newline character after the exit of the
        // inner for loop.
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows of the right triangle pattern
    //  as static input and store it in a variable.
    int triNumRows = 8;
    // Iterate from 1 to given rows using First for loop.
    for (int m = 1; m <= triNumRows; m++) {
        // Iterate from 1 to given rows using another for
        // loop(Nested For loop)
        for (int n = 1; n <= triNumRows; n++) {
            /* Check if the iterator value of the inner for
             * loop is less than the given first iterator
             * value using the If statement.*/
            if (n < m) {
                // If the statement is true then print
                // space.
                printf("  ");
            }
            else {
                // Else print star character with space.
                printf("* ");
            }
        }
        // Print the newline character after the exit of the
        // inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For Loop(User Character)

Approach:

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

1) Python Implementation:

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

Below is the implementation:

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

Output:

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

2) C++ Implementation

  • Give the number of rows of the Right Triangle as user input using cin and store it in a variable.
  • Give the Character as user input using cin and store it in another variable.

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

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

    return 0;
}

Output:

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

3) C Implementation

  • Give the number of rows of the Right Triangle as user input using scanf and store it in a variable.
  • Give the Character as user input using scanf and store it in another variable.

Below is the implementation:

#include <stdio.h>

int main()
{

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

Output:

9&
& & & & & & & & & 
    & & & & & & & & 
        & & & & & & & 
            & & & & & & 
                & & & & & 
                    & & & & 
                        & & & 
                            & & 
                                &

Related Programs:

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

Python Program to Print Hollow Rectangle Star Pattern

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

Given the length, breadth of the rectangle the task is to print the hollow rectangle star pattern in C, C++, and Python.

Examples:

Example1:

Input:

given length of rectangle =11
given breadth of rectangle =19

Output:

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

Example2:

Input:

given length of rectangle =20
given breadth of rectangle =5

Output:

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

 

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

Below are the ways to print the Hollow 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.
  • We use the If Else statement to check If the length or breadth number is 0 or maximum – 1.
  • If it is true then print * else print space.
  • 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 = 11
breadthnum = 19
# 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):
        # We use the If Else statement to check If the length or breadth number is 0 or maximum – 1.
        # If it is true then print * else print space.
        if(m == 0 or m == lengthnum - 1 or n == 0 or n == breadthnum - 1):
            print('*', end=' ')
        else:
            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++) {
            // We use the If Else statement to check If the
            // length or breadth number is 0 or maximum – 1.
            // If it is true then print * else print space.
            if (m == 0 || m == lengthnum - 1 || n == 0
                || n == breadthnum - 1)
                cout << "* ";
            else
                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 = 20;
    int breadthnum = 5;
    // 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++) {
            // We use the If Else statement to check If the
            // length or breadth number is 0 or maximum – 1.
            // If it is true then print * else print space.
            if (m == 0 || m == lengthnum - 1 || n == 0
                || n == breadthnum - 1)
                printf("* ");
            else
                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.
  • We use the If Else statement to check If the length or breadth number is 0 or maximum – 1.
  • If it is true then print * else print space.
  • 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):
        # We use the If Else statement to check If the length or breadth number is 0 or maximum – 1.
        # If it is true then print * else print space.
        if(m == 0 or m == lengthnum - 1 or n == 0 or n == breadthnum - 1):
            print('*', end=' ')
        else:
            print(' ', end=' ')
    print()

Output:

Enter some random length value of the rectangle = 20
Enter some random breadth value of the rectangle = 5
*  *  *  *  * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*  *  *  * *

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++) {
            // We use the If Else statement to check If the
            // length or breadth number is 0 or maximum – 1.
            // If it is true then print * else print space.
            if (m == 0 || m == lengthnum - 1 || n == 0
                || n == breadthnum - 1)
                cout << "* ";
            else
                cout << "  ";
        }
        cout << endl;
    }

    return 0;
}

Output:

Enter some random length and breadth of rectangle separated by spaces
11 19
* * * * * * * * * * * * * * * * * * * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
* * * * * * * * * * * * * * * * * * *

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++) {
            // We use the If Else statement to check If the
            // length or breadth number is 0 or maximum – 1.
            // If it is true then print * else print space.
            if (m == 0 || m == lengthnum - 1 || n == 0
                || n == breadthnum - 1)
                printf("* ");
            else
                printf("  ");
        }
        printf("\n");
    }
}

Output:

Enter some random length and breadth of rectangle separated by spaces
11 23
* * * * * * * * * * * * * * * * * * * * * * * 
*                                                      * 
*                                                      * 
*                                                      * 
*                                                      * 
*                                                      * 
*                                                      * 
*                                                      * 
*                                                      * 
*                                                      * 
* * * * * * * * * * * * * * * * * * * * * * *

Here we printed two blank spaces if the else condition is executed.

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

Python Program to Print Rectangle Star Pattern

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

Given the 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: