Skip to content

Commit e5d6515

Browse files
authored
Add files via upload
1 parent 222949a commit e5d6515

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

Assignment-9/AddTwoMatrix.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* PROGRAM : To add two matrices using your own method. Print the resultant along with two input matrices.
3+
* FILE : AddTwoMatrix.java
4+
* CREATED BY : Santosh Hembram
5+
* DATE : 12-10-20
6+
*/
7+
import java.util.*;
8+
class Matrix{
9+
public int[][] addMatrix(int arr[][],int arr2[][],int r,int c) {
10+
11+
int addResult[][] = new int[r][c];
12+
13+
for(int i=0; i<r; i++) {
14+
for (int j=0; j<c; j++) {
15+
16+
addResult[i][j] = arr[i][j] + arr2[i][j];
17+
}
18+
}
19+
return addResult;
20+
}
21+
}
22+
class AddTwoMatrix {
23+
public static void main(String[] args) {
24+
Scanner sc = new Scanner(System.in);
25+
System.out.print("Enter the row size: ");
26+
int r = sc.nextInt();
27+
System.out.print("Enter the coloumn size: ");
28+
int c = sc.nextInt();
29+
int arr[][] = new int[r][c];
30+
31+
System.out.println("---------- Enter the elements of the 1st array --------------");
32+
for(int i=0; i<r; i++) {
33+
for (int j=0; j<c; j++) {
34+
35+
System.out.print("Enter the elements for row "+i+" coloumn "+j+": ");
36+
arr[i][j] = sc.nextInt();
37+
}
38+
}
39+
int arr2[][] = new int[r][c];
40+
System.out.println("--------- Enter the elements of the 2nd array --------------");
41+
for(int k=0; k<r; k++) {
42+
for (int l=0; l<c; l++) {
43+
44+
System.out.print("Enter the elements for row "+k+" coloumn "+l+": ");
45+
arr2[k][l] = sc.nextInt();
46+
}
47+
}
48+
System.out.println("------- Displaying the 1st Matrix ----------");
49+
for( int i=0; i<r; i++) {
50+
for ( int j=0; j<c; j++) {
51+
System.out.print(arr[i][j]+" ");
52+
53+
}
54+
System.out.println();
55+
56+
}
57+
System.out.println("------- Displaying the 2nd Matrix ----------");
58+
for( int k=0; k<r; k++) {
59+
for ( int l=0; l<c; l++) {
60+
System.out.print(arr2[k][l]+" ");
61+
62+
}
63+
System.out.println();
64+
65+
}
66+
67+
Matrix obj = new Matrix();
68+
int result[][] = obj.addMatrix(arr,arr2,r,c);
69+
System.out.println("--------- After adding the two Matrix -------------");
70+
for(int k=0; k<r; k++) {
71+
for (int l=0; l<c; l++) {
72+
System.out.print(result[k][l]+" ");
73+
74+
}
75+
System.out.println();
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy