Draw a Rectangle in C language using For loop
ADVERTISEMENTS
Draw a rectangle in C language using for loop. In this article, you will learn how to draw a rectangle in c language using for loop.
Source Code
// Draw a Rectangle in C language using For loop
#include <stdio.h>
int main() {
int r = 0, c = 0, i, j;
// r - To store the number of rows
// c - To store the number of columns
printf("-----Enter the number of rows & columns-----\n");
scanf("%d%d", &r, &c);
for (i = 1; i <= r; i++) {
for (j = 1; j <= c; j++) {
if (i == 1 || i == r || j == 1 || j == c)
printf("* ");
else
printf(" ");
}
printf("\n");
}
return 0;
}
Output
-----Enter the number of rows & columns-----
8
12
* * * * * * * * * * * *
* *
* *
* *
* *
* *
* *
* * * * * * * * * * * *
8
12
* * * * * * * * * * * *
* *
* *
* *
* *
* *
* *
* * * * * * * * * * * *