AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: calculate the addition and subtraction of two given matrices

Write a Program to calculate the addition and subtraction of two given matrices(3x3).

Input Specification:

Input will be two matrices each 3x3 order

Output Specification:

Your Program should print two 3x3 matrices with first matrix Representing the sum and the next matrix representing the difference Of those two matrices. Print an empty line between the matrices.

Sample Input:

1 2 3

4 5 6

7 8 9

5 4 3

6 7 2

5 4 5

Sample Output:

6 6 6

10 12 8

12 12 14



-4 -2 0

-2 -2 4

2 4 4

corejava x 333
programming x 150
Posted On : 2016-07-13 17:37:16.0
profile Boopalan - anyforum.in Boopalan
3730
up-rate
4
down-rate

Answers


import java.util.Scanner;

public class MainClass {

public static void main(String args[]) {

Matrix obj = new Matrix();

obj.create();
obj.display();
obj.add();

}
}

class Matrix {

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

void create() {

Scanner sc= new Scanner(System.in);

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

// 1st matrix creation

System.out.println("\nEnter number of rows & columns");

row = Integer.parseInt(sc.nextLine());
column = Integer.parseInt(sc.nextLine());

matrix1 = new int[row][column];
matrix2 = new int[row][column];
sum = new int[row][column];
System.out.println("Enter the data for 1st matrix :");

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

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

matrix1[i][j] = sc.nextInt();

}

}

// 2nd 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] = sc.nextInt();
}
}
}

void display() {

System.out.println("\n The 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\n The 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++) {

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

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

Posted On : 2016-07-14 15:50:18
Satisfied : 0 Yes  1 No
profile MOHAMMAD SALEEM BASHA - anyforum.in MOHAMMAD SALEEM BASHA
266150
Reply This Thread
up-rate
3
down-rate
Comments
Incomplete solution, and as per the requirement no need to print input matrices.
profile Rishi Kumar - anyforum.in Rishi Kumar
523  1854  18912
Posted On :2016-07-18 17:58:53.0
Leave a Comment

import java.util.*;
import java.io.*;

public class Matrix
{

public static void main(String[] ar)throws IOException
{

int m1[][] = new int[5][5];
int m2[][] = new int[5][5];
int m3[][] = new int[5][5];

Scanner sc = new Scanner(System.in);

System.out.print("Number of row: ");
int row = sc.nextInt();
System.out.print("Number of column: ");
int col = sc.nextInt();

System.out.println();

for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
System.out.print("Enter element "+(i+1)+":"+(j+1)+" of first table = ");
m1[i][j] = sc.nextInt();
}
}

System.out.println("\n Table 1:");
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
System.out.print(m1[i][j]+" ");
}
System.out.println();
}

System.out.println();
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
System.out.print("Enter element "+(i+1)+":"+(j+1)+" of second table = ");
m2[i][j] = sc.nextInt();
}
}

System.out.println("\n Table 2:");
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
System.out.print(m2[i][j]+" ");
}
System.out.println();
}

System.out.println("\n Added Matrix: ");
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
m3[i][j] = m1[i][j] + m2[i][j];
System.out.print(m3[i][j]+" ");
}
System.out.println();
}

System.out.println("\n Subtracted Matrix: ");
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
m3[i][j] = m1[i][j] - m2[i][j];

System.out.print(m3[i][j]+" ");
}
System.out.println();
}

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("\n Play again?[Y/N]\n");
String answer = br.readLine();

if(answer == "Y" || answer == "y")
{
System Matrix;
}
else
{
System.exit(0);
}
}
}



Posted On : 2016-07-15 09:11:30
Satisfied : 1 Yes  1 No
profile MOHAMMAD SALEEM BASHA - anyforum.in MOHAMMAD SALEEM BASHA
266150
Reply This Thread
up-rate
3
down-rate
Comments
Ohh.. Here you again posted the answer with full solution. But the input taking strategy is quite good in first answer.
profile Rishi Kumar - anyforum.in Rishi Kumar
523  1854  18912
Posted On :2016-07-18 18:03:24.0
Leave a Comment

import java.util.Scanner;
class SumSubmatrices
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int[][] mat1=new int[3][3];
int[][] mat2=new int[3][3];
int[][] mat3=new int[3][3];

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
mat1[i][j]=s.nextInt();
}
System.out.println();
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
mat2[i][j]=s.nextInt();
}
System.out.println();
}
System.out.println("::Output::");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
mat3[i][j]=mat1[i][j]+mat2[i][j];
System.out.print(mat3[i][j]+" ");
}
System.out.println();
}

System.out.println(" ");

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
mat3[i][j]=mat1[i][j]-mat2[i][j];
System.out.print(mat3[i][j]+" ");
}
System.out.println();
}


}
}

Posted On : 2016-07-18 17:05:18
Satisfied : 1 Yes  0 No
profile sudheerkumar Javvadi - anyforum.in sudheerkumar Javvadi
0920
Reply This Thread
up-rate
5
down-rate
Comments
Thanks sudheerkumar Javvadi,
This is the right way to program any problem. Whatever is given as sample i/p and o/p, nothing more than that should be on console. and you did exactly right.
profile Rishi Kumar - anyforum.in Rishi Kumar
523  1854  18912
Posted On :2016-07-18 18:13:14.0
Leave a Comment



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in