Diamond Number-Star Pattern (Sandwich) - Java Implementation
Understanding the Problem
The goal is to print a diamond-shaped pattern where numbers are printed in increasing order in the upper half, followed by a repeated maximum row, and then decreasing order in the lower half. Each number is followed by a star except for the last number in each row.
Pattern Explanation
The pattern consists of three parts: the upper half, the repeated maximum row, and the lower half.
1
2*2
3*3*3
4*4*4*4
4*4*4*4
3*3*3
2*2
1
Observations:
- The pattern consists of 7 rows.
- Numbers increment from 1 to 4 in the upper half.
- The maximum row (4) is repeated once.
- Each number is followed by a '*', except the last number in each row.
- The lower half mirrors the upper half, decrementing from 3 to 1.
Algorithm
The pattern follows these steps:
- Initialize a variable to determine the maximum number (N).
- Loop through rows from 1 to N to print the upper half of the diamond.
- For each row, loop through columns (up to the current row number) and print the current row number.
- Print the number followed by '*' except for the last number in the row.
- After completing the upper half, print the maximum row (N) again.
- Loop through rows from N down to 1 to print the lower half of the diamond.
- Repeat steps 3 and 4 for the lower half.
- Move to the next line after printing each row.
Method 1: Using Nested Loops
This method uses two loops to print the pattern.
public class DiamondNumberStarPatternSandwich {
public static void main(String[] args) {
int n = 4; // Maximum number for the diamond pattern
// Loop to print the upper half of the diamond
for (int i = 1; i <= n; i++) { // Loop for each row
for (int j = 1; j <= i; j++) { // Loop for each column in the current row
System.out.print(i); // Print the current row number
if (j < i) System.out.print("*"); // Print '*' if not the last number in the row
}
System.out.println(); // Move to the next line after each row
}
// Print the maximum row again
for (int j = 1; j <= n; j++) { // Loop for the maximum row
System.out.print(n); // Print the maximum number
if (j < n) System.out.print("*"); // Print '*' if not the last number in the row
}
System.out.println(); // Move to the next line after the maximum row
// Loop to print the lower half of the diamond
for (int i = n - 1; i >= 1; i--) { // Loop for each row
for (int j = 1; j <= i; j++) { // Loop for each column in the current row
System.out.print(i); // Print the current row number
if (j < i) System.out.print("*"); // Print '*' if not the last number in the row
}
System.out.println(); // Move to the next line after each row
}
}
}
Explanation:
- The first nested loop prints the upper half of the diamond, incrementing from 1 to n.
- The middle loop prints the maximum row (n) again to create the "sandwich" effect.
- The last nested loop prints the lower half of the diamond, decrementing from n-1 to 1.
- Each number is followed by a '*' except for the last number in each row.
Method 2: Using Methods
This approach modularizes the code by using methods for better readability and reusability.
public class DiamondPatternWithMethods {
public static void printRow(int num, int count) {
for (int j = 1; j <= count; j++) {
System.out.print(num);
if (j < count) System.out.print("*");
}
System.out.println();
}
public static void printDiamond(int n) {
// Upper half
for (int i = 1; i <= n; i++) {
printRow(i, i);
}
// Middle repeated row
printRow(n, n);
// Lower half
for (int i = n - 1; i >= 1; i--) {
printRow(i, i);
}
}
public static void main(String[] args) {
int n = 4;
printDiamond(n);
}
}
Advantages:
- More modular and readable code
- Easier to maintain and modify
- Reusable methods for printing rows
- Better separation of concerns
Output
Both methods will produce the same output:
1
2*2
3*3*3
4*4*4*4
4*4*4*4
3*3*3
2*2
1
Key Differences from C Implementation
While the logic remains the same, there are some Java-specific aspects to note:
- Java uses
System.out.print()andSystem.out.println()instead ofprintf() - Java code must be contained within a class
- Java methods are defined with access modifiers like
publicandstatic - Java requires a
mainmethod as the entry point - Java uses
println()for new lines instead of\n