Android Tutorial , Programming Tutorial, Php Tutorial, Learn Android, HTML Tutorial, Coding , Java Tutorial, GTU Programs, Learning Programming

Monday 5 September 2016

Write a program to perform matrix addition operation in java

In this program, Take the row and column value from the user and perform addition of the matrix. User can add more than one row and one column. Here explain the code and display the output of the program in given below.

Step of Execute Program :

1) Open Command prompt
2) Set path
3) Compile File // javac matrix.java
4) Run File // java matrix
5) Fill the Details


File Name : matrix.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.io.*;
class matrix
{
 public static void main(String args[])throws Exception
 {
  int n,i,j,r,c;
  
  DataInputStream dis=new DataInputStream(System.in);

   System.out.println();
  System.out.println();
  System.out.print("enter The Number Of rows :-    ");
  r=Integer.parseInt(dis.readLine());

   System.out.print("enter The Number Of Column :-    ");
  c=Integer.parseInt(dis.readLine());

   int arr1[][]=new int[r][c];
  int arr2[][]=new int[r][c];
  int arr3[][]=new int[r][c];

   System.out.println();
  System.out.println();

   for(i=0;i<r;i++)
  {
   for(j=0;j<c;j++)
   {
    System.out.print("Enter number for First Matrix [ "+(i)+" ] [

"+(j)+" ]    :-   ");
    arr1[i][j]=Integer.parseInt(dis.readLine());
   }
 
  }

   System.out.println();

   for(i=0;i<r;i++)
  {
   for(j=0;j<c;j++)
   {
    System.out.print("Enter number for Second Matrix [ "+(i)+" ] [

"+(j)+" ]    :-   ");
    arr2[i][j]=Integer.parseInt(dis.readLine());
   }
 
  }

   System.out.println();

   for(i=0;i<r;i++)
  {
   for(j=0;j<c;j++)
   {
    arr3[i][j]=arr1[i][j]+arr2[i][j];
   }
 
  }

   for(i=0;i<r;i++)
  {
   for(j=0;j<c;j++)
   {
    System.out.println("Addition of Two Matrix is  [ "+(i)+" ] [ "+(j)+"

]    :-   "+arr3[i][j]);
   }
  }
  System.out.println();
 }
}



Output :

Matrix Addition in Java

0 comments:

Post a Comment

Like us on Facebook

Site Visitor

Powered by Blogger.