Write a C program to Print Square Number Pattern with example. For this, we are going to use For Loop and While Loop.
C program to Print Square Number Pattern
This program allows the user to enter any side of a square (In Square, all sides are equal). This value will decide the total number of rows and columns of a square. Here, we are going to print 1’s until it reaches to the user-specified rows, and columns (sides).
#include<stdio.h>
int main()
{
int i, j, Side;
printf("Please Enter Any Side of a Square : ");
scanf("%d", &Side);
for(i = 0; i < Side; i++)
{
for(j = 0; j < Side; j++)
{
printf("1");
}
printf("\n");
}
return 0;
}

Let us see the Nested for loop in C language
for(i = 0; i < Side; i++)
{
for(j = 0; j < Side; j++)
{
printf("1");
}
printf("\n");
}
First For Loop – First Iteration
From the above C Programming screenshot, you can see that the value of i is 0 and Side is 4. So, the condition (0 < 4) is True, and it will enter into second for loop
Second For Loop – First Iteration
The j value is 0 and the condition (0 < 4) is True. So, it will start executing the statement inside the loop. The following statement will print 1
printf("1");
The following statement will increment the value of j by 1 using the Increment Operator in C
j++
It will happen until it reaches 4, and after that condition inside the Outer loop will fail. Next, i value will be incremented to 1.
First For Loop – Second Iteration
The condition (1 < 4) is True. So, it will enter into second for loop.
Repeat the above steps until i becomes 4.
C program to Print Square Number Pattern
This C program for pattern is the same as the above example. Her we changed the print value from 1 to 0. It means, this program will print a square pattern of zeros.
#include<stdio.h>
int main()
{
int i, j, Side;
printf("Please Enter Any Side of a Square : ");
scanf("%d", &Side);
for(i = 0; i < Side; i++)
{
for(j = 0; j < Side; j++)
{
printf("0");
}
printf("\n");
}
return 0;
}
Please Enter Any Side of a Square : 7
0000000
0000000
0000000
0000000
0000000
0000000
0000000
C Program to Print Square Number Pattern
This program is same as first example, but this time we are using While loop (Just replacing For Loop with While Loop). I suggest you to refer to the C Programming While Loop to understand the loop Iteration.
#include<stdio.h>
int main()
{
int i, j, Side;
i = 0;
printf("Please Enter Any Side of a Square : ");
scanf("%d", &Side);
while(i < Side)
{
j = 0;
while(j < Side)
{
printf("1");
j++;
}
i++;
printf("\n");
}
return 0;
}
Please Enter Any Side of a Square : 9
111111111
111111111
111111111
111111111
111111111
111111111
111111111
111111111
111111111
Print Square Number Pattern with Dynamic integer
This program allows the user to enter their own integer value, and the compiler will print that particular value in square pattern. For more number programs, please refer to the C Number Pattern Programs article.
#include<stdio.h>
int main()
{
int i, j, Num, Side;
printf(" Please Enter any Integer Value : ");
scanf("%d", &Num);
printf(" \nPlease Enter Any Side of a Square : ");
scanf("%d", &Side);
for(i = 0; i < Side; i++)
{
for(j = 0; j < Side; j++)
{
printf("%d", Num);
}
printf("\n");
}
return 0;
}
Please Enter any Integer Value : 9
Please Enter Any Side of a Square :
12
999999999999
999999999999
999999999999
999999999999
999999999999
999999999999
999999999999
999999999999
999999999999
999999999999
999999999999
999999999999
Also Read
- C Program to Print Square where each row contains one Number
- C Program to Print Square where each column contains one Number