Write a program in C to print square patterns. In this C programming example, we will learn how to create a square pattern program in C.
Algorithm to print square pattern
- Declare a three-variable
- Then take an input with the help of scanf .
- Take a for loop in 2d dimensions and in loop initialize the variable , give conditions and increment it .
- Then print output with suitable patterns .
C program to print square pattern
#include<stdio.h> void main (){ int i,j,r; printf("enter rows "); scanf("%d",&r); for(i=1;i<=r;i++){ for(j=1;j<=r;j++){ printf("*"); } printf("\n"); } }
Explanation of the C Program
Step 1 : Start.
Step 2 : Create header file and include the library (stdio.h).
Step 3 : Create void main function .
Step 4 : Then declare the three variables (i,j,r).
Step 5 : Create an input with help of scanf for taken input from user , for r variable.
Step 6 : Create a for loop in 2d dimensions and in 2d dimensions take two for loop like; take a for loop and initialize the variable (i) ,give conditions and increment it , then a for loop in for loop and then in 2 for loop initialize the second variable (j),give condition and increment it.
Step 7 : Then print ‘*’ character in the second loop and print ‘\n’ in the first loop for sequential pattern print .
Step 8 : End .
Output :
Thus, in this way we learned how to create a star pattern in C programing.