Introduction

    When working with numbers in different systems, it’s common to need to convert a decimal number into other number systems such as binary, octal, or hexadecimal. These conversions are essential in various fields, including computer science, digital electronics, and software development. In this blog post, we'll explore a simple Java program that converts a decimal number to its binary, octal, and hexadecimal equivalents.

 

Understanding Number Systems

Before diving into the code, let's briefly review the number systems we'll be working with:

1. Binary (Base 2): Represents numbers using only two digits: 0 and 1. It's the foundational system for digital electronics and computer systems.

2. Octal (Base 8): Uses eight digits (0-7) and is often used in computing as a more compact representation of binary numbers.

3. Hexadecimal (Base 16): Uses sixteen symbols, 0-9 and A-F, where A-F represent the decimal values 10-15. This system is widely used in programming, especially in dealing with memory addresses and color codes.

        
        import java.io.*;
        import java.util.*;
        
        public class base {
            public static void main(String args[]) throws IOException {
                DataInputStream s = new DataInputStream(System.in);
                String ans;
                System.out.println("Enter the decimal value needed to convert:");
                int dec = Integer.parseInt(s.readLine());
        
                System.out.println("Select base\n binary-b;\n octal-o;\n hexadecimal-h;");
                char choice = (char) System.in.read();
        
                switch (choice) {
                    case 'b':
                        ans = Integer.toString(dec, 2);
                        System.out.println("Binary value of " + dec + " = " + ans);
                        break;
                    case 'o':
                        ans = Integer.toString(dec, 8);
                        System.out.println("Octal value of " + dec + " = " + ans);
                        break;
                    case 'h':
                        ans = Integer.toString(dec, 16);
                        System.out.println("Hexadecimal value of " + dec + " = " + ans);
                        break;
                    default:
                        System.out.println("Wrong option!");
                        break;
                }
            }
        }                        
        
        


Output


        Enter the decimal value needed to convert:
        4
        Select base
            binary-b;
            octal-o;
            hexadecimal-h;
        b
        Binary value of 4 = 100
        


   

Explanation

1. User Input:
The program begins by prompting the user to enter a decimal number. It then asks the user to select the base (binary, octal, or hexadecimal) for the conversion.
2. Reading and Parsing Input:
The decimal number is read as a string and then parsed into an integer using Integer.parseInt().
3. Switch Case for Base Selection:
The user’s choice of base is captured, and a switch statement is used to determine which conversion to 4. perform:
For binary, Integer.toString(dec, 2) converts the decimal to binary.
For octal, Integer.toString(dec, 8) converts the decimal to octal.
For hexadecimal, Integer.toString(dec, 16) converts the decimal to hexadecimal.
5. Output:
The program outputs the converted value based on the user’s selection.
6. Default Case:
If the user enters an invalid option, the program displays a "Wrong option!" message.


Conclusion

     This Java program offers a straightforward way to convert a decimal number into binary, octal, or hexadecimal formats. Understanding these conversions is essential for anyone working in fields related to computer science or electronics. The program's flexibility and simplicity make it a great tool for learning and practicing number system conversions.

Feel free to experiment with different numbers and conversions to get a better understanding of how these systems work. Happy coding!