{"id":5239,"date":"2018-01-22T23:55:25","date_gmt":"2018-01-22T18:25:25","guid":{"rendered":"https:\/\/java2blog.com\/?p=5239"},"modified":"2023-11-25T08:39:31","modified_gmt":"2023-11-25T03:09:31","slug":"java-program-add-two-matrices","status":"publish","type":"post","link":"https:\/\/java2blog.com\/java-program-add-two-matrices\/","title":{"rendered":"Java program to add two matrices"},"content":{"rendered":"<p>In this post, we will see how to add two matrices in java.I am giving example for 3<em>3 matrix. You can extend it to n<\/em>n matrices.<\/p>\n<pre class=\"java\" name=\"code\">\nimport java.util.Scanner;\n\npublic class MatrixOperator\n{\n   public static void main(String args[])\n   {\n       int i, j;\n       int mat1[][] = new int[3][3];\n       int mat2[][] = new int[3][3];\n       int res[][] = new int[3][3];\n       Scanner scanner = new Scanner(System.in);\n\n       System.out.print(\"Enter matrix 1 elements : \");\n       for(i=0; i&lt;3; i++){\n           for(j=0; j&lt;3; j++){\n               mat1[i][j] = scanner.nextInt();\n           }\n       }\n\n       System.out.print(\"Enter matrix 2 elements : \");\n       for(i=0; i&lt;3; i++){\n           for(j=0; j&lt;3; j++){\n               mat2[i][j] = scanner.nextInt();\n           }\n       }\n\n       for(i=0; i&lt;3; i++){\n           for(j=0; j&lt;3; j++){\n               res[i][j] = mat1[i][j] + mat2[i][j];\n           }\n       }\n\n       System.out.print(\"The new matrix is :\\n\");\n       for(i=0; i&lt;3; i++){\n           for(j=0; j&lt;3; j++){\n               System.out.print(res[i][j]+ \" \");\n           }\n           System.out.println();\n       }\n   }\n}\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\nEnter matrix 1 elements : 11 22 33 44 55 66 77 88 99<br \/>\nEnter matrix 2 elements : 10 20 30 40 50 60 70 80 90<br \/>\nThe new matrix is :<br \/>\n21 42 63<br \/>\n84 105 126<br \/>\n147 168 189\n<\/div>\n<p>That&#8217;s all about adding two matrices.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, we will see how to add two matrices in java.I am giving example for 33 matrix. You can extend it to nn matrices. import java.util.Scanner; public class MatrixOperator { public static void main(String args[]) { int i, j; int mat1[][] = new int[3][3]; int mat2[][] = new int[3][3]; int res[][] = new [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[151],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/5239"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=5239"}],"version-history":[{"count":1,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/5239\/revisions"}],"predecessor-version":[{"id":25662,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/5239\/revisions\/25662"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=5239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=5239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=5239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}