- We can convert binary to decimal in three ways
1.Using Integer.toBinaryString(int num);
2.Using Stack
3.Using Custom logic
1.Write a Java Program to convert decimal to binary in java using Integer.toBinaryString(int num)
- import java.util.Scanner;
- public class ConvertDecimalToBinary{
- /**
- *www.instanceofjava.com
- */
- public static void main(String[] args) {
- System.out.println("\nBinary representation of 1: ");
- System.out.println(Integer.toBinaryString(1));
- System.out.println("\nBinary representation of 4: ");
- System.out.println(Integer.toBinaryString(4));
- System.out.println("Binary representation of 10: ");
- System.out.println(Integer.toBinaryString(10));
- System.out.println("\nBinary representation of 12: ");
- System.out.println(Integer.toBinaryString(12));
- System.out.println("\nBinary representation of 120: ");
- System.out.println(Integer.toBinaryString(120));
- System.out.println("\nBinary representation of 500: ");
- System.out.println(Integer.toBinaryString(500));
- }
- }
Output:
- Binary representation of 1:
- 1
- Binary representation of 4:
- 100
- Binary representation of 10:
- 1010
- Binary representation of 12:
- 1100
- Binary representation of 120:
- 1111000
- Binary representation of 500:
- 111110100
2.Write a Java Program to convert decimal to binary in java
- import java.util.Scanner;
- import java.util.Stack;
- public class ConvertDecimalToBinary{
- /**
- *www.instanceofjava.com
- */
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- // Create Stack object
- Stack<Integer> stack = new Stack<Integer>();
- //Take User input from keyboard
- System.out.println("Enter decimal number: ");
- int num = in.nextInt();
- while (num != 0)
- {
- int d = num % 2;
- stack.push(d);
- num /= 2;
- }
- System.out.print("\nBinary representation is:");
- while (!(stack.isEmpty() ))
- {
- System.out.print(stack.pop());
- }
- System.out.println();
- }
- }
- }
Output:
- Enter decimal number:
- 12
- Binary representation is:1100
3.Write a Java Program to convert decimal to binary in java
- import java.util.Scanner;
- public class ConvertDecimalToBinary{
- /**
- *www.instanceofjava.com
- */
- public static void convertDeciamlToBinary(int num){
- int binary[] = new int[40];
- int index = 0;
- while(num > 0){
- binary[index++] = num%2;
- num = num/2;
- }
- for(int i = index-1;i >= 0;i--){
- System.out.print(binary[i]);
- }
- }
- public static void main(String[] args) {
- System.out.println("Binary representation of 1: ");
- convertDeciamlToBinary(1);
- System.out.println("\nBinary representation of 4: ");
- convertDeciamlToBinary(4);
- System.out.println("\nBinary representation of 10: ");
- convertDeciamlToBinary(10);
- System.out.println("\nBinary representation of 12: ");
- convertDeciamlToBinary(12);
- }
- }
Output:
- Binary representation of 1:
- 1
- Binary representation of 4:
- 100
- Binary representation of 10:
- 1010
- Binary representation of 12:
- 1100
You Might Like:
Java Example Program to convert binary to decimal
Bagikan
Java Example program convert Decimal to Binary
4/
5
Oleh
Kris Kimcil