{"id":1370,"date":"2017-03-12T12:14:37","date_gmt":"2017-03-12T12:14:37","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1370"},"modified":"2024-12-14T04:31:21","modified_gmt":"2024-12-14T04:31:21","slug":"write-a-java-program-to-compute-multiplication-table","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/write-a-java-program-to-compute-multiplication-table\/","title":{"rendered":"Write a java program to compute multiplication table?"},"content":{"rendered":"<h2>Write a Java program to compute the Multiplication table.<\/h2>\n<p>Write a Java program to compute multiplication tables. Consider the following multiplication table, which we denote it by M:<\/p>\n<p>Multiplication table function: M[i][j] = i*j<\/p>\n<p>where 1&lt;= (i,j) &lt;=n<\/p>\n<p>Run the program and test your program with some testcases.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1394\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Multiplication-Table.png\" alt=\"Multiplication Table\" width=\"1008\" height=\"567\" title=\"\"><\/p>\n<h2><strong>Java Program<\/strong><\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import java.util.InputMismatchException;\r\nimport java.util.Scanner;\r\n\r\npublic class MultiplicationTable {\r\n    public static void main(String[] args) {\r\n        int n;\r\n        Scanner input = null;\r\n        try {\r\n            input = new Scanner(System.in);\r\n            System.out.println(\"Enter size of the array:\");\r\n            n = input.nextInt();\r\n\r\n            if (n &gt; 0) {\r\n                int[][] a = computeTable(n);\r\n                printTable(a);\r\n            } else {\r\n                System.out.println(\"Enter positive number.\");\r\n                System.exit(0);\r\n            }\r\n\r\n        } catch (InputMismatchException ime) {\r\n            System.out.println(\"Not a valid input\");\r\n        } catch (Exception e) {\r\n            e.printStackTrace();\r\n        } finally {\r\n            if (input != null) {\r\n                input.close();\r\n            }\r\n        }\r\n    }\r\n\r\n    static int[][] computeTable(int n) {\r\n        int[][] a = new int[n][n];\r\n        for (int i = 0; i &lt; n; i++)\r\n            for (int j = 0; j &lt; n; j++)\r\n                a[i][j] = (i+1)*(j+1);\r\n        return a;\r\n    }\r\n\r\n    static void printTable(int[][] a) {\r\n        for (int i = 0; i &lt; a.length; i++) {\r\n            for (int j = 0; j &lt; a.length; j++)\r\n                printWithSpace(a[i][j],10);\r\n            System.out.println();\r\n        }\r\n    }\r\n    static void printWithSpace(int n, int z) {\r\n        String s = \"\" + n, space = \"                    \";\r\n        int l = s.length();\r\n        System.out.print(space.substring(0, z-l) + s);\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h3><strong>Run output<\/strong><\/h3>\n<h3><strong>TestCase: Happy paths <\/strong><\/h3>\n<p>Enter size of the array:<br \/>\n5<\/p>\n<p>1\u00a0\u00a0\u00a0\u00a0\u00a0 2\u00a0\u00a0\u00a0\u00a0 3\u00a0\u00a0\u00a0\u00a0 4\u00a0\u00a0\u00a0\u00a0\u00a0 5<br \/>\n2\u00a0\u00a0\u00a0\u00a0\u00a0 4\u00a0\u00a0\u00a0\u00a0 6\u00a0\u00a0\u00a0\u00a0 8\u00a0\u00a0\u00a0 10<br \/>\n3\u00a0\u00a0\u00a0\u00a0\u00a0 6\u00a0\u00a0\u00a0\u00a0 9\u00a0\u00a0\u00a0 12\u00a0\u00a0 15<br \/>\n4\u00a0\u00a0\u00a0\u00a0\u00a0 8\u00a0\u00a0 12\u00a0\u00a0\u00a0 16 \u00a0\u00a0 20<br \/>\n5\u00a0\u00a0\u00a0 10\u00a0\u00a0 15 \u00a0\u00a0 20\u00a0\u00a0\u00a0 25<\/p>\n<p><strong>Run output 2<\/strong><\/p>\n<p>Enter size of the array:<br \/>\n10<br \/>\n1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2\u00a0\u00a0\u00a0\u00a0\u00a0 3\u00a0\u00a0\u00a0\u00a0 4\u00a0\u00a0 \u00a0 5\u00a0\u00a0 \u00a0 \u00a0 6\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 8\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 9\u00a0\u00a0\u00a0\u00a0\u00a0 10<br \/>\n2\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 4\u00a0\u00a0\u00a0\u00a0\u00a0 6\u00a0\u00a0 \u00a0 8\u00a0\u00a0 10\u00a0\u00a0\u00a0\u00a0 12\u00a0\u00a0\u00a0\u00a0\u00a0 14\u00a0\u00a0\u00a0\u00a0 16\u00a0\u00a0\u00a0\u00a0 18\u00a0 \u00a0\u00a0 20<br \/>\n3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 6\u00a0\u00a0\u00a0\u00a0\u00a0 9\u00a0\u00a0 12\u00a0\u00a0 15\u00a0\u00a0\u00a0\u00a0 18\u00a0\u00a0\u00a0\u00a0\u00a0 21\u00a0\u00a0\u00a0\u00a0 24\u00a0\u00a0\u00a0\u00a0\u00a0 27\u00a0\u00a0\u00a0 30<br \/>\n4\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 8\u00a0\u00a0\u00a0 12\u00a0\u00a0 16\u00a0\u00a0\u00a0 20\u00a0\u00a0\u00a0\u00a0 24\u00a0\u00a0\u00a0\u00a0 28\u00a0\u00a0\u00a0\u00a0 32\u00a0\u00a0\u00a0\u00a0\u00a0 36 \u00a0\u00a0 40<br \/>\n5\u00a0\u00a0\u00a0\u00a0 10\u00a0\u00a0\u00a0 15\u00a0\u00a0 20\u00a0\u00a0\u00a0 25\u00a0\u00a0\u00a0\u00a0 30\u00a0\u00a0\u00a0\u00a0 35\u00a0\u00a0\u00a0\u00a0 40\u00a0\u00a0\u00a0\u00a0\u00a0 45\u00a0\u00a0\u00a0 50<br \/>\n6\u00a0\u00a0\u00a0\u00a0 12\u00a0\u00a0\u00a0 18\u00a0\u00a0 24\u00a0\u00a0\u00a0 30\u00a0\u00a0\u00a0\u00a0 36\u00a0\u00a0\u00a0\u00a0 42\u00a0\u00a0\u00a0\u00a0 48\u00a0\u00a0\u00a0\u00a0\u00a0 54\u00a0\u00a0\u00a0 60<br \/>\n7\u00a0\u00a0\u00a0\u00a0 14\u00a0\u00a0\u00a0 21\u00a0\u00a0 28\u00a0\u00a0\u00a0 35\u00a0\u00a0\u00a0\u00a0 42\u00a0\u00a0\u00a0\u00a0 49\u00a0\u00a0\u00a0\u00a0 56\u00a0\u00a0\u00a0\u00a0\u00a0 63\u00a0\u00a0\u00a0 70<br \/>\n8\u00a0\u00a0\u00a0\u00a0 16\u00a0\u00a0\u00a0 24\u00a0\u00a0 32\u00a0\u00a0\u00a0 40\u00a0\u00a0\u00a0\u00a0 48\u00a0\u00a0\u00a0\u00a0 56 \u00a0 \u00a0 64 \u00a0 \u00a0\u00a0 72\u00a0\u00a0\u00a0 80<br \/>\n9\u00a0\u00a0\u00a0\u00a0 18\u00a0\u00a0\u00a0 27\u00a0\u00a0 36 \u00a0\u00a0 45\u00a0\u00a0\u00a0\u00a0 54\u00a0\u00a0\u00a0\u00a0 63\u00a0\u00a0\u00a0\u00a0 72\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 81 \u00a0 90<br \/>\n10\u00a0\u00a0\u00a0 20\u00a0\u00a0 30\u00a0\u00a0 40\u00a0\u00a0\u00a0 50\u00a0\u00a0\u00a0\u00a0 60\u00a0\u00a0\u00a0\u00a0 70\u00a0\u00a0\u00a0\u00a0 80\u00a0\u00a0\u00a0\u00a0\u00a0 90\u00a0\u00a0 100<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1403\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Multiplication-table-java-program.png\" alt=\"Multiplication table java program\" width=\"1319\" height=\"611\" title=\"\"><\/p>\n<h3>Testcase : Unhappy paths<\/h3>\n<p>Enter size of the array:<br \/>\n0<br \/>\nEnter positive number.<\/p>\n<h3>Another run<\/h3>\n<p>Enter size of the array:<br \/>\ntable<br \/>\nNot a valid input<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write a Java program to compute the Multiplication table. Write a Java program to compute multiplication tables. Consider the following multiplication table, which we denote it by M: Multiplication table function: M[i][j] = i*j where 1&lt;= (i,j) &lt;=n Run the program and test your program with some testcases. Java Program import java.util.InputMismatchException; import java.util.Scanner; public [&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":[24],"class_list":["post-1370","post","type-post","status-publish","format-standard","hentry","category-java-programs","tag-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\/1370","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=1370"}],"version-history":[{"count":4,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1370\/revisions"}],"predecessor-version":[{"id":26415,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1370\/revisions\/26415"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}