## Interview Question for Students

• 0

Country: India

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````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();
}``````

Comment hidden because of low score. Click to expand.
0

import statements

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````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();``````

}

Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
}
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void printPattern(int n) {

int col = n;
if(n%2 == 0) {
col++;
}

for(int i=0; i<=n;i++ ) {
for (int j =0; j<col; j++) {
if(i>0 && j == (col/2)) {
System.out.print(i);
}else {
System.out.print(n);
}
}
System.out.println();
}
}

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.