{"id":1362,"date":"2022-03-03T07:31:24","date_gmt":"2022-03-03T07:31:24","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1362"},"modified":"2022-03-03T07:31:24","modified_gmt":"2022-03-03T07:31:24","slug":"convert-binary-number-to-decimal-number","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/03\/convert-binary-number-to-decimal-number\/","title":{"rendered":"Convert Binary Number to Decimal Number"},"content":{"rendered":"\n<p>Binary numbers are numbers consisting only of&nbsp;<strong>2<\/strong>&nbsp;digits:&nbsp;<code>0<\/code>&nbsp;and&nbsp;<code>1<\/code>. They can be expressed in the base&nbsp;<code>2<\/code>&nbsp;numeral system. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10 (2), 1000 (8), 11001 (25)<\/code><\/pre>\n\n\n\n<p>Decimal numbers are numbers consisting of&nbsp;<strong>10<\/strong>&nbsp;digits:&nbsp;<code>0<\/code>,&nbsp;<code>1<\/code>,&nbsp;<code>2<\/code>,&nbsp;<code>3<\/code>,&nbsp;<code>4<\/code>,&nbsp;<code>5<\/code>,&nbsp;<code>6<\/code>,&nbsp;<code>7<\/code>,&nbsp;<code>8<\/code>,&nbsp;<code>9<\/code>. They can be expressed in the base&nbsp;<code>10<\/code>&nbsp;numeral system.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>18, 222, 987<\/code><\/pre>\n\n\n\n<p>Here, we will be writing a Java program that will convert a binary number into decimal and vice versa using built-in methods and custom methods.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Binary to Decimal Conversion Using Custom Method<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n    \n  public static void main(String&#91;] args) {\n\n    \/\/ binary number\n    long num = 110110111;\n\n    \/\/ call method by passing the binary number\n    int decimal = convertBinaryToDecimal(num);\n\n    System.out.println(\"Binary to Decimal\");\n    System.out.println(num + \" = \" + decimal);\n  }\n\n  public static int convertBinaryToDecimal(long num) {\n    int decimalNumber = 0, i = 0;\n    long remainder;\n    \n    while (num != 0) {\n      remainder = num % 10;\n      num \/= 10;\n      decimalNumber += remainder * Math.pow(2, i);\n      ++i;\n    }\n    \n    return decimalNumber;\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>110110111 in binary = 439 in decimal<\/samp><\/code><\/pre>\n\n\n\n<p>Here&#8217;s how the above program works:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/cdn\/farfuture\/gu8RAKQ1w7pmppUO73Ka3ngRe48gMhTxgjqIsldSFYI\/mtime:1620213933\/sites\/tutorial2program\/files\/binary-to-decimal-conversion.png\" alt=\"Converting Binary Number to Decimal\" title=\"Binary to Decimal Conversion\"\/><figcaption>Binary to Decimal Conversion<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Binary numbers are numbers consisting only of&nbsp;2&nbsp;digits:&nbsp;0&nbsp;and&nbsp;1. They can be expressed in the base&nbsp;2&nbsp;numeral system. For example, Decimal numbers are numbers consisting of&nbsp;10&nbsp;digits:&nbsp;0,&nbsp;1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5,&nbsp;6,&nbsp;7,&nbsp;8,&nbsp;9. They can be expressed in the base&nbsp;10&nbsp;numeral system. Here, we will be writing a Java program that will convert a binary number into decimal and vice versa using built-in methods and custom [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[490],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1362"}],"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=1362"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1362\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}