Write a program in Java to allow the user to create a square matrix M[][] of order ‘N’.

The maximum value possible for ‘N’ is 10 >= N > 3.

Accept three different characters from the keyboard and fill the array according to the instructions given below:

- Fill the upper and lower elements formed by the intersection of the diagonals by character 1.
- Fill the left and right elements formed by the intersection of the diagonals by character 2.
- Fill both the diagonals by character 3.

Show the output in the format given below:

Example:

INPUT:

Enter size: 4

First character: *

Second character: ?

Third character: #

OUTPUT:

# * * # ? # # ? ? # # ? # * * #

```
import java.io.*;
class FillMatrix{
public static void main(String args[])throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Square matrix size: ");
int n = Integer.parseInt(br.readLine());
if(n > 10 || n < 3){
System.out.println("Size out of range!");
return;
}
char m[][] = new char[n][n];
System.out.print("Enter first character: ");
char ch1 = br.readLine().charAt(0);
System.out.print("Enter second character: ");
char ch2 = br.readLine().charAt(0);
System.out.print("Enter third character: ");
char ch3 = br.readLine().charAt(0);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(i + j <= n / 2 + 1){
m[i][j] = ch2;
m[j][i] = ch1;
}
else if(i + j >= n){
m[i][j] = ch1;
m[j][i] = ch2;
}
}
}
for(int i = 0, j = n - 1; i < n; i++, j--){
m[i][i] = ch3;
m[i][j] = ch3;
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
System.out.print(m[i][j] + "\t");
}
System.out.println();
}
}
}
```

## 2 replies on “Fill Square Matrix with Three Characters in Java”

Sir,a question is given which states that:

Given the boolean function F(A, B, C, D) = ∑(0, 2,3,6,8,10,11,14,15):

i) Reduce the above expression by using 4-variable Karnaugh Map, showing the various groups (i.e. octals, quads and pairs).

ii) Draw the logic gate diagram for the reduced expression. Assume that the variables and their complements are available as inputs.

Sir,how to draw the logic circuit and the 4-variable K-map of the above question ?

Visit this website to find out the solutions for K-Map Problems.