[Go to site: main page, start]

0% found this document useful (0 votes)
7 views21 pages

Java Matrix Operations Tutorial

The document contains multiple Java programs that perform various matrix operations such as addition, subtraction, multiplication, and transposition, as well as basic arithmetic operations like sum of digits, reversing a number, and finding the largest number among three inputs. Each program includes user input for matrix dimensions and elements, followed by the computation and output of results. The document illustrates the use of loops and arrays in Java to manipulate and display matrix data and perform calculations.

Uploaded by

youube828101
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)
7 views21 pages

Java Matrix Operations Tutorial

The document contains multiple Java programs that perform various matrix operations such as addition, subtraction, multiplication, and transposition, as well as basic arithmetic operations like sum of digits, reversing a number, and finding the largest number among three inputs. Each program includes user input for matrix dimensions and elements, followed by the computation and output of results. The document illustrates the use of loops and arrays in Java to manipulate and display matrix data and perform calculations.

Uploaded by

youube828101
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

Addition of matrix

import [Link].*;
public class addmatrix {

public static void main(String arg[])


{
Scanner sc = new Scanner([Link]);
int i,j,m=0,n=0;
[Link]("Enter the size of matrix");
m=[Link]();
n=[Link]();
int a[][] =new int[m][n];
int b[][] =new int[m][n];
int c[][]= new int[m][n];
[Link]("Enter the element of matrix a");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=[Link]();
}
}
[Link]("matrix a");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{

[Link]("\t"+a[i][j]);
}
[Link]("");
}

[Link]("Enter the elemnet of matrix b");


for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[i][j]=[Link]();
}
}
[Link]("Matrix b");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{

[Link]("\t"+b[i][j]);
}

[Link]("");

[Link]("sum of matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
[Link]("Sum of matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
[Link]("\t"+c[i][j]);
}
[Link]("");
}

}
}

output
s-java-project/bin addmatrix
Enter the size of matrix
2
2
Enter the element of matrix a
2
3
4
5
matrix a
2 3
4 5
Enter the elemnet of matrix b
5
4
4
4
Matrix b
5 4
4 4
sum of matrix
Sum of matrix
7 7
8 9
Subtraction of matrix

import [Link].*;
public class submatrix {

public static void main(String arg[])


{
Scanner sc = new Scanner([Link]);
int i,j,m=0,n=0;
[Link]("Enter the size of matrix");
m=[Link]();
n=[Link]();
int a[][] =new int[m][n];
int b[][] =new int[m][n];
int c[][]= new int[m][n];
[Link]("Enter the element of matrix a");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=[Link]();
}
}
[Link]("matrix a");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{

[Link]("\t"+a[i][j]);
}
[Link]("");
}

[Link]("Enter the elemnet of matrix b");


for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[i][j]=[Link]();
}
}
[Link]("Matrix b");

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{

[Link]("\t"+b[i][j]);
}

[Link]("");

[Link]("Difference of matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
[Link]("Difference of matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
[Link]("\t"+c[i][j]);
}
[Link]("");
}

}
}

Output
venvshamsheralam@Sulekh-Kumars-MacBook-Air sulekh % /usr/bin/env /Users/shamsh
eralam/Library/Java/JavaVirtualMachines/openjdk-23.0.2/Contents/Home/bin/java --
enable-preview -XX:+ShowCodeDetailsInExceptionMessages -cp /Users/shamsheralam/L
ibrary/Application\ Support/Code/User/workspaceStorage/9a55cf60d2fac102374376bbc
7797c0d/[Link]/jdt_ws/sulekh_b0839b17/bin submatrix
Enter the size of matrix
2
2
Enter the element of matrix a
2
3
4
5
matrix a
2 3
4 5
Enter the elemnet of matrix b

6
6
7
7
Matrix b
6 6
7 7
Difference of matrix
Difference of matrix
-4 -3
-3 -2
Subtraction of matrix using io package
import [Link].*;
public class matrixsuio{
public static void main(String agr[])
{
InputStreamReader read =new InputStreamReader([Link]);
BufferedReader br= new BufferedReader(read);
int i,j,m=0,n=0;
[Link]("Enter the size of matrix");
try{
m=[Link]([Link]());
n=[Link]([Link]());
}
catch(Exception e)
{
[Link]("IO exception ");
}
int a[][] =new int[m][n];
int b[][]=new int[m][n];
int c[][]= new int[m][n];
[Link]("Enter the element of matrix a");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{

try{

a[i][j]=[Link]([Link]());
}
catch(Exception e)
{

}
[Link]("Matrix a");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
[Link]("\t"+a[i][j]);

}
[Link]("");

[Link]("Enter the element of matrix b");

for(i=0;i<m;i++)
{

for(j=0;j<n;j++)
{
try{

b[i][j]=[Link]([Link]());
}
catch(Exception e)

}
}
}
[Link]("Matrix b");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
[Link]("\t"+b[i][j]);
}
[Link]("");
}
[Link]("Difference of matrix");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
c[i][j]=a[i][j]-b[i][j];

}
}
[Link]("Difference of matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
[Link]("\t"+c[i][j]);
}
[Link]("");
}

Output
.venvshamsheralam@Sulekh-Kumars-MacBook-Air sulekh % /usr/bin/env /Users/shamsh
eralam/Library/Java/JavaVirtualMachines/openjdk-23.0.2/Contents/Home/bin/java --
enable-preview -XX:+ShowCodeDetailsInExceptionMessages -cp /Users/shamsheralam/L
ibrary/Application\ Support/Code/User/workspaceStorage/9a55cf60d2fac102374376bbc
7797c0d/[Link]/jdt_ws/sulekh_b0839b17/bin submatrix
Enter the size of matrix
2
2
Enter the element of matrix a
2
3
4
5
matrix a
2 3
4 5
Enter the elemnet of matrix b

6
6
7
7
Matrix b
6 6
7 7
Difference of matrix
Difference of matrix
-4 -3
-3 -2

Matrix Multiplication
import [Link].*;
public class matrixmul {
public static void main(String arg[])
{
Scanner sc = new Scanner([Link]);
int i,j,m,n;
[Link]("Enter the value of row and column");
m=[Link]();
n=[Link]();
int a[][]= new int[m][n];
int b[][]= new int[m][n];
int c[][]=new int[m][n];
[Link]("Enter the element of matrix A");
for( i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=[Link]();
}
}
[Link]("Element of matrix A");
for( i=0;i<m;i++)
{
for( j=0;j<n;j++)
{
[Link]("\t"+a[i][j]);
}
[Link]("");
}
[Link]("Enter the element of matrix B");

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[i][j]=[Link]();
}
}
[Link]("Print Matrix B");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
[Link]("\t"+b[i][j]);

}
[Link]("");
}

[Link]("Product of matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
for(int k=0;k<n;k++)
c[i][j]=c[i][j]+ a[i][k]*b[k][j];
}
}
[Link]("Product of Matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
[Link]("\t"+c[i][j]);
}
[Link]("");
}
}

Output
venvshamsheralam@Sulekh-Kumars-MacBook-Air sulekh % /usr/bin/env /Users/shamsh
eralam/Library/Java/JavaVirtualMachines/openjdk-23.0.2/Contents/Home/bin/java --
enable-preview -XX:+ShowCodeDetailsInExceptionMessages -cp /Users/shamsheralam/L
ibrary/Application\ Support/Code/User/workspaceStorage/9a55cf60d2fac102374376bbc
7797c0d/[Link]/jdt_ws/sulekh_b0839b17/bin matrixmul
Enter the value of row and column
2
2
Enter the element of matrix A
2
3
4
5
Element of matrix A
2 3
4 5
Enter the element of matrix B
4
4
5
6
Print Matrix B
4 4
5 6
Product of matrix
Product of Matrix
23 26
41 46

Tranpose of Matrix
import [Link].*;
public class transpose {
public static void main(String arg[])
{
Scanner sc =new Scanner([Link]);
int i,j,m=0,n=0;
[Link]("Enter the size of matrix");
m=[Link]();
n=[Link]();
int a[][]=new int[m][n];
int t[][]=new int[m][n];
[Link]("Enter the element of matrix a");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=[Link]();
}

}
[Link]("Matrix B");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
[Link]("\t"+a[i][j]);
}
[Link]("");
}
[Link]("Transpose of matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
t[j][i]=a[i][j];
}
}
[Link]("Print Transpose matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
[Link]("\t"+t[i][j]);
}
[Link]("");
}
}

