Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Double Dimensional Array

1. WAP in Java to store the numbers in a 3 by 3 Matrix in a Double Dimensional Array.


Find the sum of all the numbers of the Matrix and display the sum of all elements.
Sample Input:
1 5 9
7 3 1
8 6 2
Sample Output:
Sum of elements = 42
import java.util.*;
class Matrix1
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int A[][]=new int[3][3];
int i,j,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
A[i][j]=sc.nextInt();
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=sum+A[i][j];
}
}
System.out.println("sum of matrix = "+sum);
}
}
class Matrix1
{
public static void main(int A[][])
{
int i,j,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=sum+A[i][j];
}
}
System.out.println("sum of matrix = "+sum);
}
}
2. WAP in Java to store the numbers in a 3x3 Matrix in Double Dimensional Array.
Find the sum of the number of each row.
Sample Input:
1 5 9
7 3 1
8 6 2
Sample Output:
Sum of Row 1 = 15
Sum of Row 2 = 11
Sum of Row 3 = 16
class Matrix2
{
public static void main(int A[][])
{
int i,j,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=sum+A[i][j];
}
System.out.println("sum of Row "+(i+1)+" = "+sum);
sum=0;
}

}
}
3. WAP in Java to store the numbers in a 3x3 Matrix in Double Dimensional Array.
Find the sum of the number of each columns.
Sample Input:
1 5 9
7 3 1
8 6 2
Sample Output:
Sum of Column 1 = 16
Sum of Column 2 = 14
Sum of Column 3 = 12
class Matrix3
{
public static void main(int A[][])
{
int i,j,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=sum+A[i][j];
}
System.out.println("sum of Column "+(i+1)+" = "+sum);
sum=0;
}

}
}
4. WAP in Java to store the numbers in a 3x3 Matrix in Double Dimensional Array.
Find the sum of the numbers in the Left diagonal.
Sample Input:
1 5 9
7 3 1
8 6 2
Sample Output:
Sum Left diagonal = 6
class Matrix4
{
public static void main(int A[][])
{
int i,j,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
sum=sum+A[i][j];
}
}
System.out.println("sum of left diagonal = "+sum);
}
}
5. WAP in Java to store the numbers in a 3x3 Matrix in Double Dimensional Array.
Find the sum of the numbers in the Right diagonal.
Sample Input:
1 5 9
7 3 1
8 6 2
Sample Output:
Sum of Right diagonal = 20
class Matrix5
{
public static void main(int A[][])
{
int i,j,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i+j==2)
sum=sum+A[i][j];
}
}
System.out.println("sum of right diagonal = "+sum);
}
}
6. WAP in Java to store the numbers in 3x3 matrix in a Double Dimensional Array.
Display the Highest and the Lowest number among the stored number in the matrix.
Sample Input:
1 5 9
7 3 4
8 6 2
Sample Output:
Highest number = 9
Lowest number = 1
class Matrix3
{
public static void main(int A[][])
{
int i,j,High=A[0][0],Low=A[0][0];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(A[i][j]>High)
High=A[i][j];
else if(A[i][j]<Low)
Low=A[i][j];
}
}
System.out.println("Smallest number = "+Low);
System.out.println("Greatest number = "+High);
}
}
7. WAP in Java to store the numbers in a 3x3 matrix in Double Dimensional Array.
Display the elements of the matrix by replacing each element of the left diagonal with the zero.
Sample Input: Sample Output:
1 5 9 0 5 9
7 3 1 7 0 1
8 6 2 8 6 0
class Matrix3
{
public static void main(int A[][])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
System.out.print("0");
else
System.out.print(A[i][j]);
}
System.out.println();
}
}
}
8. WAP in java to store the numbers in a 3x3 matrix in Double Dimensional Array.
Display the sum of the border elements of the matrix.
Sample Input:
1 5 9
7 3 4
8 6 2
Sample Output:
Sum of Border elements = 42
class Matrix8
{
public static void main(int A[][])
{
int i,j,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==0||j==0||i==2||j==2)
sum=sum+A[i][j];
}
}
System.out.println("sum of border elements = "+sum);
}
}

9. WAP to accept value into 3x3 array and check if it is a Special array.
(An Array is special array if the sum of the even elements = sum of odd elements)
Sample Input:
4 5 6
5 3 2
4 2 5
Sample Output:
Sum of even elements = 4+6+2+4+2=18
Sum of odd elements = 5+5+3+5=18
class Matrix9
{
public static void main(int A[][])
{
int i,j,Esum=0,Osum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(A[i][j]%2==0)
Esum=Esum+A[i][j];
else
Osum=Osum+A[i][j];
}
}
if(Esum==Osum)
System.out.println("It is Special Array");
else
System.out.println("It is not Special Array");
}
}

