Java Program to Print Half Diamond Star Pattern
In this tutorial, we will see how to print the half diamond star pattern in Java. First, we will ask the user to initialize the number of rows. Then, we will use loops to print the half diamond star pattern. But before moving further, if you are not familiar with the concept of the loops in java, then do check the article on Loops in Java.
Input: Enter the number of rows: 7
Output: The pattern is:
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
This can be done by using the following methods:
Approach 1: Using a For Loop
Approach 2: Using a While loop
Approach 3: Using a do-while loop
Let us look at each of these approaches for a better understanding
Program 1: Print Half Diamond Star Pattern
In this program, we will see how to print the half diamond star pattern in java using a for loop.
Algorithm:
-
Start
-
Create an instance of the Scanner class.
-
Declare a variable to store the number of rows.
-
Ask the user to initialize the variable.
-
Use a for loop to print the pattern.
-
The outer loops iterate through rows and inner loops iterate through columns
-
The first outer for loop will print the half of the half diamond pattern, the second outer for loop will print the remaining half of the half diamond pattern.
-
The first outer for loop iterates until the condition i<=n is false, the inner loop will display the character until the condition j<i. The first outer loop will print this pattern.
-
The second outer for loop iterates until the condition i>0 is false, the inner loop will display character until j<=i, the second outer for loop will print this pattern.
-
Display the result.
-
Stop
The below example illustrates the implementation of the above algorithm.
//Java Program to Print the Half Diamond Star Pattern
import java.util.*;
public class Main
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int n=sc.nextInt();
int i, j;
for (i = 0; i < n; i++)
{
for (j = 0; j < i + 1; j++)
System.out.print("*");
System.out.print("\n");
}
for (i = 1; i < n; i++)
{
for (j = i; j < n; j++)
System.out.print("*");
System.out.print("\n");
}
}
}
Enter the number of rows: 4
*
**
***
****
***
**
*
Program 2: Print Half Diamond Star Pattern
In this program, we will see how to print the half diamond star pattern in java using a while loop.
Algorithm:
-
Start
-
Create an instance of the Scanner class.
-
Declare a variable to store the number of rows.
-
Ask the user to initialize the variable.
-
Use a while loop to print the pattern.
-
The first while loop iterates until i<=n.
-
Initialize the inner loop variable to 1. The inner while loop will iterate till j++<=n. If the condition satisfies it will print the pattern.
-
The second while loop iterates until i>0.
-
Initialize the inner loop variable to 1. The inner while loop will iterate till j++<=i. If the condition satisfies it will print the pattern.
-
Display the result.
-
Stop
The below example illustrates the implementation of the above algorithm.
//Java Program to Print the Half Diamond Star Pattern
import java.util.*;
public class Main
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int n=sc.nextInt();
int i=1,j;
while(i<=n)
{
j=1;
while(j++<=i)
{
System.out.print("*");
}
System.out.println();
i++;
}
i=n-1;
while(i>0)
{
j=1;
while(j++<=i)
{
System.out.print("*");
}
System.out.println();
i--;
}
}
}
Enter the number of rows: 7
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
Program 3: Print Half Diamond Star Pattern
In this program, we will see how to print the half diamond star pattern in java using a do-while loop.
Algorithm:
-
Start
-
Create an instance of the Scanner class.
-
Declare a variable to store the number of rows.
-
Ask the user to initialize the variable.
-
Use two do-while loops to print the pattern.
-
The first do-while loop iterates untill ++i<=n.
-
Initialize the inner loop variable to 1. The inner do-while loop will iterate till ++j<=i. If the condition satisfies it will print the pattern.
-
The second do-while loop iterates until --i>0.
-
Initialize the inner loop variable to 1. The inner do-while loop will iterate till ++j<=i. If the condition satisfies it will print the pattern.
-
Display the result.
-
Stop
The below example illustrates the implementation of the above algorithm.
//Java Program to Print the Half Diamond Star Pattern
import java.util.*;
public class Main
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int n=sc.nextInt();
int i=1;
int j;
do
{
j=1;
do
{
System.out.print("*");
}while(++j<=i);
System.out.println();
} while(++i<=n);
i=n-1;
do
{
j=1;
do
{
System.out.print("*");
}while(++j<=i);
System.out.println();
} while(--i>0);
}
}
Enter the number of rows: 7
*
**
***
****
*****
******
*******
******
*****
****
***
**
*