Diamond Pattern using Recursion
class DiamondRecursion {
static void column(int c, int a) {
System.out.print(a + " ");
if (a < c) {
column(c, a+1);
System.out.print(a + " ");
}
}
static void row(int r, int a) {
for (int i=0; i<r-a; i++)
System.out.print(" ");
column(a, 1); System.out.println();
if (a < r) {
row(r, a+1);
for (int i=0; i<r-a; i++)
System.out.print(" ");
column(a, 1); System.out.println();
}
}
public static void main(String args[]) {
row(new java.util.Scanner(System.in).nextInt(), 1);
}
}
Diamond Pattern using Iteration
class DiamondIteration {
static void column(int N, int c) {
for (int i = 0; i < N-c; i++)
System.out.print(" ");
for (int i = 1; i < c; i++)
System.out.print(i + " ");
for (int i=c; i>0; i--)
System.out.print(i + " ");
System.out.println();
}
static void row(int N) {
for (int i=1; i<N; i++)
column(N, i);
for (int i=N; i>0; i--)
column(N, i);
}
public static void main(String args[]) {
row(new java.util.Scanner(System.in).nextInt());
}
}
Input
5
Output
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1