10. WAP to create a Double Dimension character Array as:


char ch[][]=new ch[3][3].

The program store nine different uppercase letters in the double dimensional array. Display the letter which are
vowels.
Sample Input:
P E R
W D A
U M K
Sample Output: E,A,U
class Matrix10
{
public static void main(char A[][])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(A[i][j]=='A'||A[i][j]=='E'||A[i][j]=='I'||A[i][j]=='O'||A[i][j]=='U')
System.out.print(A[i][j]+" ");
}
}
}
}

11. WAP in Java to create a 4x4 matrix. Now swap the elements of 1st row and 4th row. (i.e. interchange the
elements of the 1st row with the 4th row) . Display the result.
Sample Input: Sample Output:
12 4 1 9 2 41 7 9
3 2 5 8 3 2 5 8
1 3 5 7 1 3 5 7
2 41 7 9 12 4 1 9
class Matrix10
{
public static void main(int A[][])
{
int i,j,temp;
for(i=0;i<3;i++)
{
temp=A[0][i];
A[0][i]=A[2][i];
A[2][i]=temp;
}

System.out.println("Matrix are : ");


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(A[i][j]);
}
System.out.println();
}
}
}
13. WAP in Java to store the numbers in a 3x3 Matrix in Double Dimensional Array.
Print the Left diagonal elements in matrix format.
Sample Input: Sample Output:
1 5 9 1
7 3 1 3
8 6 2 2
class Matrix12
{
public static void main(int A[][])
{
int i,j;
System.out.println("Left Diagonal Matrix are : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
System.out.print(A[i][j]);
else
System.out.print(" ");
}
System.out.println();
}
}
}
14. WAP in Java to store the numbers in a 3x3 Matrix in Double Dimensional Array.
Print the Right diagonal elements in matrix format.
Sample Input: Sample Output:
1 5 9 9
7 3 1 3
8 6 2 8
class Matrix12
{
public static void main(int A[][])
{
int i,j;
System.out.println("Right Diagonal Matrix are : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i+j==2)
System.out.print(A[i][j]);
else
System.out.print(" ");
}
System.out.println();
}
}
}
15. WAP in java to store he numbers in a 4x4 matrix in a Double Dimensional Array.
Display only the elements available above the left diagonal of the matrix.
Sample Input: Sample Output:
2 4 1 9 4 1 9
3 2 5 8 5 8
1 3 5 7 7
2 4 7 9
class Matrix12
{
public static void main(int A[][])
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(j>i)
System.out.print(A[i][j]);
else
System.out.print(" ");
}
System.out.println();
}
}
}
16. WAP in Java to store the numbers in 3x3 matrix in Double Dimensional Array. Display the sum of the
elements that are above and below the left diagonal matrix.
Sample Input:
1 2 3
4 5 6
7 8 9
Sample Output:
Sum of the elements that are above and below the left diagonal = 30
class Matrix12
{
public static void main(int A[][])
{
int i,j,sum=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i!=j)
sum=sum+A[i][j];
}
}
System.out.println("Sum of the elements that are above and below the left diagonal = "+sum);
}
}

18. WAP in Java to store numbers in 4x4 matrix. Display the numbers which are below left diagonal as shown
below.
Sample Input: Sample Output:
1 2 3 4
5 6 7 8 5
3 4 5 8 3 4
9 3 2 1 9 3 2
class Matrix12
{
public static void main(int A[][])
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(j>i)
System.out.print(A[i][j]);
else
System.out.print(" ");
}
System.out.println();
}
}
}
19. WAP in Java to store the numbers in 4x4 matrix. Display the numbers which are above the right diagonal as
shown below.
Sample Input: Sample Output:
1 2 3 4 1 2 3
5 6 7 8 5 6
3 4 5 8 3
9 3 2 1
class Matrix12
{
public static void main(int A[][])
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if((i+j)<3)
System.out.print(A[i][j]);
else
System.out.print(" ");
}
System.out.println();
}
}
}

20. WAP in Java to store numbers in 4x4 matrix. Display the numbers which are below the right diagonal as
shown.
Sample Input: Sample Output:
1 2 3 4
5 6 7 9 9
3 4 5 8 5 8
9 3 2 1 3 2 1
class Matrix12
{
public static void main(int A[][])
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if((i+j)>3)
System.out.print(A[i][j]);
else
System.out.print(" ");
}
System.out.println();
}
}
}

