-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixFilling.java
More file actions
46 lines (43 loc) · 1.41 KB
/
Copy pathMatrixFilling.java
File metadata and controls
46 lines (43 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package basic;
import java.util.Scanner;
class MatrixFilling {
public static void main(String[] args) {
Scanner ob = new Scanner(System.in);
System.out.println("ENTER SIZE OF MATRIX:");
int n = ob.nextInt();
if (n < 3 || n > 10) {
System.out.println("Invalid Input,Size out of range");
System.exit(0);
}
int M[][] = new int[n][n];
System.out.println("Enter first character :");
char ch1 = ob.next().charAt(0);
System.out.println("Enter second character :");
char ch2 = ob.next().charAt(0);
System.out.println("Enter third character :");
char ch3 = ob.next().charAt(0);
for (int i = 0; i < n; i++) //Logic for Matrix Filling
{
for (int j = 0; j < n; j++) {
if (i == j || i + j == n - 1) {
M[i][j] = ch3;
} else {
M[i][j] = ch2;
}
}
}
for (int i = 0; i < n / 2; i++) {
for (int j = i + 1; j < n - 1 - i; j++) {
M[i][j] = ch1;
M[n - 1 - i][j] = ch1;
}
}
System.out.println("Matrix after filling");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print((char) M[i][j] + " ");
}
System.out.println("");
}
}
}