BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java program to convert decimal to binary

Last Updated: September 10, 2022 by Chaitanya Singh | Filed Under: Java Examples

There are three following ways to convert Decimal number to binary number:

1) Using toBinaryString() method of Integer class.
2) Do conversion by writing your own logic without using any predefined methods.
3) Using Stack

Method 1: Using toBinaryString() method

class DecimalBinaryExample{
     
    public static void main(String a[]){
    	System.out.println("Binary representation of 124: ");
    	System.out.println(Integer.toBinaryString(124));
        System.out.println("\nBinary representation of 45: ");
        System.out.println(Integer.toBinaryString(45));
        System.out.println("\nBinary representation of 999: ");
        System.out.println(Integer.toBinaryString(999));
    }
}

Output:

Binary representation of 124: 
1111100

Binary representation of 45: 
101101

Binary representation of 999: 
1111100111

Method 2: Without using predefined method

class DecimalBinaryExample{
 
  public void convertBinary(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 a[]){
     DecimalBinaryExample obj = new DecimalBinaryExample();
     System.out.println("Binary representation of 124: ");
     obj.convertBinary(124);
     System.out.println("\nBinary representation of 45: ");
     obj.convertBinary(45);
     System.out.println("\nBinary representation of 999: ");
     obj.convertBinary(999);
  }
}

Output:

Binary representation of 124: 
1111100
Binary representation of 45: 
101101
Binary representation of 999: 
1111100111

Method 3: Decimal to Binary using Stack

import java.util.*;
class DecimalBinaryStack
{
  public static void main(String[] args) 
  { 
    Scanner in = new Scanner(System.in);
 
    // Create Stack object
    Stack<Integer> stack = new Stack<Integer>();
 
    // User input 
    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: 
999

Binary representation is:1111100111

Top Related Articles:

  1. Java Program for Decimal to Hexadecimal Conversion
  2. Java Program for Decimal to Octal Conversion
  3. Java Program to Calculate average using Array
  4. java program to find factorial of a given number using recursion
  5. Java Program to Add Two Complex Numbers

Tags: Java-Conversion

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Ganesh Kumar Mahato says

    January 12, 2017 at 10:33 AM

    HOW TO CONVERT ARRAY[4,6,8,10] TO ARRAY [4,7,9,11] IN JAVA

    INPUT =[4,6,8,10]
    OUTPUT= [4,7,9,11]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap