0% found this document useful (0 votes)
2 views3 pages

Matrix Test Dedt

Tedt

Uploaded by

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

Matrix Test Dedt

Tedt

Uploaded by

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

Practical-3: Program to perform various Matrix operations.

import java.util.Scanner;
public class matrix
{
int a[][]=new int[3][3];
Scanner scan=new Scanner(System.in);
matrix()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=0;
}
}
}
void enterelements()
{
System.out.println("Enter the elements");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=scan.nextInt();
}
}
}
matrix addition(matrix m)
{
matrix result=new matrix();
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
result.a[i][j]=a[i][j]+m.a[i][j];
}
}
return result;
}
matrix transpose()
{
matrix trans=new matrix();
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
trans.a[i][j]=a[j][i];
}
}
return trans;
}
void display()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.print("\n");
}
}
matrix multiplication(matrix m)
{
matrix mul=new matrix();
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
for(int k=0;k<3;k++)
{
mul.a[i][j]=mul.a[i][j]+a[i][k]*m.a[k][j];
}
}
}
return mul;
}
public static void main(String[] args)
{
matrix m1=new matrix();
matrix m2=new matrix();
matrix m3=new matrix();
matrix m4=new matrix();
matrix m5=new matrix();
m1.enterelements();
m2.enterelements();
m3=m1.addition(m2);
System.out.println("Addition of m1 and m2");
m3.display();
System.out.println("Transpose of m3");
m4=m3.transpose();
m4.display();
System.out.println("Multiplication of m1 and m2");
m5=m1.multiplication(m2);
m5.display();
}
}

Output:

Enter the elements


123
456
789
Enter the elements
100
010
001
Addition of m1 and m2
223
466
7 8 10
Transpose of m3
247
268
3 6 10
Multiplication of m1 and m2
123
456
789

You might also like