Given a binary number as input, convert it into its equivalent decimal number.
Definition:
A binary number uses only two digits, 0 and 1, while a decimal number uses digits from 0 to 9. To convert binary to decimal, we multiply each binary digit by its corresponding power of 2 and add all the values.
Example:
Binary Number: 1011
Decimal Conversion:
1 × 2³ + 0 × 2² + 1 × 2¹ + 1 × 2⁰
= 8 + 0 + 2 + 1
= 11
So, the decimal value of 1011 is 11.
Example Explanation:
We start from the rightmost digit and assign powers of 2 starting from 0. Each binary digit is multiplied by its corresponding power of 2, and all the results are added to get the decimal value.
Code Explanation:
First, we take the binary number as input. We extract each digit using the modulo operator and calculate its value using powers of 2. The power is doubled after every digit, and the processed digit is removed using integer division. The program also checks for invalid binary digits and handles negative input.
Time Complexity: O(log n)
Space Complexity: O(1)
If you found this video helpful, like the video, share it with your friends, and subscribe to OLD TIME LEARNINGS for more Java programming and placement preparation videos.
On this page of the site you can watch the video online Binary To Decimal in JAVA with a duration of hours minute second in good quality, which was uploaded by the user Old Time Learnings 10 August 2026, share the link with friends and acquaintances, this video has already been watched 14 times on youtube and it was liked by 8 viewers. Enjoy your viewing!