codescracker


java

Java Program to Convert Binary to Decimal



« Previous Program Next Program »


Convert Binary to Decimal

To convert binary to decimal in Java programming, you have to ask to the user to enter the number in binary format to convert it into decimal format, and then display the equivalent decimal value on the output screen.

Java Programming Code to Convert Binary to Decimal

Following Java program ask to the user to enter any number in binary format and will convert the given number into equivalent decimal format to provide the equivalent decimal value on the screen:

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

public class JavaProgram
{
    public static void main(String args[])
    {
        int binnum, decnum=0, i=1, rem;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter Binary Number : ");
        binnum = scan.nextInt();
		
        // converting the number into decimal format
        while(binnum != 0)
        {
            rem = binnum%10;
            decnum = decnum + rem*i;
            i = i*2;
            binnum = binnum/10;
        }
		
        System.out.print("Equivalent Decimal Value of " +binnum+ " is :\n");
        System.out.print(decnum);
    }
}

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

Java Program convert binary to decimal

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