{"id":1376,"date":"2022-03-03T19:08:17","date_gmt":"2022-03-03T19:08:17","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1376"},"modified":"2022-03-03T19:08:17","modified_gmt":"2022-03-03T19:08:17","slug":"program-to-add-two-binary-numbers","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/03\/program-to-add-two-binary-numbers\/","title":{"rendered":"Program to Add two Binary Numbers"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Example: Adding binary numbers in Java<\/h4>\n\n\n\n<p>In this program we are using Scanner to get the\u00a0input from user\u00a0(user enters the two binary numbers that we need to add) and then we are adding them bit by bit using\u00a0while loop\u00a0and storing the result in an array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Scanner;\npublic class JavaExample {\n   public static void main(String&#91;] args)\n   {\n\t\/\/Two variables to hold two input binary numbers\t \n\tlong b1, b2;\n\tint i = 0, carry = 0;\n\n\t\/\/This is to hold the output binary number\n\tint&#91;] sum = new int&#91;10];\n\n\t\/\/To read the input binary numbers entered by user\n\tScanner scanner = new Scanner(System.in);\n\n\t\/\/getting first binary number from user\n\tSystem.out.print(\"Enter first binary number: \");\n\tb1 = scanner.nextLong();\n\t\/\/getting second binary number from user\n\tSystem.out.print(\"Enter second binary number: \");\n\tb2 = scanner.nextLong();\n\n\t\/\/closing scanner after use to avoid memory leak\n\tscanner.close();\n\twhile (b1 != 0 || b2 != 0) \n\t{\n\t\tsum&#91;i++] = (int)((b1 % 10 + b2 % 10 + carry) % 2);\n\t\tcarry = (int)((b1 % 10 + b2 % 10 + carry) \/ 2);\n\t\tb1 = b1 \/ 10;\n\t\tb2 = b2 \/ 10;\n\t}\n\tif (carry != 0) {\n\t\tsum&#91;i++] = carry;\n\t}\n\t--i;\n\tSystem.out.print(\"Output: \");\n\twhile (i &gt;= 0) {\n\t\tSystem.out.print(sum&#91;i--]);\n\t}\n\tSystem.out.print(\"\\n\");  \n   }\n}<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter first binary number: 11100\nEnter second binary number: 10101\nOutput: 110001<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Example: Adding binary numbers in Java In this program we are using Scanner to get the\u00a0input from user\u00a0(user enters the two binary numbers that we need to add) and then we are adding them bit by bit using\u00a0while loop\u00a0and storing the result in an array. Output:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[260],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1376"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1376"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1376\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}