Solid and hollow rectangle star pattern in c programming
ADVERTISEMENTS
In this article, you will learn how to print the solid and hollow rectangle star patterns in the c programming language.
Examples
Enter the number of rows & columns:
4
6
4
6
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
Enter the number of rows & columns:
4
6
4
6
* * * * * *
* *
* *
* * * * * *
* *
* *
* * * * * *
You should have knowledge of the following topics in c programming to understand these programs:
- C
main()
function - C
for
loop statement - C
if
condition statement - C
printf()
function
1. C program to print solid rectangle star pattern
// C program to print solid rectangle star pattern
#include <stdio.h>
int main() {
int rows, columns;
printf("Enter the number of rows & columns:\n");
scanf("%d%d", &rows, &columns);
printf("\n");
if (rows > 0 && columns > 0) {
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= columns; j++)
printf("* ");
printf("\n");
}
}
return 0;
}
Output
Enter the number of rows & columns:
4
6
* * * * * *
* * * * * *
* * * * * *
* * * * * *
2. C program to print hollow rectangle star pattern
// C program to print hollow rectangle star pattern
#include <stdio.h>
int main() {
int rows, columns;
printf("Enter the number of rows & columns:\n");
scanf("%d%d", &rows, &columns);
printf("\n");
if (rows > 0 && columns > 0) {
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= columns; j++) {
if (i == 1 || i == rows || j == 1 || j == columns)
printf("* ");
else
printf(" ");
}
printf("\n");
}
}
return 0;
}
Output
Enter the number of rows & columns:
4
6
* * * * * *
* *
* *
* * * * * *
Explanation
In these programs, we have taken input 4
and 6
from the user and applied loop iteration on these rows and columns using the nested-for
loop statement.