How to write a C Program to Print Square Star Pattern with example?. And also show you how to print a square pattern with different symbols.
C program to Print Square Star Pattern
This C program allows the user to enter any side of a square (In Square, all sides are equal). This value will decide the number of rows and columns of a square. Here, we are going to print the stars until it reaches the user-specified rows and columns.
/* C program to Print Square Star Pattern */ #include<stdio.h> int main() { int i, j, Side; printf("Please Enter Any Side of a Square\n"); scanf("%d", &Side); for(i = 0; i < Side; i++) { for(j = 0; j < Side; j++) { printf("*"); } printf("\n"); } return 0; }
Let us see the Nested for loop
for(i = 0; i < Side; i++) { for(j = 0; j < Side; j++) { printf("*"); } printf("\n"); }
Outer Loop – First Iteration
From the above C Programming screenshot, you can observe that the value of i is 0 and Rows is 10 so, the condition (i < 10) is True. So, it will enter into second for loop
Inner Loop – First Iteration
The j value is 0 and the condition (j < 10) is True. So, it will start executing the statement inside the loop. The following statement will print *
printf("*");
The following statement will increment the Value of j by 1 using the Increment Operator
j++
It will happen until it reaches 10. After that, both the Inner Loop and Outer loop will terminate.
C Program to Print Square Pattern with Dynamic Character
This C program to display a square pattern with a dynamic character allows the user to enter the Symbol that he/she wants to print as a square pattern.
#include<stdio.h> int main() { int i, j, Side; char Ch; printf("Please Enter any Symbol\n"); scanf("%c", &Ch); printf("Please Enter Any Side of a Square\n"); scanf("%d", &Side); for(i = 0; i < Side; i++) { for(j = 0; j < Side; j++) { printf("%c", Ch); } printf("\n"); } return 0; }
Please Enter any Symbol
$
Please Enter Any Side of a Square
10
$$$$$$$$$$
$$$$$$$$$$
$$$$$$$$$$
$$$$$$$$$$
$$$$$$$$$$
$$$$$$$$$$
$$$$$$$$$$
$$$$$$$$$$
$$$$$$$$$$
$$$$$$$$$$