Skip to content
Related Articles

Related Articles

Java Program for Decimal to Binary Conversion
  • Difficulty Level : Easy
  • Last Updated : 13 Feb, 2018

Given a decimal number as input, we need to write a program to convert the given decimal number into equivalent binary number.

Examples:

Input : 7
Output : 111

Input : 10
Output : 1010

Input: 33
Output: 100001


Java




// Java program to convert a decimal
// number to binary number
import java.io.*;
  
class GFG 
{
    // function to convert decimal to binary
    static void decToBinary(int n)
    {
        // array to store binary number
        int[] binaryNum = new int[1000];
   
        // counter for binary array
        int i = 0;
        while (n > 0
        {
            // storing remainder in binary array
            binaryNum[i] = n % 2;
            n = n / 2;
            i++;
        }
   
        // printing binary array in reverse order
        for (int j = i - 1; j >= 0; j--)
            System.out.print(binaryNum[j]);
    }
      
    // driver program
    public static void main (String[] args) 
    {
        int n = 17;
        decToBinary(n);
    }
}
  
// Contributed by Pramod Kumar

Please refer complete article on Program for Decimal to Binary Conversion for more details!

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :