C program to print square or rectangle star pattern
********************
********************
********************
********************
********************
C Programming Language Loop control in C Language (Article) Loop control in C Language (Program)
3240Program:
/** * C program to print rectangle star pattern */ #include int main() { int i, j, rows, columns; /* Input rows and columns from user */ printf("Enter number of rows: "); scanf("%d", &rows); printf("Enter number of columns: "); scanf("%d", &columns); /* Iterate through each row */ for(i=1; i<=rows; i++) { /* Iterate through each column */ for(j=1; j<=columns; j++) { /* For each column print star */ printf("*"); } /* Move to the next line/row */ printf("\n"); } return 0; }
Output:
Enter number of rows: 5 Enter number of columns: 10 ********** ********** ********** ********** **********
Explanation:
Step by step descriptive logic to print rectangle star pattern.
- Input number of rows and columns from user. Store it in a variable say rows and columns.
- To iterate through rows, run an outer loop from 1 to rows. Define a loop with structure
for(i=1; i<=rows; i++)
. - To iterate through columns, run an inner loop from 1 to columns. Define a loop with structure
for(j=1; j<=columns; j++)
. - Inside inner loop print star *.
- After printing all columns of a row. Move to next line i.e. print a new line.
This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.