codescracker


java

Java Program to Convert Decimal to Binary



« Previous Program Next Program »


Convert Decimal to Binary

To convert decimal to binary in Java Programming, you have to ask to the user to enter any number in decimal number format to convert it into binary number format to display the equivalent value in binary number system as shown in the following program.

Java Programming Code to Convert Decimal to Binary

Following Java Program ask to the user to enter any number in decimal to convert it into binary, then display the result on the screen :

/* Java Program Example - Convert Decimal to Binary */
		
import java.util.Scanner;

public class JavaProgram
{
    public static void main(String args[])
    {
        int decnum, rem, quot, i=1, j;
        int binnum[] = new int[100];
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter any Decimal Number : ");
        decnum = scan.nextInt();
		
        quot = decnum;
		
        while(quot != 0)
        {
            binnum[i++] = quot%2;
            quot = quot/2;
        }
		
        System.out.print("Equivalent Binary Value of " + decnum + " is :\n");
        for(j=i-1; j>0; j--)
        {
            System.out.print(binnum[j]);
        }
    }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program convert decimal to binary

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test