print the pattern 1 121 12321 1234321 12321 121 1
in Java.... DIAMOND PATTERN
Answers
Answer: The required Java program is :-
public class Main {
public static void main(String[] args) {
int i,j;
for(i=1;i<=4;i++) {
for (j=1;j<=4-i;j++) {
System.out.print(" ");
}
for(j=1;j<=i;j++) {
System.out.print(j);
}
for(j=i-1;j>=1;j--) {
System.out.print(j);
}
System.out.println();
}
for(i=3;i>=1;i--) {
for(j=1;j<=4-i;j++) {
System.out.print(" ");
}
for(j=1;j<=i;j++) {
System.out.print(j);
}
for(j=i-1;j>=1;j--) {
System.out.print(j);
}
System.out.println();
}
}
}
Note :-
The above program is saved in a file named Main.java
Also, try-catch block can be used to avoid errors.
Answer:
Method1:
import java.util.Scanner;
public class halfDiamond2 {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int n = s.nextInt();
System.out.print("*");
System.out.println("");
//upper Triangle
for(int upperRow=1; upperRow<=n; upperRow++)
{
System.out.print("*"); //printing of first columns as stars
//now print increasing numbers
for(int incNum=1; incNum<=upperRow;incNum++)
{
System.out.print(incNum);
}
//now print decreasing numbers
for(int decNum=upperRow-1; decNum>=1; decNum--)
{
System.out.print(decNum);
}
System.out.print("*");
System.out.println("");
}
//lower Triangle
for(int lowerRow=n-1; lowerRow>=1; lowerRow--)
{
System.out.print("*");
//now print increasing numbers
for(int incNum=1; incNum<=lowerRow;incNum++)
{
System.out.print(incNum);
}
//printing decreasing numbers
for(int decNum = lowerRow-1; decNum>=1; decNum--)
{
System.out.print(decNum);
}
System.out.print("*");
System.out.println();
}
System.out.print("*");
}
}
Method2:
import java.util.Scanner;
public class halfDiamond {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int n = s.nextInt();
if(n==0)
{
System.out.print("*\n");
System.out.print("*");
}
else {
int upperRows = n+1;
//upper Triangle
for(int upperRow=1; upperRow<=upperRows; upperRow++)
{
System.out.print("*"); //printing of first columns as stars
//now print increasing numbers
for(int incNum=1; incNum<=upperRow-1;incNum++)
{
System.out.print(incNum);
}
//now print decreasing numbers
int toPrint = upperRow-2;
for(int decNum=1; decNum<=upperRow-1; decNum++)
{
int x = toPrint;
if(x==0)
{
System.out.print("*");
}
else {
System.out.print(x);
}
toPrint--;
}
System.out.println();
}
//lower Triangle
int lowerRows = ((2*n)+1)/2;
for(int lowerRow=lowerRows; lowerRow>=1; lowerRow--)
{
System.out.print("*");
//now print increasing numbers
for(int incNum=1; incNum<=lowerRow-1;incNum++)
{
System.out.print(incNum);
}
//printing decreasing numbers
int toPrint = lowerRow -2;
for(int decNum = 1; decNum<=lowerRow-1; decNum++)
{
int x = toPrint;
if(x==0)
{
System.out.print("*");
}
else {
System.out.print(x);
}
toPrint--;
}
System.out.println();
}
System.out.println(); // new line for every row
}
}
Explanation:
Method1) We print the "*" in first and last row, and then handle all elements be "*", increasing number, decreasing number using for loops.
Method2) This method is more relatable while trying to understand. Here, we handle the test case for user input if 0. But for every other case , for loops print "*", increasing Numbers, and decreasing Numbers.