Senin, 21 Maret 2016

Java Example program convert Decimal to Binary


    • 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)

    1. import java.util.Scanner;
    2. public class ConvertDecimalToBinary{
    3.  
    4.     /**
    5.      *www.instanceofjava.com
    6.      */
    7.  
    8.  public static void main(String[] args) {
    9.         
    10.  System.out.println("\nBinary representation of 1: ");
    11.  System.out.println(Integer.toBinaryString(1));
    12.  System.out.println("\nBinary representation of 4: ");
    13.  System.out.println(Integer.toBinaryString(4));
    14.  System.out.println("Binary representation of 10: ");
    15.  System.out.println(Integer.toBinaryString(10));
    16.  System.out.println("\nBinary representation of 12: ");
    17.  System.out.println(Integer.toBinaryString(12));
    18.  System.out.println("\nBinary representation of 120: ");
    19.  System.out.println(Integer.toBinaryString(120));
    20.  System.out.println("\nBinary representation of 500: ");
    21.  System.out.println(Integer.toBinaryString(500));

    22. }
    23.  
    24. }





    Output:

    1. Binary representation of 1: 
    2. 1
    3. Binary representation of 4: 
    4. 100
    5. Binary representation of 10: 
    6. 1010
    7. Binary representation of 12: 
    8. 1100
    9. Binary representation of 120: 
    10. 1111000
    11. Binary representation of 500: 
    12. 111110100

    2.Write a Java Program to convert decimal to binary in java


    1. import java.util.Scanner;
    2. import java.util.Stack;
    3. public class ConvertDecimalToBinary{
    4.  
    5.     /**
    6.      *www.instanceofjava.com
    7.      */
    8.  
    9.  
    10.  public static void main(String[] args) {
    11.         
    12. Scanner in = new Scanner(System.in);
    13.          
    14. // Create Stack object
    15. Stack<Integer> stack = new Stack<Integer>();
    16.      
    17. //Take  User input from keyboard
    18.  System.out.println("Enter decimal number: ");
    19.  int num = in.nextInt();
    20.     
    21. while (num != 0)
    22. {
    23.    int d = num % 2;
    24.    stack.push(d);
    25.    num /= 2;
    26.  
    27.  } 
    28.      
    29.  System.out.print("\nBinary representation is:");
    30.  
    31. while (!(stack.isEmpty() ))
    32.  {
    33.    System.out.print(stack.pop());
    34.  }
    35.         
    36.  
    37. System.out.println();
    38.  
    39. }
    40.  
    41. }
    42.  
    43. }


    Output:

    1. Enter decimal number: 
    2. 12
    3.  
    4. Binary representation is:1100

    java program convert decimal to binary


    3.Write a Java Program to convert decimal to binary in java


    1. import java.util.Scanner;
    2. public class ConvertDecimalToBinary{
    3.  
    4.     /**
    5.      *www.instanceofjava.com
    6.      */
    7.  
    8. public static void convertDeciamlToBinary(int num){
    9.  
    10.   int binary[] = new int[40];
    11.          int index = 0;
    12.  while(num > 0){
    13.            binary[index++] = num%2;
    14.            num = num/2;
    15.  }

    16. for(int i = index-1;i >= 0;i--){
    17.            System.out.print(binary[i]);
    18. }
    19.  
    20. }
    21.  
    22.  public static void main(String[] args) {
    23.         
    24. System.out.println("Binary representation of 1: ");
    25. convertDeciamlToBinary(1);
    26.  
    27. System.out.println("\nBinary representation of 4: ");
    28. convertDeciamlToBinary(4);
    29.  
    30. System.out.println("\nBinary representation of 10: ");
    31. convertDeciamlToBinary(10);
    32.  
    33. System.out.println("\nBinary representation of 12: ");
    34.  convertDeciamlToBinary(12);
    35.  
    36. }
    37.  
    38. }


    Output:

    1. Binary representation of 1: 
    2. 1
    3. Binary representation of 4: 
    4. 100
    5. Binary representation of 10: 
    6. 1010
    7. Binary representation of 12: 
    8. 1100

    You Might Like:

    Bagikan

    Jangan lewatkan

    Java Example program convert Decimal to Binary
    4/ 5
    Oleh

    Subscribe via email

    Suka dengan artikel di atas? Tambahkan email Anda untuk berlangganan.