Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Program to print solid and hollow rhombus patterns in C
A rhombus pattern in C is a diamond-like shape created using stars (*). There are two types: solid rhombus (completely filled with stars) and hollow rhombus (only the border is filled with stars).
Syntax
// Nested loops to create rhombus pattern
for(row = 1; row <= n; row++) {
// Print leading spaces
for(space = 1; space <= n - row; space++) {
printf(" ");
}
// Print stars or spaces based on pattern type
for(col = 1; col <= n; col++) {
// Logic for solid or hollow pattern
}
printf("<br>");
}
Example 1: Hollow Rhombus Pattern
In a hollow rhombus, only the first row, last row, first column, and last column contain stars −
#include <stdio.h>
int main() {
int rows = 5;
int r, c;
printf("Hollow Rhombus Pattern:<br>");
for(r = 1; r <= rows; r++) {
/* Print leading spaces */
for(c = 1; c <= rows - r; c++) {
printf(" ");
}
/* Print stars and spaces */
for(c = 1; c <= rows; c++) {
if(r == 1 || r == rows || c == 1 || c == rows) {
printf("*");
} else {
printf(" ");
}
}
printf("<br>");
}
return 0;
}
Hollow Rhombus Pattern:
*****
* *
* *
* *
*****
Example 2: Solid Rhombus Pattern
In a solid rhombus, all positions are filled with stars −
#include <stdio.h>
int main() {
int rows = 5;
int r, c;
printf("Solid Rhombus Pattern:<br>");
for(r = 1; r <= rows; r++) {
/* Print leading spaces */
for(c = 1; c <= rows - r; c++) {
printf(" ");
}
/* Print stars */
for(c = 1; c <= rows; c++) {
printf("*");
}
printf("<br>");
}
return 0;
}
Solid Rhombus Pattern:
*****
*****
*****
*****
*****
Example 3: Combined Program with User Input
This program demonstrates both patterns with user-specified dimensions −
#include <stdio.h>
int main() {
int rows = 4;
int r, c;
printf("Number of rows: %d<br><br>", rows);
/* Hollow Rhombus */
printf("Hollow Rhombus Pattern:<br>");
for(r = 1; r <= rows; r++) {
for(c = 1; c <= rows - r; c++) {
printf(" ");
}
for(c = 1; c <= rows; c++) {
if(r == 1 || r == rows || c == 1 || c == rows) {
printf("*");
} else {
printf(" ");
}
}
printf("<br>");
}
printf("<br>");
/* Solid Rhombus */
printf("Solid Rhombus Pattern:<br>");
for(r = 1; r <= rows; r++) {
for(c = 1; c <= rows - r; c++) {
printf(" ");
}
for(c = 1; c <= rows; c++) {
printf("*");
}
printf("<br>");
}
return 0;
}
Number of rows: 4 Hollow Rhombus Pattern: **** * * * * **** Solid Rhombus Pattern: **** **** **** ****
How It Works
- Outer loop: Controls the number of rows
- First inner loop: Prints leading spaces to create the slanted effect
- Second inner loop: Prints stars (*) or spaces based on pattern type
- Hollow condition: Stars only on borders (first/last row/column)
- Solid condition: Stars in all positions
Conclusion
Rhombus patterns in C use nested loops to create geometric shapes. The key is controlling spaces for alignment and applying conditions for hollow versus solid patterns.
Advertisements
