Java Program to Print Hollow Rhombus Star Pattern
In this article we are going to see how to print the Hollow Rhombus Star Pattern
Example-1 When row value=4 **** * * * * ****
Example-2 When row value=5 ***** * * * * * * *****
Now, let’s see the actual program to print it.
Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.
Approach:
- Enter total row and store it in an integer variable
rows
. - Take one outer for loop to iterate the rows.
- Take two inner for loops, one to print the space and the other one to print the star.
- After each iteration print a new line.
JAVA Code:
Method-1 : Static Star Character
import java.util.Scanner; class pattern { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Rows : "); //Taking input from user int rows=scan.nextInt(); //Outer loop for (int r=1; r<=rows; r++) {//Inner loop 1 for (int c=rows-1; c>=r; c--) { System.out.print(" "); } // Prints star in decreasing order for (int k=1; k<=rows; k++) {//Inner loop 2 if (r == 1 || r == rows || k == 1 || k == rows) System.out.print("*"); else System.out.print(" "); } //Prints a newline System.out.println(); } } }
Output: Rows : 5 ***** * * * * * * *****
Method-2 : User Input Character
import java.util.Scanner; class pattern { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Rows : "); //Taking input from user int rows=scan.nextInt(); System.out.print("Symbol : "); //Taking random character input from user char s=scan.next().charAt(0); //Outer loop for (int r=1; r<=rows; r++) { //Inner loop 1 for (int c=rows-1; c>=r; c--) { System.out.print(" "); } // Prints star in decreasing order for (int k=1; k<=rows; k++) { //Inner loop 2 if (r == 1 || r == rows || k == 1 || k == rows) System.out.print(s); else System.out.print(" "); } //Prints a newline System.out.println(); } } }
Output: Rows : 5 Symbol : % %%%%% % % % % % % %%%%%
Explanation :
Let’s understand the program by going through the detailed explanation.
We have taken rows value as 5.
Iteration-1
r=1, goes into the first inner loop.
Now, c=rows-1
means 4. So prints 4 space as it will iterate upto c>=r
.
The second for loop prints five star(‘*’) for
.r == 1
*****
Iteration-2
r=2, goes into the first inner loop.
Now, c=rows-1
means 4. So prints 3 space as it will iterate upto c>=r
The second for loop prints one star(‘*’) for k ==1
, then three space then another star(‘*’) for k == rows
* *
Iteration-3
r=3, goes into the first inner loop.
Now, c=rows-1
means 4. So prints 2 space as it will iterate upto c>=r
The second for loop prints one star(‘*’) for k ==1
, then three space then another star(‘*’) for k == rows
* *
Iteration-4
r=4, goes into the first inner loop.
Now, c=rows-1
means 4. So prints 1 space as it will iterate upto c>=r
.
The second for loop prints one star(‘*’) for k ==1
, then three space then another star(‘*’) for k == rows
* *
Iteration-5
r=5, does not go into first inner loop c>rows-1(i.e. c = 5).
Now, c=rows-1
means 4. So prints no space as it will iterate upto c>=r
.
The second for loop prints five star(‘*’) for k == rows
*****
After this r >rows i.e. 6 so program will come out of the loop.
Now, after end of all iteration we will see the complete pattern is printed on the output screen like this.
***** * * * * * * *****
C Code:
#include <stdio.h> int main(int argc, char const *argv[]) { printf("Enter rows : "); int rows; //Taking row as input from user scanf("%d", &rows); //Outer loop for (int r = 1; r <= rows; r++) { //Inner loop for (int c = rows - 1; c >= r; c--) { printf(" "); } // Print stars in decreasing order for (int k = 1; k <= rows; k++) { //Inner loop 2 if (r == 1 || r == rows || k == 1 || k == rows) printf("*"); else printf(" "); } //To print a newline printf("\n"); } return 0; }
Output: Enter rows : 5 ***** * * * * * * *****
C++ Code:
{ cout << "Enter rows : "; int rows; //Taking row as input from user cin >> rows; //Outer loop for (int r = 1; r <= rows; r++) { //Inner loop 1 for (int c = rows - 1; c >= r; c--) { cout << " "; } // Print stars in decreasing order for (int k = 1; k <= rows; k++) { //Inner loop 2 if (r == 1 || r == rows || k == 1 || k == rows) cout << "*"; else cout << " "; } //To print a newline cout << endl; } return 0; }
Output: Enter rows : 5 ***** * * * * * * *****
Related Java Star Pattern Programs: