Thursday, February 16, 2012

Tutorial: Decimal to Binary Converter in Java

Today, we will try to create a Java program that enables the user input a DECIMAL number then it will convert to its BINARY equivalent value. If you remember, I posted a tutorial about a Java program that converts a decimal number to its octal value (you can read here). There are lot of sites from the Internet that you can download the program source code but in this tutorial, I made my own code which is based from the rules in number system conversion.

To do this, we will again follow the rules in converting. Here are the rules in converting decimal to binary:

1. Get or write the whole decimal number and serve as the dividend.
2. Use the base of the Binary, which is 2 as the divisor.
3. Proceed to Division operation.
4. The quotient and the remainder will be written on the other side whether 0 or 1. If the quotient is a whole number, the write 0 in the remainder, else if the quotient is a decimal number, then write 1.
5. Get again the quotient and divide into its base, repeat step # 4.
6. Repeat step # 5 until the quotient will be zero.
7. Write the remainder from bottom to up, to form the binary equivalent.

The Output:

 

Now, as you can see the figure above, the user enters the Decimal number 25, then the program provides a number conversion simulation table that explains the process or concepts of the rules stated above. Rule # 7 indicates the final binary equivalent of the decimal number where the program must read the remainder from the bottom to top.

Like Us on Facebook

Related Posts Plugin for WordPress, Blogger...