Visitar URL original
Create MatrixAddition.java by PranavViswanathan · Pull Request #2227 · TheAlgorithms/Java · GitHub
The Wayback Machine - https://web.archive.org/web/20210605234238/https://github.com/TheAlgorithms/Java/pull/2227/files
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create MatrixAddition.java #2227

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter
Filter file types
Beta Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.

Always

Just for now

@@ -0,0 +1,105 @@

import java.util.Scanner;

public class MatrixAddition {

Scanner scan;
int matrix1[][], matrix2[][], sum[][];
int row, column;

void create() {

scan = new Scanner(System.in);

System.out.println("Matrix Addition");

// First Matrix Creation..
System.out.println("\nEnter number of rows & columns");
row = Integer.parseInt(scan.nextLine());
column = Integer.parseInt(scan.nextLine());

matrix1 = new int[row][column];
matrix2 = new int[row][column];
sum = new int[row][column];

System.out.println("Enter the data for first matrix :");

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {

matrix1[i][j] = scan.nextInt();
}
}

// Second Matrix Creation..
System.out.println("Enter the data for second matrix :");

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {

matrix2[i][j] = scan.nextInt();
}
}
}

void display() {

System.out.println("\nThe First Matrix is :");

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {

System.out.print("\t" + matrix1[i][j]);
}
System.out.println();
}

System.out.println("\n\nThe Second Matrix is :");

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {

System.out.print("\t" + matrix2[i][j]);
}
System.out.println();
}
}

void add() {

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {

sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

System.out.println("\n\nThe Sum is :");

for(int i=0; i<row; i++) {

This comment has been minimized.

@anantcodes

anantcodes May 30, 2021
Member

Please remove extra spacing between lines.

for(int j=0; j<column; j++) {

System.out.print("\t" + sum[i][j]);
}
System.out.println();
}
}
}

class MainClass {

public static void main(String args[]) {

MatrixAddition obj = new MatrixAddition();

obj.create();
obj.display();
obj.add();
}
}
@@ -0,0 +1,104 @@
import java.util.Scanner;

public class Matrix_Multiplication {

Scanner scan;
int matrix1[][], matrix2[][], multi[][];
int row, column;

void create() {

scan = new Scanner(System.in);

System.out.println("Matrix Multiplication");

// First Matrix Creation..
System.out.println("\nEnter number of rows & columns");
row = Integer.parseInt(scan.nextLine());
column = Integer.parseInt(scan.nextLine());

matrix1 = new int[row][column];
matrix2 = new int[row][column];
multi = new int[row][column];

System.out.println("Enter the data for first matrix :");

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {

matrix1[i][j] = scan.nextInt();
}
}

// Second Matrix Creation..
System.out.println("Enter the data for second matrix :");

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {

matrix2[i][j] = scan.nextInt();
}
}
}

void display() {

System.out.println("\nThe First Matrix is :");

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {

System.out.print("\t" + matrix1[i][j]);
}
System.out.println();
}

System.out.println("\n\nThe Second Matrix is :");

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {

System.out.print("\t" + matrix2[i][j]);
}
System.out.println();
}
}

void multi() {

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {

multi[i][j] = matrix1[i][j] * matrix2[i][j];
}
}

System.out.println("\n\nThe Multiplication is :");

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {

System.out.print("\t" + multi[i][j]);
}
System.out.println();
}
}
}

class MainClass {

public static void main(String args[]) {

Matrix_Multiplication obj = new Matrix_Multiplication();

obj.create();
obj.display();
obj.multi();
}
}
@@ -0,0 +1,104 @@
import java.util.Scanner;

public class Matrix_Subtraction {

Scanner scan;
int matrix1[][], matrix2[][], sub[][];
int row, column;

void create() {

scan = new Scanner(System.in);

System.out.println("Matrix Subtraction");

// First Matrix Creation..
System.out.println("\nEnter number of rows & columns");
row = Integer.parseInt(scan.nextLine());
column = Integer.parseInt(scan.nextLine());

matrix1 = new int[row][column];
matrix2 = new int[row][column];
sub = new int[row][column];

System.out.println("Enter the data for first matrix :");

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {

matrix1[i][j] = scan.nextInt();
}
}

// Second Matrix Creation..
System.out.println("Enter the data for second matrix :");

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {

matrix2[i][j] = scan.nextInt();
}
}
}

void display() {

System.out.println("\nThe First Matrix is :");

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {

System.out.print("\t" + matrix1[i][j]);
}
System.out.println();
}

System.out.println("\n\nThe Second Matrix is :");

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {

System.out.print("\t" + matrix2[i][j]);
}
System.out.println();
}
}

void sub() {

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {

sub[i][j] = matrix1[i][j] - matrix2[i][j];
}
}

System.out.println("\n\nThe Subtraction is :");

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {

System.out.print("\t" + sub[i][j]);
}
System.out.println();
}
}
}

class MainClass {

public static void main(String args[]) {

Matrix_Subtraction obj = new Matrix_Subtraction();

obj.create();
obj.display();
obj.sub();
}
}
ProTip! Use n and p to navigate between commits in a pull request.