Print rhombus pattern in C and Java
Write a C and Java program to print the Rhombus pattern of stars. A rhombus is a quadrilateral, all of whose sides have the same length.
This post covers the following patterns:
Pattern 1: Rhombus
Pattern 2: Mirror of Rhombus
Pattern 3: Hollow Rhombus
Pattern 4: Mirror of Hollow Rhombus
Pattern 1: Rhombus
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
We can have n rows. Here, the 1st row will contain (n-1) spaces, followed by n star, the 2nd row will contain (n-2) spaces, followed by n stars, and so on. We can use nested loops to print this pattern, where the outer loop represents row number (say i) and the inner loop prints space (n-i times), followed by the star pattern (n times).
C
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <stdio.h> int main(void) { // number of rows int n = 5; int i, j; for (i = 1; i <= n; i++) { // print space for (j = i; j < n; j++) { printf(" "); } // print `n` stars for (j = 1; j <= n; j++) { printf("*"); } // move to the next row printf("\n"); } return 0; } |
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Main { public static void main(String[] args) { // number of rows final int n = 5; for (int i = 1; i <= n; i++) { // print space for (int j = i; j < n; j++) { System.out.print(' '); } // Print `n` stars for (int j = 1; j <= n; j++) { System.out.print('*'); } // move to the next row System.out.print(System.lineSeparator()); } } } |
Pattern 2: Mirror of Rhombus
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Suppose we have n rows. 1st row will contain 0 spaces, followed by n stars, the 2nd row will contain 1 space, followed by n stars, the 3rd row will contain 2 spaces, followed by n stars, and so on. We can use nested loops to print this pattern, where the outer loop represents row number (say i) and the inner loop prints space (i-1 times), followed by the star pattern (n times).
C
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <stdio.h> int main(void) { // number of rows int n = 5; int i, j; for (i = 1; i <= n; i++) { // print space for (j = 1; j < i; j++) { printf(" "); } // print star(*) `n` times for (j = 1; j <= n; j++) { printf("*"); } // move to the next row printf("\n"); } return 0; } |
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Main { public static void main(String[] args) { // number of rows final int n = 5; for (int i = 1; i <= n; i++) { // print space for (int j = 1; j < i; j++) { System.out.print(' '); } // Print star(*) `n` times for (int j = 1; j <= n; j++) { System.out.print('*'); } // move to the next row System.out.print(System.lineSeparator()); } } } |
Pattern 3: Hollow Rhombus
* * * * *
* *
* *
* *
* * * * *
The idea remains the same as Pattern 1, but here we print ‘*’ only for the last row and first & last positions for each row. For all other positions, we print space.
C
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <stdio.h> int main(void) { // number of rows int n = 5; int i, j; for (i = 1; i <= n; i++) { // print space for (j = i; j < n; j++) { printf(" "); } for (j = 1; j <= n; j++) { // print '*' for boundaries if (i == 1 || i == n || j == 1 || j == n) { printf("*"); } else { printf(" "); } } // move to the next row printf("\n"); } return 0; } |
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
class Main { public static void main(String[] args) { // number of rows final int n = 5; for (int i = 1; i <= n; i++) { // print space for (int j = i; j < n; j++) { System.out.print(' '); } for (int j = 1; j <= n; j++) { // print '*' for boundaries if (i == 1 || i == n || j == 1 || j == n) { System.out.print('*'); } else { System.out.print(' '); } } // move to the next row System.out.print(System.lineSeparator()); } } } |
Pattern 4: Mirror of Hollow Rhombus
* * * * *
* *
* *
* *
* * * * *
Here the idea remains the same as Pattern 2, but we print ‘*’ only for the last row and first & last cell in each row. Spaces will fill all other positions.
C
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <stdio.h> int main(void) { // number of rows int n = 5; int i, j; for (i = 1; i <= n; i++) { // print space for (j = 1; j < i; j++) { printf(" "); } for (j = 1; j <= n; j++) { // print '*' for boundaries if (i == 1 || i == n || j == 1 || j == n) { printf("*"); } else { printf(" "); } } // move to the next row printf("\n"); } return 0; } |
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
class Main { public static void main(String[] args) { // number of rows final int n = 5; for (int i = 1; i <= n; i++) { // print space for (int j = 1; j < i; j++) { System.out.print(' '); } for (int j = 1; j <= n; j++) { // print '*' for boundaries if (i == 1 || i == n || j == 1 || j == n) { System.out.print('*'); } else { System.out.print(' '); } } // move to the next row System.out.print(System.lineSeparator()); } } } |
That’s all about printing a rhombus pattern in C and Java.
Exercise: Extend the solution to print parallelogram (quadrilateral with two pairs of parallel sides).
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)