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: