Unit –I
JAVA PROGRAMS
1 a. Implementation of Matrix Operations using Arrays.
Ex-1.a1: Matrix Addition
Aim:
To write a Java program to implement Matrix Addition using Arrays.
Algorithm:
Step 1: Start
Step 2: Create a Scanner object to read input from the user.
Step 3: Prompt the user to enter the number of rows (r) and columns (c).
Step 4: Declare three 2D arrays of size r x c:
• A[][] → to store the first matrix
• B[][] → to store the second matrix
• sum[][] → to store the result of matrix addition
Step 5: Prompt the user to enter elements for the first matrix
Step 6: Prompt the user to enter elements for the second matrix
Step 7: Perform matrix addition
Step 8: Display the result matrix (sum[][])
Step 9: Stop
Program:
import java.util.Scanner;
public class MatrixAddition
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int r = s.nextInt();
System.out.print("Enter the number of columns: ");
int c = s.nextInt();
int A[][] = new int[r][c];
int B[][]= new int[r][c];
int sum[][] = new int[r][c];
System.out.println("Enter elements for the first matrix:");
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
A[i][j] = s.nextInt();
}
}
System.out.println("Enter elements for the second matrix:");
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
B[i][j] = s.nextInt();
}
}
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
sum[i][j] = A[i][j] + B[i][j];
}
}
System.out.println("Sum of the matrices:");
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
}
}
Output:
Compilation:
C:\javanew>javac MatrixAddition.java
Run:
C:\javanew>java MatrixAddition
Enter the number of rows: 3
Enter the number of columns: 3
Enter elements for the first matrix:
2
2
2
2
2
2
2
2
2
Enter elements for the second matrix:
3
1
4
5
2
3
4
5
1
Sum of the matrices:
5 3 6
7 4 5
6 7 3
Result:
Thus the Addition of Matrix program was compiled and executed
successfully.
Ex- 1.a2: Matrix Multiplication
Aim:
To write a Java program to implement Matrix Addition using Arrays.
Algorithm:
Step 1: Start the program
Step 2: Create a Scanner object to read input from the user.
Step 3: Read the number of rows and columns for both matrices:
Step 4: Check the compatibility condition for matrix multiplication:
• If col1 != row2, print "Matrix multiplication is not possible"
and exit the program.
Step 5: Declare three matrices A,B and C
Step 6: Input elements of matrix A:
Step 7: Input elements of matrix B:
Step 8: Perform matrix multiplication:
Step 9: Display the result matrix C:
Step 10: Stop the program
Program:
import java.util.Scanner;
public class matrixmultiplication
{
public static void main(String args[])
{
int row1, col1, row2, col2;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of rows in first matrix: ");
row1 = s.nextInt();
System.out.print("Enter number of columns in first matrix: ");
col1 = s.nextInt();
System.out.print("Enter number of rows in second matrix: ");
row2 = s.nextInt();
System.out.print("Enter number of columns in second matrix: ");
col2 = s.nextInt();
if (col1 != row2)
{
System.out.println("Matrix multiplication is not possible");
return;
}
int A[][] = new int[row1][col1];
int B[][] = new int[row2][col2];
int C[][] = new int[row1][col2];
System.out.println("\nEnter values for matrix A : ");
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col1; j++)
{
A[i][j] = s.nextInt();
}
}
System.out.println("\nEnter values for matrix B : ");
for (int i = 0; i < row2; i++)
{
for (int j = 0; j < col2; j++)
{
B[i][j] = s.nextInt();
}
}
System.out.println("\nMatrix multiplication is : ");
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col2; j++)
{
C[i][j] = 0;
for (int k = 0; k < col1; k++)
{
C[i][j] += A[i][k] * B[k][j];
}
System.out.print(C[i][j] + " ");
}
System.out.println();
}
}
}
Output:
Compilation:
C:\javanew>javac matrixmultiplication.java
Run:
C:\javanew>java matrixmultiplication
Enter number of rows in first
matrix: 2 Enter number of
columns in first matrix: 3 Enter
number of rows in second
matrix: 3
Enter number of columns in second matrix: 2
Enter values for matrix A :
2
4
6
8
10
12
Enter values for matrix B :
2
4
3
5
6
7
Matrix multiplication is :
52 70
118 166
Result:
Thus the Addition of Matrix program was compiled and
executed successfully.
Ex- 1.a3: Matrix Transpose
Aim:
Implementing Matrix Transpose using Arrays in Java.
Algorithm:
Step 1: Start
Step 2: Create a Scanner object to read input from the user.
Step 3: Read the number of rows (r) and columns (c) of the matrix.
Step 4: Declare a 2D array A of size [r][c] to store the original matrix.
Step 5: Input the elements of the matrix A[i][j]
Step 6: Display the original matrix
Step 7: Display the transpose of the matrix A[j][i]
Step 8: End
Program:
import java.util.Scanner;
public class Transpose
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int r = s.nextInt();
System.out.print("Enter the number of columns: ");
int c = s.nextInt();
int A[][] = new int[r][c];
System.out.println("Enter elements for the first matrix:");
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
A[i][j] = s.nextInt();
}
}
System.out.println("Original Matrix:");
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
System.out.print(A[i][j] + " ");
}
System.out.println();
}
System.out.println("Transpose of Matrix:");
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
System.out.print(A[j][i] + " ");
}
System.out.println();
}
}
}
Output:
Compilation:
C:\javanew>javac Transpose.java
Run:
C:\javanew>java Transpose
Enter the number of rows: 2
Enter the number of columns: 2
Enter elements for the first matrix:
1
2
3
4
Original Matrix:
1 2
3 4
Transpose of Matrix:
1 3
2 4
Result:
Thus the Transpose Matrix program was compiled and executed
successfully.