Java Program to Print Diamond Pattern
Java Programming Language Decision Making and Looping in java (Article) Decision Making and Looping in java (Program)
97
Given Input:
Enter number of rows: 5
Expected Output:
*
***
*****
*******
*********
*******
*****
***
*
Program:
import java.util.Scanner;
public class DiamondPattern {
public static void main(String[] args) {
int rows, i, j, space = 1;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of rows: ");
rows = scanner.nextInt();
scanner.close();
space = rows - 1;
for (j = 1; j <= rows; j++) {
for (i = 1; i <= space; i++) {
System.out.print(" ");
}
space--;
for (i = 1; i <= 2 * j - 1; i++) {
System.out.print("*");
}
System.out.println("");
}
space = 1;
for (j = 1; j <= rows - 1; j++) {
for (i = 1; i <= space; i++) {
System.out.print(" ");
}
space++;
for (i = 1; i <= 2 * (rows - j) - 1; i++) {
System.out.print("*");
}
System.out.println("");
}
}
}
Output:
*
***
*****
*******
*********
*******
*****
***
*
Explanation:
- Declare variables and get user input
Here, the integer variables rows
, i
, j
, and space
are declared. The Scanner
class is used to get user input for the number of rows. The nextInt()
method reads the integer input from the user and stores it in the rows
variable. The close()
method is called on the Scanner
object to free up system resources.
- Print the top half of the diamond pattern
This block of code uses two nested loops to print the top half of the diamond pattern. The outer loop (j
) iterates through the rows, while the inner loop (i
) prints the spaces and asterisks for each row. The number of spaces printed before the asterisks is determined by the space
variable, which starts at rows - 1
and is decremented by one after each row. The number of asterisks printed is determined by the formula 2 * j - 1
.
- Print the bottom half of the diamond pattern
This block of code is similar to the previous block, but it prints the bottom half of the diamond pattern. The outer loop (j
) iterates through the rows, while the inner loop (i
) prints the spaces and asterisks for each row. The number of spaces printed before the asterisks is determined by the space
variable, which starts at 1
and is incremented by one after each row. The number of asterisks printed is determined by the formula 2 * (rows - j) - 1
.
- Output The output of the program is a diamond pattern of asterisks (*) based on the number of rows entered by the user. The top half of the diamond is printed first, followed by the bottom half.
Another Way you can do
public class App {
public static void main(String[] args) throws Exception {
int i,j,k;
for(i=1; i<=9; i++){
for( j=9; j> i; j--){
System.out.print(" ");
}
for(k=1;k<=(2*i-1); k++)
{
System.out.print("*");
}
System.out.print("\n");
}
for(i=9; i >= 1; i--){
for( j=9; j> i; j--){
System.out.print(" ");
}
for(k=1;k<=(2*i-1); k++)
{
System.out.print("*");
}
System.out.print("\n");
}
}
}
This Java code prints a diamond pattern made of asterisks using loops. Here's how it works:
- Declare three integer variables
i
, j
, and k
to use as counters in the loops.
- The first loop uses
i
to iterate from 1 to 9 to print the top half of the diamond.
- The first inner loop uses
j
to print spaces before the asterisks, with the number of spaces decreasing from 9 to i
.
- The second inner loop uses
k
to print the asterisks, with the number of asterisks increasing from 1 to 2*i-1
.
- After printing the asterisks, the first outer loop goes to the next line using
System.out.print("\n")
.
- The second loop prints the bottom half of the diamond in a similar way, but with
i
decreasing from 9 to 1 instead.
- The program ends.
Here's what the code does in detail for the top half of the diamond:
- When
i
equals 1, the outer loop begins. The first inner loop prints eight spaces, and the second inner loop prints one asterisk, as k
equals 1. The first outer loop prints one line, then continues to the next iteration of the loop.
- When
i
equals 2, the first inner loop prints seven spaces, and the second inner loop prints three asterisks, as k
equals 1, 2, and 3. The second outer loop prints one line, then continues to the next iteration of the loop.
- This process repeats until
i
equals 9, at which point the first inner loop prints zero spaces, and the second inner loop prints 17 asterisks, as k
equals 1 to 17. The top half of the diamond is complete.
Then the program prints the bottom half of the diamond in a similar way, with i
decreasing from 9 to 1. Finally, the program ends.
This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.
* *** ***** ******* ********* ******* ***** *** *
Program:
import java.util.Scanner; public class DiamondPattern { public static void main(String[] args) { int rows, i, j, space = 1; Scanner scanner = new Scanner(System.in); System.out.print("Enter number of rows: "); rows = scanner.nextInt(); scanner.close(); space = rows - 1; for (j = 1; j <= rows; j++) { for (i = 1; i <= space; i++) { System.out.print(" "); } space--; for (i = 1; i <= 2 * j - 1; i++) { System.out.print("*"); } System.out.println(""); } space = 1; for (j = 1; j <= rows - 1; j++) { for (i = 1; i <= space; i++) { System.out.print(" "); } space++; for (i = 1; i <= 2 * (rows - j) - 1; i++) { System.out.print("*"); } System.out.println(""); } } }
Output:
* *** ***** ******* ********* ******* ***** *** *
Explanation:
- Declare variables and get user input
Here, the integer variables rows
, i
, j
, and space
are declared. The Scanner
class is used to get user input for the number of rows. The nextInt()
method reads the integer input from the user and stores it in the rows
variable. The close()
method is called on the Scanner
object to free up system resources.
- Print the top half of the diamond pattern
This block of code uses two nested loops to print the top half of the diamond pattern. The outer loop (j
) iterates through the rows, while the inner loop (i
) prints the spaces and asterisks for each row. The number of spaces printed before the asterisks is determined by the space
variable, which starts at rows - 1
and is decremented by one after each row. The number of asterisks printed is determined by the formula 2 * j - 1
.
- Print the bottom half of the diamond pattern
This block of code is similar to the previous block, but it prints the bottom half of the diamond pattern. The outer loop (j
) iterates through the rows, while the inner loop (i
) prints the spaces and asterisks for each row. The number of spaces printed before the asterisks is determined by the space
variable, which starts at 1
and is incremented by one after each row. The number of asterisks printed is determined by the formula 2 * (rows - j) - 1
.
- Output The output of the program is a diamond pattern of asterisks (*) based on the number of rows entered by the user. The top half of the diamond is printed first, followed by the bottom half.
Another Way you can do
public class App { public static void main(String[] args) throws Exception { int i,j,k; for(i=1; i<=9; i++){ for( j=9; j> i; j--){ System.out.print(" "); } for(k=1;k<=(2*i-1); k++) { System.out.print("*"); } System.out.print("\n"); } for(i=9; i >= 1; i--){ for( j=9; j> i; j--){ System.out.print(" "); } for(k=1;k<=(2*i-1); k++) { System.out.print("*"); } System.out.print("\n"); } } }
This Java code prints a diamond pattern made of asterisks using loops. Here's how it works:
- Declare three integer variables
i
,j
, andk
to use as counters in the loops. - The first loop uses
i
to iterate from 1 to 9 to print the top half of the diamond. - The first inner loop uses
j
to print spaces before the asterisks, with the number of spaces decreasing from 9 toi
. - The second inner loop uses
k
to print the asterisks, with the number of asterisks increasing from 1 to2*i-1
. - After printing the asterisks, the first outer loop goes to the next line using
System.out.print("\n")
. - The second loop prints the bottom half of the diamond in a similar way, but with
i
decreasing from 9 to 1 instead. - The program ends.
Here's what the code does in detail for the top half of the diamond:
- When
i
equals 1, the outer loop begins. The first inner loop prints eight spaces, and the second inner loop prints one asterisk, ask
equals 1. The first outer loop prints one line, then continues to the next iteration of the loop. - When
i
equals 2, the first inner loop prints seven spaces, and the second inner loop prints three asterisks, ask
equals 1, 2, and 3. The second outer loop prints one line, then continues to the next iteration of the loop. - This process repeats until
i
equals 9, at which point the first inner loop prints zero spaces, and the second inner loop prints 17 asterisks, ask
equals 1 to 17. The top half of the diamond is complete.
Then the program prints the bottom half of the diamond in a similar way, with i
decreasing from 9 to 1. Finally, the program ends.
This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.