0% found this document useful (0 votes)
37 views

Document

Uploaded by

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

Document

Uploaded by

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

MATRIX addition

Public class MatrixAddition {

Public static void main(String[] args) {

Int[][] matrix1 = {{1, 2}, {3, 4}};

Int[][] matrix2 = {{5, 6}, {7, 8}};

Int[][] result = new int[2][2];

For (int i = 0; i < 2; i++) {

For (int j = 0; j < 2; j++) {

Result[i][j] = matrix1[i][j] + matrix2[i][j];

System.out.println(“Matrix Addition:”);

For (int[] row : result) {

For (int value : row) {

System.out.print(value + “ “);

System.out.println();

SUBTRACTION

Public class MatrixSubtraction {

Public static void main(String[] args) {

Int[][] matrix1 = {{5, 6}, {7, 8}};

Int[][] matrix2 = {{1, 2}, {3, 4}};


Int[][] result = new int[2][2];

For (int i = 0; i < 2; i++) {

For (int j = 0; j < 2; j++) {

Result[i][j] = matrix1[i][j] – matrix2[i][j];

System.out.println(“Matrix Subtraction:”);

For (int[] row : result) {

For (int value : row) {

System.out.print(value + “ “);

System.out.println();

MULTIPLICATION

Public class MatrixMultiplication {

Public static void main(String[] args) {

Int[][] matrix1 = {{1, 2}, {3, 4}};

Int[][] matrix2 = {{5, 6}, {7, 8}};

Int[][] result = new int[2][2];

For (int i = 0; i < 2; i++) {

For (int j = 0; j < 2; j++) {

For (int k = 0; k < 2; k++) {


Result[i][j] += matrix1[i][k] * matrix2[k][j];

System.out.println(“Matrix Multiplication:”);

For (int[] row : result) {

For (int value : row) {

System.out.print(value + “ “);

System.out.println();

TRANSPOSE OF MATRIX

Public class MatrixTranspose {

Public static void main(String[] args) {

Int[][] matrix = {{1, 2}, {3, 4}};

Int[][] transpose = new int[2][2];

For (int i = 0; i < 2; i++) {

For (int j = 0; j < 2; j++) {

Transpose[i][j] = matrix[j][i];

System.out.println(“Transpose of Matrix:”);

For (int[] row : transpose) {


For (int value : row) {

System.out.print(value + “ “);

System.out.println();

Display main

Public class MatrixDiagonalElements {

Public static void main(String[] args) {

Int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

System.out.print(“Main Diagonal: “);

For (int i = 0; i < matrix.length; i++) {

System.out.print(matrix[i][i] + “ “);

System.out.print(“\nSecondary Diagonal: “);

For (int i = 0; i < matrix.length; i++) {

System.out.print(matrix[i][matrix.length – i – 1] + “ “);

Sum of elements

Int mainSum = 0, secSum = 0;

For (int i = 0; i < matrix.length; i++) {

mainSum += matrix[i][i];
secSum += matrix[i][matrix.length – i – 1];

You might also like