0% found this document useful (0 votes)
56 views2 pages

2D Array - Border of Matrix 100 Elements Program Java

The document describes a Java program that takes in a 10x10 matrix from the user and prints out the original matrix and the border of the matrix. The program defines a 2D array to store the matrix elements, takes in the elements from the user in a nested for loop, and then prints out the original matrix. It then prints the border matrix by printing elements only if they are on the border (first column, last column, first row, last row) and spaces otherwise.

Uploaded by

SAMRIT MUKHERJEE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views2 pages

2D Array - Border of Matrix 100 Elements Program Java

The document describes a Java program that takes in a 10x10 matrix from the user and prints out the original matrix and the border of the matrix. The program defines a 2D array to store the matrix elements, takes in the elements from the user in a nested for loop, and then prints out the original matrix. It then prints the border matrix by printing elements only if they are on the border (first column, last column, first row, last row) and spaces otherwise.

Uploaded by

SAMRIT MUKHERJEE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Enter no.

of elemnts for a square matrix


10
Enter the elemnts
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
ORIGINAL MATRIX
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 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
BORDER OF THE MATRIX
1 2 3 4 5 6 7 8 9 10
11 20
21 30
31 40
41 50
51 60
61 70
71 80
81 90
91 92 93 94 95 96 97 98 99 100
Program:-

import java.util.*;
class exm5
{
public static void main()
{
Scanner sc = new Scanner (System.in);
System.out.println ("Enter no. of elemnts for a square matrix");
int m=sc.nextInt();
int a[][] = new int [m][m];
int b[][] = new int [m][m];
int i,j;
int sd1=0,sd2=0;
System.out.println ("Enter the elemnts ");
for (i=0;i<m;i++)
{
for (j=0;j<m;j++)
{
a[i][j] =sc.nextInt();
}
}

//original matrix
System.out.println ("ORIGINAL MATRIX");
for (i=0;i<m;i++)
{
for (j=0;j<m;j++)
{
System.out.print(a[i][j]+ " ");
}
System.out.println();
}

// border matrix
System.out.println ("BORDER OF THE MATRIX");
for (i=0;i<m;i++)
{
for (j=0;j<m;j++)
{
if (i==0)
{
System.out.print(a[i][j]+ " ");

}
else if (j==0)
{
System.out.print(a[i][j]+ " ");
}
else if (i==m-1)
{
System.out.print(a[i][j]+ " ");
}
else if (j==m-1)
{
System.out.print(a[i][j]+ " ");
}
else
{
System.out.print(" ");
}
}
System.out.println ();
}
}
}

You might also like