**Question:**

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 instruction 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.

Output the result in format given below:

**Example 1**

ENTER SIZE : 4

INPUT : FIRST CHARACTER : ‘*’

SECOND CHARACTER : ‘?’

THIRD CHARACTER : ‘#’

OUTPUT :

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

**Example 2**

ENTER SIZE : 5

INPUT : FIRST CHARACTER : ‘$’

SECOND CHARACTER : ‘!’

THIRD CHARACTER : ‘@’

OUTPUT :

@ $ $ $ @ ! @ $ @ ! ! ! @ ! ! ! @ $ @ ! @ $ $ $ @

**Example 3**

ENTER SIZE : 65

OUTPUT : SIZE OUT OF RANGE

**Programming Code:**

/** * The class MatrixFill creates a matrix using 3 characters taken as inputs * Upper and lower elements formed by the intersection of the diagonals are filled by character 1. * Left and right elements formed by the intersection of the diagonals are filled by character 2. * Both the diagonals are filled by character 3. * @author : www.guideforschool.com * @Program Type : BlueJ Program - Java * @ISC Computer Science Practical Specimen Paper - Question 2 */ import java.util.*; class MatrixFill { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter size of the matrix : "); int n = sc.nextInt(); if(n<2 || n>10) System.out.println("Size out of Range"); else { char A[][]=new char[n][n]; System.out.print("Enter the 1st character : "); char c1 = sc.next().charAt(0); System.out.print("Enter the 2nd character : "); char c2 = sc.next().charAt(0); System.out.print("Enter the 3rd character : "); char c3 = sc.next().charAt(0); for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { if(i==j || (i+j)==(n-1)) A[i][j] = c3; // Filling the diagonals with 3rd character else A[i][j] = c2; // Filling all other positions with 2nd character } } for(int i=0; i<n/2; i++) { for(int j=i+1; j<n-1-i; j++) { A[i][j] = c1; // Filling the upper positions formed by intersection of diagonals A[n-1-i][j] = c1; // Filling the lower positions formed by intersection of diagonals } } // Printing the Matrix System.out.println("\nOutput : \n"); for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { System.out.print(A[i][j]+" "); } System.out.println(); } } } }

**Output:**

Enter size of the matrix : 7 Enter the 1st character : @ Enter the 2nd character : # Enter the 3rd character : % Output : % @ @ @ @ @ % # % @ @ @ % # # # % @ % # # # # # % # # # # # % @ % # # # % @ @ @ % # % @ @ @ @ @ %

vry vry hlpful…thnk u!!

thanks sir your programs are in easy language and easy to understand… the programs were very helpful

Sir why is this program not giving the required output,its not accepting c3

import java.io.*;

class x

{

public static void main (String args[])throws IOException

{

DataInputStream in=new DataInputStream(System.in);

int n;

System.out.println(“Enter the value of ‘n’ i.e the order”);

n=Integer.parseInt(in.readLine());

if(n>0&&n<10)

{

char a[][]=new char[n][n];

System.out.println("Enter the value of c1,c2,c3");

char c1,c2,c3;

c1=(char)(in.read());

c2=(char)(in.read());

c3=(char)(in.read());

for(int i=0;i<n;i++)

{

for(int j=0;j<n;j++)

{

if(i==j)

{

a[i][j]=c3;

}

if(i+j==n-1)

{

a[i][j]=c3;

}

else if((i!=j)&&(i+j!=n-1))

{

a[i][j]=c1;

}

}

}

for(int i=0;i<n;i++)

{

for(int j=0;j<n;j++)

{

if(i!=j&&(j==(n-1)||j==0))

{

a[i][j]=c2;

}}}

for(int i=0;i<n;i++)

{

for(int j=0;j<n;j++)

{

System.out.println(a[i][j] + " ");

}}

}

else

System.out.println("Out of bond");

}

}

yes because it is accepting the ‘enter key’ which you are pressing as an input for c3.

Try using :

c1 = in.readLine().charAt(0);

c2 = in.readLine().charAt(0);

c3 = in.readLine().charAt(0);

How can we solve 1st and 2nd sum in 2016 specimen please help me

Question 1 : http://www.guideforschool.com/2469166-java-program-to-check-for-evil-number-isc-specimen-2016-question-1/

Question 2 : http://www.guideforschool.com/2469183-java-program-to-arrange-words-in-ascending-order-of-their-potential-isc-specimen-2016-question-2/

Thnx for guidance