KnowledgeBoat Logo

Computer Science

Given a square matrix M[][] of order 'n'. The maximum value possible for 'n' is 10. Accept three different characters from the keyboard and fill the array according to the output shown in the examples. If the value of n exceeds 10 then an appropriate message should be displayed.

Example 1

Enter Size: 4

Input:
First Character '*'
Second Character '?'
Third Character '#'

Output:
# * * #
? # # ?
? # # ?
# * * #

Example 2

Enter Size: 5

Input:
First Character 'SecondCharacter!ThirdCharacter@Output:@'
Second Character '!'
Third Character '@'

Output:
@ @
! @ @!!!@!!!@@ ! ! ! @ ! ! ! @ @ !
@ $ @

Example 3

Enter Size: 65

Output:
Size out of Range

Java

Java Arrays

14 Likes

Answer

import java.util.Scanner;

public class KboatCharMatrix
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter size: ");
        int n = in.nextInt();
        
        if (n < 1 || n > 10) {
            System.out.println("Size out of Range");
            return;
        }
        
        System.out.print("First Character: ");
        char ch1 = in.next().charAt(0);
        System.out.print("Second Character: ");
        char ch2 = in.next().charAt(0);
        System.out.print("Third Character: ");
        char ch3 = in.next().charAt(0);
        
        char m[][] = new char[n][n];
        char x = ch2;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i == j || (i + j) == n - 1) {
                    m[i][j] = ch3;
                }
                else if (i == 0 || i == n - 1) {
                    m[i][j] = ch1;
                }
                else if (j == 0 || j == n - 1 || i % 2 == 0) {
                    m[i][j] = ch2;
                }
                else {
                    m[i][j] = ch1;
                }
            }
        }
        
        System.out.println("OUTPUT:");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(m[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

Output

BlueJ output of Given a square matrix M[][] of order 'n'. The maximum value possible for 'n' is 10. Accept three different characters from the keyboard and fill the array according to the output shown in the examples. If the value of n exceeds 10 then an appropriate message should be displayed. Example 1 Enter Size: 4 Input: First Character '*' Second Character '?' Third Character '#' Output: # * * # ? # # ? ? # # ? # * * # Example 2 Enter Size: 5 Input: First Character ' @ ! @ @ ! @ $ @ Example 3 Enter Size: 65 Output: Size out of RangeBlueJ output of Given a square matrix M[][] of order 'n'. The maximum value possible for 'n' is 10. Accept three different characters from the keyboard and fill the array according to the output shown in the examples. If the value of n exceeds 10 then an appropriate message should be displayed. Example 1 Enter Size: 4 Input: First Character '*' Second Character '?' Third Character '#' Output: # * * # ? # # ? ? # # ? # * * # Example 2 Enter Size: 5 Input: First Character ' @ ! @ @ ! @ $ @ Example 3 Enter Size: 65 Output: Size out of RangeBlueJ output of Given a square matrix M[][] of order 'n'. The maximum value possible for 'n' is 10. Accept three different characters from the keyboard and fill the array according to the output shown in the examples. If the value of n exceeds 10 then an appropriate message should be displayed. Example 1 Enter Size: 4 Input: First Character '*' Second Character '?' Third Character '#' Output: # * * # ? # # ? ? # # ? # * * # Example 2 Enter Size: 5 Input: First Character ' @ ! @ @ ! @ $ @ Example 3 Enter Size: 65 Output: Size out of Range

Answered By

5 Likes


Related Questions