Output
eralam/Library/Java/JavaVirtualMachines/openjdk-23.0.2/Contents/Home/bin/java --
enable-preview -XX:+ShowCodeDetailsInExceptionMessages -cp /Users/shamsheralam/L
ibrary/Application\ Support/Code/User/workspaceStorage/9a55cf60d2fac102374376bbc
7797c0d/[Link]/jdt_ws/sulekh_b0839b17/bin transpose
Enter the size of matrix
2
2
Enter the element of matrix a
3
4
5
6
Matrix B
3 4
5 6
Transpose of matrix
Print Transpose matrix
3 5
4 6
Sum of Digit
import [Link].*;
public class sumdigit {
public static void main(String args[])
{
Scanner sc =new Scanner([Link]);
int r,n,sum=0;
[Link]("Enter the value of n");
n=[Link]();
while(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
[Link]("Sum of digit="+sum);
}

Output
venvshamsheralam@Sulekh-Kumars-MacBook-Air sulekh % /usr/bin/env /Users/shamsh
eralam/Library/Java/JavaVirtualMachines/openjdk-23.0.2/Contents/Home/bin/java --
enable-preview -XX:+ShowCodeDetailsInExceptionMessages -cp /Users/shamsheralam/L
ibrary/Application\ Support/Code/User/workspaceStorage/9a55cf60d2fac102374376bbc
7797c0d/[Link]/jdt_ws/sulekh_b0839b17/bin sumdigit
Enter the value of n
3456
Sum of digit=18
Reverse of a no
import [Link].*;
public class reverse {
public static void main(String arg[])
{
Scanner sc =new Scanner([Link]);
int r,n,rev=0;
[Link]("Enter the value of n");
n=[Link]();
while(n>0)
{
r=n%10;
rev=rev*10 +r;
n=n/10;
}
[Link]("Reverse of ano="+rev);

Output
.venvshamsheralam@Sulekh-Kumars-MacBook-Air sulekh % /usr/bin/env /Users/shamsh
eralam/Library/Java/JavaVirtualMachines/openjdk-23.0.2/Contents/Home/bin/java --
enable-preview -XX:+ShowCodeDetailsInExceptionMessages -cp /Users/shamsheralam/L
ibrary/Application\ Support/Code/User/workspaceStorage/9a55cf60d2fac102374376bbc
7797c0d/[Link]/jdt_ws/sulekh_b0839b17/bin reverse
Enter the value of n
4567
Reverse of ano=7654

Largest no
import [Link].*;
public class largest {
public static void main(String arg[])
{
Scanner sc = new Scanner([Link]);
int a,b,c;
[Link]("Enter the value of a,b,c");
a=[Link]();
b=[Link]();
c=[Link]();
if(a>b && a>c)
{
[Link]("a is the largest no"+a);
}
else if (b>a && b>c)
{
[Link]("b is the largest no"+b);

}
else if(c>a && c>b)
{
[Link]("c is the largest no"+c);
}
else
{
[Link]("All numbers are equal");
}
}

Output:-
venvshamsheralam@Sulekh-Kumars-MacBook-Air sulekh % /usr/bin/env /Users/shamsh
eralam/Library/Java/JavaVirtualMachines/openjdk-23.0.2/Contents/Home/bin/java --
enable-preview -XX:+ShowCodeDetailsInExceptionMessages -cp /Users/shamsheralam/L
ibrary/Application\ Support/Code/User/workspaceStorage/9a55cf60d2fac102374376bbc
7797c0d/[Link]/jdt_ws/sulekh_b0839b17/bin largest
Enter the value of a,b,c
45
56
67
c is the largest no67

Program of grade
import [Link].*;
public class grade {

public static void main(String arg[])


{
Scanner sc = new Scanner([Link]);
int mk1,mk2,mk3,mk4,mk5;
double avg;
[Link]("Enter the marks ");
mk1=[Link]();
mk2=[Link]();
mk3=[Link]();
mk4=[Link]();
mk5=[Link]();
avg=(mk1+mk2+mk3+mk4+mk5)/5;
if(avg>=91)
{
[Link]("This is grade A+"+avg);

}
else if(avg>=81 && avg<91)
{
[Link]("This is a grade A"+avg);
}
else if(avg>=71 && avg<81)
{
[Link]("This is grade B+"+avg);
}
else if(avg>=61 && avg<71)
{
[Link]("This is grade B"+avg);
}
else if(avg>=51 && avg<61)
{
[Link]("This is grade C+"+avg);
}
else if(avg>=41 && avg<51)
{
[Link]("This is grade C"+avg);

}
else if(avg>=33 && avg<41)
{
[Link]("This is grade D"+avg);

}
else
{
[Link]("This is fail"+avg);
}

}
}

Output:
.venvshamsheralam@Sulekh-Kumars-MacBook-Air sulekh % /usr/bin/env /Users/shamsh
eralam/Library/Java/JavaVirtualMachines/openjdk-23.0.2/Contents/Home/bin/java --
enable-preview -XX:+ShowCodeDetailsInExceptionMessages -cp /Users/shamsheralam/L
ibrary/Application\ Support/Code/User/workspaceStorage/9a55cf60d2fac102374376bbc
7797c0d/[Link]/jdt_ws/sulekh_b0839b17/bin grade
Enter the marks
56
78
98
67
98
This is grade B+79.0

Program of Factorial
import [Link].*;
public class factorial {
public static void main(String arg[])
{
Scanner sc = new Scanner([Link]);
int n,f=1;
[Link]("Enter a number");
n=[Link]();
for(int i=1;i<=n;i++){
f=f*i;
}
[Link]("Factorial of a number="+f);

}
}

Output
venvshamsheralam@Sulekh-Kumars-MacBook-Air sulekh % /usr/bin/env /Users/shamsh
eralam/Library/Java/JavaVirtualMachines/openjdk-23.0.2/Contents/Home/bin/java --
enable-preview -XX:+ShowCodeDetailsInExceptionMessages -cp /Users/shamsheralam/L
ibrary/Application\ Support/Code/User/workspaceStorage/9a55cf60d2fac102374376bbc
7797c0d/[Link]/jdt_ws/sulekh_b0839b17/bin factorial
Enter a number
5
Factorial of a number=120
Evenodd number
import [Link].*;
public class even{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int n;
[Link]("Enter the vlaue of n");
n= [Link]();
if(n%2==0)
{
[Link]("This is even no="+n);

}
else{
[Link]("This is odd no="+n);
}

Output
ralam/Library/Java/JavaVirtualMachines/openjdk-23.0.2/Contents/Home/bin/java --
enable-preview -XX:+ShowCodeDetailsInExceptionMessages -cp /Users/shamsheralam/L
ibrary/Application\ Support/Code/User/workspaceStorage/9a55cf60d2fac102374376bbc
7797c0d/[Link]/jdt_ws/sulekh_b0839b17/bin even
Enter the value of n
6
This is even no=6

Program of Divion
import [Link].*;
public class divison {
public static void main(String arg[])
{
Scanner sc = new Scanner([Link]);
int mk1,mk2,mk3,mk4,mk5;
double avg;
[Link]("Enter the marks of five subject");
mk1=[Link]();
mk2=[Link]();
mk3=[Link]();
mk4=[Link]();
mk5=[Link]();
avg=(mk1+mk2+mk3+mk4+mk5)/5;
[Link]("Percentage ="+avg);
if(avg>=60)
{
[Link]("This is the 1st divison"+avg);
}
else if(avg>=45 && avg<60)
{
[Link]("This is 2nd Divison"+avg);

}
else if(avg>=33 && avg<45)
{
[Link]("This is 3rd Division"+avg);

}
else
{
[Link]("This is fail"+avg);
}

Output:
venvshamsheralam@Sulekh-Kumars-MacBook-Air sulekh % /usr/bin/env /Users/shamsh
eralam/Library/Java/JavaVirtualMachines/openjdk-23.0.2/Contents/Home/bin/java --
enable-preview -XX:+ShowCodeDetailsInExceptionMessages -cp /Users/shamsheralam/L
ibrary/Application\ Support/Code/User/workspaceStorage/9a55cf60d2fac102374376bbc
7797c0d/[Link]/jdt_ws/sulekh_b0839b17/bin divison
Enter the marks of five subject
56
78
98
87
87
Percentage =81.0
This is the 1st divison81.0
Armstrong no
import [Link].*;
import [Link].*;
public class armstrong {
public static void main(String arg[])
{
InputStreamReader read = new InputStreamReader([Link]);
BufferedReader br = new BufferedReader(read);
int n=0,r;
double sum=0;
[Link]("Enter the value of n");
try{
n=[Link]([Link]());

}
catch(Exception e)
{
[Link]("IO error");

}
double temp=n;
while(n!=0)
{
r= n%10;
sum = sum + [Link](r, 3);
n=n/10;
}
[Link]("Sum="+sum);
if(temp==sum)
{
[Link]("This is armstrong no"+sum);
}
else
{
[Link]("This is not a armstrong no");
}
}

Output:
.venvshamsheralam@Sulekh-Kumars-MacBook-Air sulekh % /usr/bin/env /Users/shamsh
eralam/Library/Java/JavaVirtualMachines/openjdk-23.0.2/Contents/Home/bin/java --
enable-preview -XX:+ShowCodeDetailsInExceptionMessages -cp /Users/shamsheralam/L
ibrary/Application\ Support/Code/User/workspaceStorage/9a55cf60d2fac102374376bbc
7797c0d/[Link]/jdt_ws/sulekh_b0839b17/bin armstrong
Enter the value of n
786
Sum=1071.0
This is not a armstrong no

You might also like