## Interview Question

Students**Country:**India

```
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter value of n: ");
int n = sc.nextInt();
int row = 0;
int column = 0;
if(n%2 == 0) {
row = n+1;
column = n+1;
}
else {
row = n+1;
column = n;
}
int[][] matrix = new int[row][column];
int count=1;
int half = n/2;
for(int i=0; i<row; i++) {
for(int j=0; j<column; j++) {
if((i > 0 && i<row) && j == half) {
matrix[i][j] = count;
count++;
}
else {
matrix[i][j] = n;
}
}
}
for(int i=0; i<row; i++) {
for(int j=0; j<column; j++) {
System.out.print(matrix[i][j]+ " ");
}
System.out.println();
}
sc.close();
```

}

This should work for 2- and 3-digit numbers as well.

```
public static void nlines(int n) {
if (n < 2) {
System.out.println(n);
} else {
IntStream.concat(IntStream.of(n), IntStream.rangeClosed(1, n))
.mapToObj(i ->
String.format(
"%" + n/2 + "s" +
"%0" + Integer.toString(n).length() + "d" +
"%" + n/2 + "s",
"", i, "")
.replace(" ", Integer.toString(n))
)
.forEach(System.out::println);
}
}
```

This should work for 2- and 3-digit numbers as well.

public static void nlines(int n) {

if (n < 2) {

System.out.println(n);

} else {

IntStream.concat(IntStream.of(n), IntStream.rangeClosed(1, n))

.mapToObj(i ->

String.format(

"%" + n/2 + "s" +

"%0" + Integer.toString(n).length() + "d" +

"%" + n/2 + "s",

"", i, "")

.replace(" ", Integer.toString(n))

)

.forEach(System.out::println);

}

}

- Kushal February 10, 2018