package PatternsQuestions;
import java.util.Scanner;
public class ques33 {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int var = 0; // 0 in the middle
int nsp = n-1;
int nst = 1;
int var2 = n; // the numbers in rows
//working
for(int row = 1; row <= n; row++) {
for(int cst = 1; cst <= nst; cst++) {
if(row==1) {
for(int csp = 1; csp <= nsp; csp++) {
System.out.print(" " + "\t");
}
System.out.print(var+ "\t");
} else {
//defining the initial number of spaces
for(int csp = 1; csp <= nsp; csp++) {
System.out.print(" "+ "\t");
}
// defining the part1 on the pattern
for(int part1 = 1; part1<= row-1; part1++) {
System.out.print(var2+ "\t");
var2++;
}
//the middle zero
System.out.print(var+ "\t");
//the second part
for(int part1 = 1; part1<= row-1; part1++) {
System.out.print(var2-1+ "\t");
var2--;
}
}
}
//preparation
System.out.println();
nsp--;
var2--;
}
scn.close();
}
}
My questions are:
-
Can this be done in an easier way?
-
Is there a ‘right way’ to write a code? Like is the easier way always the right way to write the code. Each answer can be approached differently.