Java Program to Print Hollow Diamond in a Rectangle Star Pattern

Program to Print Hollow Diamond in a Rectangle Star Pattern

In this article we are going to see how to print the Hollow diamond in a Rectangle  star program.

Example-1

When row value=4
********
***   ***
**      **
*         *
*         *
**      **
***   ***
********
Example-2:

When row value=5
**********
****   ****
***      ***
**         **
*            *
*            *
**         **
***      ***
****   ****
**********

Now, let’s see program to  how print it.

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Approach:

Approach is very simple we just need to attempt it in a modular manner. Means here we can see 4 patters like

1. In upper part inverted right triangle and inverted mirrored right triangle.
2. In lower part right triangle and mirrored right triangle.
3. So, we will take two outer for loops to print the two p[arts.
4. And inside outer loops required for loops to be taken to print the column values i.e stars.

JAVA Code:

Method-1 : Static Star Character

import java.util.*;
public class Main
{
public static void main(String args[])
{   // taking variable for loop iteration and row .
int row,r,c,d;
//creating object
Scanner s = new Scanner(System.in);
// entering the number of row
System.out.print("Enter rows : ");
row = s.nextInt();
//printing row value (upper part)
for (r =1; r<=row ; r++)
{
//printing column value (upper part)
for (c= r; c <= row ; c++)
System.out.print("*");
for (c = 1; c <= r*2-2; c++)
System.out.print(" ");
for (c = r; c <= row ; c++)
System.out.print("*");
System.out.println();
}
//printing row value (lower part)
for (r = 1; r <= row ; r ++)
{
//printing column value (upper part)
for (c = 1; c <= r; c++)
System.out.print("*");
for (c = r *2-2; c < row *2-2; c++)
System.out.print(" ");
for (c = 1; c <= r; c ++)
System.out.print("*");
System.out.println();
}
}
}


Output :

Enter rows : 5
**********
****   ****
***      ***
**         **
*            *
*            *
**         **
***      ***
****   ****
**********

Method-2 : User Input Character

import java.util.*;
public class Main
{
public static void main(String args[])
{
// taking variable for loop iteration and row .
int row,r,c,d;
char q;
//creating object
Scanner s = new Scanner(System.in);
// entering the number of row
System.out.print("Enter rows : ");
row = s.nextInt();
// entering any random character
System.out.print("Enter character : ");
q = s.next().charAt(0);
//printing row value (upper part)
for (r =1; r<=row ; r++)
{
//printing column value (upper part)
for (c= r; c <= row ; c++)
System.out.print(q);
for (c = 1; c <= r*2-2; c++)
System.out.print(" ");
for (c = r; c <= row ; c++)
System.out.print(q);
System.out.println();
}
//printing row value (lower part)
for (r = 1; r <= row ; r ++)
{
//printing column value (upper part)
for (c = 1; c <= r; c++)
System.out.print(q);
for (c = r *2-2; c < row *2-2; c++)
System.out.print(" ");
for (c = 1; c <= r; c ++)
System.out.print(q);
System.out.println();
}
}
}


Output :

Enter rows : 5
Enter characters : *
**********
****   ****
***      ***
**         **
*            *
*            *
**         **
***      ***
****   ****
**********

Explanation:

First outer for loop :

Iteration-1

r=1 (row) (passes the first for loop condition) as it will iterate up to r<=row times.

Then first inner for loop will print star “*” 5 time , because loop will execute r to c<=row times.

Then second for loop will print star 1 to r *2-2 time i.e., 0 times space will be printed .

Then third inner for loop will print star “*” 5 time , because loop will execute 1 to c<=r times.

**********

Iteration-2

r=2 (row) (passes the first for loop condition) as it will iterate up to r<=row times.

Then first inner for loop will print star “*” 4 time , because loop will execute r to c<=row times.

Then second for loop will print star 1 to r *2-2 time i.e., 2 times space will be printed .

Then third inner for loop will print star “*” 4  time , because loop will execute 1 to c<=r times.

****  ****

Iteration-3

r=3 (row) (passes the first for loop condition) as it will iterate up to r<=row times.

Then first inner for loop will print star “*” 3 time , because loop will execute r to c<=row times.

Then second for loop will print star 1 to r *2-2 time i.e., 4 times space will be printed .

Then third inner for loop will print star “*” 3  time , because loop will execute 1 to c<=r times.

***    ***

Iteration-4

r=4 (row) (passes the first for loop condition) as it will iterate up to r<=row times.

Then first inner for loop will print star “*” 2 time , because loop will execute r to c<=row times.

Then second for loop will print star 1 to r *2-2 time i.e., 6 times space will be printed .

Then third inner for loop will print star “*” 2  time , because loop will execute 1 to c<=r times.

**      **

Iteration-5

r=5 (row) (passes the first for loop condition) as it will iterate up to r<=row times.

Then first inner for loop will print star “*” 1 time , because loop will execute r to c<=row times.

Then second for loop will print star 1 to r *2-2 time i.e., 8 times space will be printed .

Then third inner for loop will print star “*” 1  time , because loop will execute 1 to c<=r times.

*        *

Now when r=6 , first outer for loop condition will fail so no other loops will be executed.

Second outer for loop :

Iteration-1

r=1 (passes the first for loop condition) as it will iterate up to r<=row  times.

Then first inner for loop will print star “*” 1 time , because loop will execute  1 to c<=r times.

Then second for loop will print star r*2-2 to row*2-2  time i.e., 8 times space will be printed .

Then third inner for loop will print star “*” 1 time , because loop will execute  1 to c<=r times.

*        *

Iteration-2

r=2 (passes the first for loop condition) as it will iterate up to r<=row  times.

Then first inner for loop will print star “*” 2 time , because loop will execute  1 to c<=r times.

Then second for loop will print star r*2-2 to row*2-2  time i.e., 6 times space will be printed .

Then third inner for loop will print star “*” 2 time , because loop will execute  1 to c<=r times.

**      **

Iteration-3

r=3 (passes the first for loop condition) as it will iterate up to r<=row  times.

Then first inner for loop will print star “*” 3 time , because loop will execute  1 to c<=r times.

Then second for loop will print star r*2-2 to row*2-2  time i.e., 4 times space will be printed .

Then third inner for loop will print star “*” 3  time , because loop will execute  1 to c<=r times.

***    ***

Iteration-4

r=4 (passes the first for loop condition) as it will iterate up to r<=row  times.

Then first inner for loop will print star “*” 4 time , because loop will execute  1 to c<=r times.

Then second for loop will print star r*2-2 to row*2-2  time i.e., 2 times space will be printed .

Then third inner for loop will print star “*” 4  time , because loop will execute  1 to c<=r times.

****  ****

Iteration -5

r=5 (passes the first for loop condition) as it will iterate up to r<=row  times.

Then first inner for loop will print star “*” 5 time , because loop will execute  1 to c<=r times.

Then second for loop will print star r*2-2 to row*2-2  time i.e., 0 times space will be printed .

Then third inner for loop will print star “*” 5  time , because loop will execute  1 to c<=r times.

**********

Now when r=6, second outer  for loop condition will fail so no other loops will be executed.

At last , we will see a pattern like this ,

**********
****   ****
***      ***
**         **
*            *
*            *
**         **
***      ***
****   ****
**********

C Code:

#include <stdio.h>
int main() {
int r, row, c ,d;
printf("Enter rows: ");
scanf("%d", &row);
for (r =1; r<=row ; r++)
{
for (c= r; c <= row ; c++)
printf("*");
for (c = 1; c <= r*2-2; c++)
printf(" ");
for (c = r; c <= row ; c++)
printf("*");
printf("\n");
}

for (r = 1; r <= row ; r ++)
{
for (c = 1; c <= r; c++)
printf("*");
for (c = r *2-2; c < row *2-2; c++)
printf(" ");
for (c = 1; c <= r; c ++)
printf("*");
printf("\n");
}
return 0;
}


Output :

Enter rows : 5
**********
****   ****
***      ***
**         **
*            *
*            *
**         **
***      ***
****   ****
**********

C++ Code:

#include <iostream>
using namespace std;
int main()
{
int row, r , c ,d ;
cout << "Enter  rows: ";
cin >> row;
for (r =1; r<=row ; r++)
{
for (c= r; c <= row ; c++)
cout << "*";
for (c = 1; c <= r*2-2; c++)
cout << " ";
for (c = r; c <= row ; c++)
cout << "*";
cout << "\n";
}

for (r = 1; r <= row ; r ++)
{
for (c = 1; c <= r; c++)
cout << "*";
for (c = r *2-2; c < row *2-2; c++)
cout << " ";
for (c = 1; c <= r; c ++)
cout << "*";
cout << "\n";
}
return 0;

}


Output :

Enter rows : 5
**********
****   ****
***      ***
**         **
*            *
*            *
**         **
***      ***
****   ****
**********

Related Java Star Pattern Programs: