Note: In linear algebra, the trace of an n-by-n square matrix A is defined to be the sum of the elements on the main diagonal (the diagonal from the upper left to the lower right) of A,
public class MatrixAddAndSub {
public static void main(String args[]) {
int a[][] = new int[3][3];
int b[][] = new int[3][3];
int add[][] = new int[3][3];
int sub[][] = new int[3][3];
int trace=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter First Matrix ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Value of First Matrix is ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println("Enter Second Matrix ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
b[i][j]=sc.nextInt();
}
}
System.out.println("Value of Second Matrix is ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println();
}
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
add[i][j]=a[i][j]+b[i][j];
}
}
System.out.println("Addition of Matrix is ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
System.out.print(add[i][j]+" ");
}
System.out.println();
}
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
sub[i][j]=a[i][j]-b[i][j];
}
}
System.out.println("Substraction of Matrix is ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
System.out.print(sub[i][j]+" ");
}
System.out.println();
}
// Tracing Logic
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
if(i==j)
{
trace=trace+a[i][j];
}
}
}
System.out.println("Trace of Matrix A is "+trace);
}
}
Output:
Enter First Matrix
1
2
3
1
2
3
1
2
3
Value of First Matrix is
1 2 3
1 2 3
1 2 3
Enter Second Matrix
1
2
3
1
2
3
1
2
3
Value of Second Matrix is
1 2 3
1 2 3
1 2 3
Addition of Matrix is
2 4 6
2 4 6
2 4 6
Substraction of Matrix is
0 0 0
0 0 0
0 0 0
Trace of Matrix A is 6
public class MatrixAddAndSub {
public static void main(String args[]) {
int a[][] = new int[3][3];
int b[][] = new int[3][3];
int add[][] = new int[3][3];
int sub[][] = new int[3][3];
int trace=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter First Matrix ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Value of First Matrix is ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println("Enter Second Matrix ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
b[i][j]=sc.nextInt();
}
}
System.out.println("Value of Second Matrix is ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println();
}
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
add[i][j]=a[i][j]+b[i][j];
}
}
System.out.println("Addition of Matrix is ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
System.out.print(add[i][j]+" ");
}
System.out.println();
}
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
sub[i][j]=a[i][j]-b[i][j];
}
}
System.out.println("Substraction of Matrix is ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
System.out.print(sub[i][j]+" ");
}
System.out.println();
}
// Tracing Logic
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
if(i==j)
{
trace=trace+a[i][j];
}
}
}
System.out.println("Trace of Matrix A is "+trace);
}
}
Output:
Enter First Matrix
1
2
3
1
2
3
1
2
3
Value of First Matrix is
1 2 3
1 2 3
1 2 3
Enter Second Matrix
1
2
3
1
2
3
1
2
3
Value of Second Matrix is
1 2 3
1 2 3
1 2 3
Addition of Matrix is
2 4 6
2 4 6
2 4 6
Substraction of Matrix is
0 0 0
0 0 0
0 0 0
Trace of Matrix A is 6
|
No comments:
Post a Comment