# 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: