Hollow Diamond Pattern in Java

Hollow diamond pattern in java for beginners in a programming language. A pattern is someone or something that is used as a template to create a copy, a design, or an expected activity.

For Loop Definition in Java

In Java, the “for” loop is an entry-controlled loop that allows a user to iteratively run a block of statement(s) for a set number of times.

The test-condition specified inside the “for” loop determines the number of iterations. 

The Java “for” loop is one of the most simple Java loops to grasp. 

A “For” Loop is used to repeat a block of code for a certain number of times. 

A for loop is a control flow statement that allows code to be executed repeatedly by defining iteration. 

When the number of iterations is known before entering the loop, for loops are commonly employed. 

While loops can be thought of as short hands for for loops, which increment and test a loop variable, for loops can be thought of as short hands for while loops.

If Else Statement in Java

The simplest basic decision-making statement in Java is the if statement.

It is used to determine if a statement or a block of statements will be executed or not, that is if a condition is true, a block of statements will be executed; otherwise, it will not. 

The if else statement is a control flow statement that allows you to only run a block of code if a specific condition is met.

Hollow Diamond Pattern in Java

import java.util.Scanner;
class Main{
public static void main(String args[]){
int i,j;
Scanner scan=new Scanner(System.in);
System.out.print("Enter Number of Rows");
int rows=scan.nextInt();
for(i=1; i<=rows; i++){
for(j=rows; j>i; j--){
System.out.print(" ");
}
System.out.print("*"); 
for(j=1; j<(i-1)*2; j++){
System.out.print(" ");
}
if(i==1){
System.out.print("\n");
}
else{
System.out.print("*\n");
}
}
for(i=rows-1; i>=1; i--){
for(j=rows; j>i; j--){
System.out.print(" ");
}
System.out.print("*");
for(j=1; j<(i-1)*2; j++){
System.out.print(" ");
}
if(i==1){
System.out.print("\n");
}
else{
System.out.print("*\n");
}
}
}
}

Enter Number of Rows5
         *
      *   *
    *       *
  *          *
 *             *
  *           *
   *       *
    *    *
       *