{"id":1286,"date":"2019-08-22T06:13:47","date_gmt":"2019-08-22T06:13:47","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1286"},"modified":"2024-12-07T14:58:09","modified_gmt":"2024-12-07T14:58:09","slug":"write-a-simple-java-program-to-add-two-numbers","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/write-a-simple-java-program-to-add-two-numbers\/","title":{"rendered":"Write a simple java program to add two numbers [ 2024 ]"},"content":{"rendered":"\r\n<h1>Write a simple Java program to add two numbers<\/h1>\r\n<p>In this tutorial, we will write a simple Java program to add two numbers and display the result on the screen.<\/p>\r\n<h2><strong>Problem statement<\/strong><\/h2>\r\n<ul>\r\n<li>Write a simple Java program to add two integers.<\/li>\r\n<li>Run the program with one positive test case and one negative test case.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<h3>Tools used<\/h3>\r\n<ul>\r\n<li>JDK(Java Development Kit)<\/li>\r\n<li>We can use an IDE like IntelliJ IDEA or Eclipse\u00a0<\/li>\r\n<li>Windows 10<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\"><strong>Flow Chart<\/strong><\/h2>\r\n\r\n\r\n\r\n<p>A flow chart for the program depicts the steps involved.<\/p>\r\n\r\n\r\n\r\n<p>Start<br \/>Take input two numbers a,b<br \/>Compute the sum sum = a + b<br \/>Display the sum<br \/>Terminate<\/p>\r\n<p>&nbsp;<\/p>\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1210\" title=\"Add Two Numbers Flow Chart\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Add-Two-Numbers-Flow-Chart.png\" alt=\"Add Two Numbers Flow Chart\" width=\"894\" height=\"624\" \/><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\"><strong>\u00a0<br \/><\/strong><\/h2>\r\n<h2>IDE Setup<\/h2>\r\n<p>Some steps and instructions to create a Java application on Eclipse IDE:<\/p>\r\n<ul>\r\n<li>Create Java Project in Eclipse<\/li>\r\n<\/ul>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/create-a-new-java-project-in-eclipse\/\">https:\/\/www.testingdocs.com\/create-a-new-java-project-in-eclipse\/<\/a><\/p>\r\n<ul>\r\n<li>Create Java Package in Eclipse<\/li>\r\n<\/ul>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/create-java-package-in-eclipse-ide\/\">https:\/\/www.testingdocs.com\/create-java-package-in-eclipse-ide\/<\/a><\/p>\r\n<ul>\r\n<li>Create Java Class in Eclipse<\/li>\r\n<\/ul>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/create-a-new-java-class-in-a-project\/\">https:\/\/www.testingdocs.com\/create-a-new-java-class-in-a-project\/<\/a><\/p>\r\n<ul>\r\n<li>Run Java Project in Eclipse<\/li>\r\n<\/ul>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/run-java-project-in-eclipse\/\">https:\/\/www.testingdocs.com\/run-java-project-in-eclipse\/<\/a><\/p>\r\n<h2><strong>Java Program<\/strong><\/h2>\r\n<p>The program prompts the user to enter two numbers. (a,b) The program computes the sum of the two numbers and displays the result to the screen. The program uses the Scanner class to take input from the keyboard which is the standard input device.\u00a0<\/p>\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code class=\"\" data-line=\"\">import java.util.Scanner;\n\npublic class AddTwoNumbers {\n\n    public static void main(String args[]) {\n        Scanner input = null;\n        int a,b, sum;\n        try {\n            input = new Scanner(System.in);\n            System.out.print(&quot;Enter a:&quot;);\n            a = Integer.parseInt(input.nextLine());\n\n            System.out.print(&quot;Enter b:&quot;);\n            b = Integer.parseInt(input.nextLine());\n\n            sum = a + b ;\n            System.out.print(&quot;Sum =&quot; + sum );\n\n        } catch(Exception e)\n        {\n            e.printStackTrace();\n            System.out.println(&quot;Error!&quot;);\n        }\n        finally\n        {\n            if (input != null) {\n                input.close();\n            }\n        }\n    }\n}<\/code><\/pre>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\"><strong>Sample Run output<\/strong><\/h2>\r\n<p>Save the program in the IDE.\u00a0<\/p>\r\n<p>Run the program and execute test cases to verify the result of the program.\u00a0<\/p>\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><img decoding=\"async\" class=\"wp-image-1211\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Add-two-numbers-java-program.png\" alt=\"Add two numbers java program\" title=\"\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\"><strong>Positive testcase<\/strong><\/h2>\r\n\r\n\r\n\r\n<p>Enter a: 25<br \/>Enter b: 50<br \/>Sum = 75<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\"><strong>Negative testcase<\/strong><\/h2>\r\n\r\n\r\n\r\n<p>Enter a: invalid input<\/p>\r\n\r\n\r\n\r\n<p>java.lang.NumberFormatException: For input string: \u201d invalid input\u201d<br \/>at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)<br \/>at java.lang.Integer.parseInt(Integer.java:569)<br \/>at java.lang.Integer.parseInt(Integer.java:615)<br \/>at com.testingdocs.simpleproject.AddTwoNumbers.main(AddTwoNumbers.java:13)<br \/>Error!<\/p>\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n","protected":false},"excerpt":{"rendered":"<p>Write a simple Java program to add two numbers : In this tutorial, we will write a simple Java program<\/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-1286","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\/1286","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=1286"}],"version-history":[{"count":14,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1286\/revisions"}],"predecessor-version":[{"id":26283,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1286\/revisions\/26283"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}