C program to Print Square Number Pattern

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).

/* C program to Print Square Number Pattern */
#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;
}
C program to Print Square Number Pattern 1

Let us see the Nested for loop

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

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 square number 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

/* C program to Print Square Number Pattern */
#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

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 While Loop to understand the loop Iteration.

/* C program to Print Square Number Pattern */
#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

Program to 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.

/* C program to Print Square Number Pattern */
#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