Java Program for Decimal to Binary Conversion

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


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

My Personal Notes arrow_drop_up
Article Tags :

5


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.