Write a Java program to Decimal to Binary Conversion
The code converts a decimal number to its binary representation. Let's go through the code step by step:
- The code starts with the import statements import java.util.Stack; and import java.util.Scanner;. These import the Stack class from the java.util package and the Scanner class from the java.util package, respectively.
- The DecimalToBinary class is defined, serving as the entry point of the program.
- In the main method, a Scanner object named input is created to read input from the user.
- The user is prompted to enter a decimal number by using the System.out.print statement.
- The user's input is read and stored in the dec_num variable using the nextInt method of the Scanner class.
- The entered decimal number is printed using the System.out.println statement.
- The convertToBinary method is called, passing the dec_num as an argument. The return value of the convertToBinary method is stored in the binaryNumber variable.
- The binary representation of the decimal number is printed using the System.out.println statement by concatenating the string "Binary Number : " with the value of the binaryNumber variable.
- The convertToBinary method is defined, which takes an integer (dec_num) as a parameter and returns a string representation of the binary number.
- If the decimal number is equal to 0, the method returns "0" as the binary representation.
- Inside the method, a stack object named stack is created using generics, specifying that it will contain integers (Integer).
- A while loop is used to convert the decimal number to binary. It iteratively divides the decimal number by 2 and pushes the remainder (0 or 1) onto the stack. The loop continues until the decimal number becomes 0.
- A StringBuilder object named binary is created to store the binary representation.
- Another while loop is used to pop the elements from the stack until it is empty. Each popped element (0 or 1) is appended to the binary using the append method.
- Finally, the method returns the binary representation as a string by converting the binary to a string using the toString method of the StringBuilder class.
Source Code
import java.util.Stack;
import java.util.Scanner;
public class DecimalToBinary
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the Decimal Number : ");
int dec_num = input.nextInt();
System.out.println("Decimal Number : " + dec_num);
String binaryNumber = convertToBinary(dec_num);
System.out.println("Binary Number : " + binaryNumber);
}
public static String convertToBinary(int dec_num)
{
if (dec_num == 0)
{
return "0";
}
Stack<Integer> stack = new Stack<>();
while (dec_num > 0)
{
int remainder = dec_num % 2;
stack.push(remainder);
dec_num /= 2;
}
StringBuilder binary = new StringBuilder();
while (!stack.isEmpty())
{
binary.append(stack.pop());
}
return binary.toString();
}
}
Output
Enter the Decimal Number : 23
Decimal Number : 23
Binary Number : 10111