21. WAP in Java to create a 3x3 matrix and store the different numbers. Display the highest value of each row.
Sample Input: Sample Output:
1 12 3
4 50 6
7 8 1
Sample Output:
Highest value of row 1 = 12
Highest value of row 1 = 50
Highest value of row 1 = 8
class Matrix12
{
public static void main(int A[][])
{
int i,j,High;
for(i=0;i<4;i++)
{
High=A[i][0];
for(j=0;j<4;j++)
{
if(A[i][j]>High)
High=A[i][j];
}
System.out.println("Highest value of row "+(i+1)+"="+High);
}
}
}
22.WAP in java to create a 3x3 Square Matrix and store elements in it. The programmer wants to check whether
the Matrix is Symmetric or not.
(A square matrix is said to be Symmetric , if the elements of the ith row and jth columns is equal to the element
of the jth row and ith columns.
Sample Input:
1 2 3
2 4 5
3 5 6
Sample Output: It is a Symmetric matrix.
class Matrix12
{
public static void main(int A[][])
{
int i,j,c=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(A[i][j]!=A[j][i])
{
c++;
break;
}
}
}
if(c==0)
System.out.println("It is Symmetric Matrix ");
else
System.out.println("Not");
}
}
23. Transpose a matrix mean to change row elements into column and the column elements into rows.
WAP to store the number in a 4x4 matrix in Double Dimensional Array and display the transpose of
the matrix.
Sample Input: Sample Output:
22 14 23 25 22 81 58 55
81 26 31 10 14 26 64 33
58 64 17 12 23 31 17 26
55 33 26 14 25 10 12 14
class Matrix12
{
public static void main(int A[][])
{
int i,j;
int B[][]=new int[4][4];
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
B[i][j]=A[j][i];
}
}
System.out.println("Transpose Matrix are : ");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(B[i][j]);
}
System.out.println();
}
}
}
24. WAP in java to input 2-Dsquare matrix and check whether it is a Lower Triangular Matrix or not.
(Lower Triangular Matrix is a square matrix in which all the entries above the main diagonals are zero. The
entries below or on the main diagonal themselves may or may not be zero.)
Sample Input:
5 0 0 0
3 1 0 0
4 9 4 0
6 8 7 2
Sample Output: It is Lower Triangular Matrix
class Matrix12
{
public static void main(int A[][])
{
int i,j,c=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(j>i && A[i][j]!=0)
c++;
}
}
if(c==0)
System.out.println("it is lower triangular matrix");
else
System.out.println("it is not lower triangular matrix");
}
}

25. 24. WAP in java to input 2-Dsquare matrix and check whether it is a Upper Triangular Matrix or not.
(Upper Triangular Matrix is a square matrix in which all the entries below the main diagonals are zero. The
entries above or on the main diagonal themselves may or may not be zero.)
Sample Input:
5 3 0 7
0 1 9 8
0 0 4 6
0 0 0 2
Sample Output: It is an Upper Triangular Matrix
class Matrix12
{
public static void main(int A[][])
{
int i,j,c=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(j<i && A[i][j]!=0)
c++;
}
}
if(c==0)
System.out.println("it is upper triangular matrix");
else
System.out.println("it is not upper triangular matrix");
}
}
26. WAP in java to input a 2-D square matrix and check whether it is a Scalar Matrix or not.
(A square matrix is said to be scalar matrix if all the main diagonal elements are equal and other elements
except main diagonal are zero)
Sample Input:
5 0 0 0
0 5 0 0
0 0 5 0
0 0 0 5
Sample Output: It is Scalar Matrix
class Matrix12
{
public static void main(int A[][])
{
int i,j,s=A[0][0],c=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i==j && A[i][j]!=s)
c++;
if(i!=j && A[i][j]!=0)
c++;
}
}
if(c==0)
System.out.println("it is Scalar matrix");
else
System.out.println("it is not Scalar matrix");
}
}
27. Write a program in Java to enter natural numbers in a double dimensional array m x n (where m
is the number of rows and n is the number of columns). Display the new matrix in such a way that
the new matrix is the mirror image of the original matrix.
Sample Input: Sample Output:
1 2 3 4 4 3 2 1
7 8 11 18 18 11 8 7
9 10 90 12 12 90 10 9
17 18 70 13 13 70 18 17
class Matrix12
{
public static void main(int A[][],int m,int n)
{
int i,j,k;
int B[][]=new int[m][n];
for(i=0;i<m;i++)
{
k=0;
for(j=n-1;j>=0;j--)
{
B[i][k++]=A[i][j];
}
}
System.out.println("Mirror image are : ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(B[i][j]);
}
System.out.println();
}
}
}

You might also like