» » 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:

     


    Java programming interview questions

    1. Print prime numbers? 
    2. What happens if we place return statement in try catch blocks 
    3. Write a java program to convert binary to decimal 
    4. Java Program to convert Decimal to Binary
    5. Java program to restrict a class from creating not more than three objects
    6. Java basic interview programs on this keyword 
    7. Interfaces allows constructors? 
    8. Can we create static constructor in java 
    9. Super keyword interview questions java 
    10. Java interview questions on final keyword
    11. Can we create private constructor in java
    12. Java Program Find Second highest number in an integer array 
    13. Java interview programming questions on interfaces 
    14. Top 15 abstract class interview questions  
    15. Java interview Questions on main() method  
    16. Top 20 collection framework interview Questions
    17. Java Interview Program to find smallest and second smallest number in an array 
    18. Java Coding Interview programming Questions : Java Test on HashMap  
    19. Explain java data types with example programs 
    20. Constructor chaining in java with example programs 
    21. Swap two numbers without using third variable in java 
    22. Find sum of digits in java 
    23. How to create immutable class in java 
    24. AtomicInteger in java 
    25. Check Even or Odd without using modulus and division  
    26. String Reverse Without using String API 
    27. Find Biggest substring in between specified character
    28. Check string is palindrome or not?
    29. Reverse a number in java?
    30. Fibonacci series with Recursive?
    31. Fibonacci series without using Recursive?
    32. Sort the String using string API?
    33. Sort the String without using String API?
    34. what is the difference between method overloading and method overriding?
    35. How to find largest element in an array with index and value ?
    36. Sort integer array using bubble sort in java?
    37. Object Cloning in java example?
    38. Method Overriding in java?
    39. Program for create Singleton class?
    40. Print numbers in pyramid shape?
    41. Check armstrong number or not?
    42. Producer Consumer Problem?
    43. Remove duplicate elements from an array
    44. Convert Byte Array to String
    45. Print 1 to 10 without using loops
    46. Add 2 Matrices
    47. Multiply 2 Matrices
    48. How to Add elements to hash map and Display
    49. Sort ArrayList in descending order
    50. Sort Object Using Comparator
    51. Count Number of Occurrences of character in a String
    52. Can we Overload static methods in java
    53. Can we Override static methods in java 
    54. Can we call super class static methods from sub class 
    55. Explain return type in java 
    56. Can we call Sub class methods using super class object? 
    57. Can we Override private methods ? 
    58. Basic Programming Questions to Practice : Test your Skill
    59. Java programming interview questions on collections

    Instance Of Java

    We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
    «
    Next
    Newer Post
    »
    Previous
    Older Post

    No comments

    Leave a Reply

    Select Menu