{"id":3876,"date":"2017-08-04T10:26:48","date_gmt":"2017-08-04T10:26:48","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=3876"},"modified":"2021-03-21T06:17:20","modified_gmt":"2021-03-21T06:17:20","slug":"write-a-java-program-to-add-two-matrices","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/write-a-java-program-to-add-two-matrices\/","title":{"rendered":"Write a Java program to Add Two Matrices"},"content":{"rendered":"<h3>Problem Description<\/h3>\n<p>In this post, we will write a sample java program to add two matrices. We would prompt the matrix dimensions and the matrix elements from the user. We will calculate the matrix sum as a third matrix and display the output in the console window. At the end of the post, we will display a sample output of the program.<\/p>\n<h3>Java Program<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package matrix;\r\n\r\nimport java.util.Scanner;\r\n\/\/ Demo program to add two matrices \r\n\/\/ www.TestingDocs.com\r\npublic class AddMatrixDemo {\r\n\r\n  public static void main(String args[]) {\r\n\r\n    Scanner in = new Scanner(System.in);\r\n    System.out.println(\"Please enter the number of Rows and Columns of Matrix:= \");\r\n\r\n    \/\/intialise rows of matrix as m\r\n    int m = in.nextInt();\r\n\r\n    \/\/intialise columns of matrix as n\r\n    int n = in.nextInt();\r\n\r\n    \/\/intialise the first matrix with m as rows and n as columns\r\n    int firstMatrix[][] = new int[m][n];\r\n\r\n    \/\/intialise the second matrix with m as rows and n as columns\r\n    int secondMatrix[][] = new int[m][n];\r\n\r\n    \/\/intialise the sum of two matrices with m as rows and n as columns\r\n    int sumOfMatrix[][] = new int[m][n];\r\n\r\n    System.out.println(\"Enter the elements of First Matrix:= \");\r\n    for (int i=0;i&lt;m;i++)\r\n      for (int j=0;j&lt;n;j++)\r\n        firstMatrix[i][j] = in.nextInt();\r\n\r\n    System.out.println(\"Enter the elements of Second Matrix:= \");\r\n    for (int i=0;i&lt;m;i++)\r\n      for (int j=0;j&lt;n;j++)\r\n        secondMatrix[i][j]=in.nextInt();\r\n\r\n    \/\/declare the elements of both matrices as i &amp; j\r\n    for (int i=0;i&lt;m;i++)\r\n      for (int j=0;j&lt;n;j++)\r\n        sumOfMatrix[i][j]=firstMatrix[i][j] + secondMatrix[i][j];\r\n\r\n    System.out.println(\"Sum of the Two Matrices: \");\r\n\r\n    for (int i=0;i&lt;m;i++)\r\n    {\r\n      for (int j=0;j&lt;n;j++) {\r\n        System.out.print(sumOfMatrix[i][j] + \" \");\r\n      }\r\n      System.out.println();\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<h3>Screenshot<\/h3>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3879\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Adding-two-matrix-Java-program.png\" alt=\"Adding two matrix Java program\" width=\"1740\" height=\"1021\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Adding-two-matrix-Java-program.png 1740w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Adding-two-matrix-Java-program-300x176.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Adding-two-matrix-Java-program-1024x601.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Adding-two-matrix-Java-program-768x451.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Adding-two-matrix-Java-program-1536x901.png 1536w\" sizes=\"auto, (max-width: 1740px) 100vw, 1740px\" \/><\/p>\n<h3>Sample program output<\/h3>\n<p>Please enter the number of Rows and Columns of Matrix:=<br \/>\n2 3<br \/>\nEnter the elements of First Matrix:=<br \/>\n3 7 9<br \/>\n6 4 5<br \/>\nEnter the elements of Second Matrix:=<br \/>\n4 7 9<br \/>\n2 1 6<br \/>\nSum of the Two Matrices:<br \/>\n7 14 18<br \/>\n8 5 11<\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-3876\" data-postid=\"3876\" class=\"themify_builder_content themify_builder_content-3876 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Problem Description In this post, we will write a sample java program to add two matrices. We would prompt the matrix dimensions and the matrix elements from the user. We will calculate the matrix sum as a third matrix and display the output in the console window. At the end of the post, we will [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-3876","post","type-post","status-publish","format-standard","hentry","category-java-programs","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3876","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=3876"}],"version-history":[{"count":3,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3876\/revisions"}],"predecessor-version":[{"id":19719,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3876\/revisions\/19719"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=3876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=3876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=3876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}