Binary to Decimal Java Source Code

Hi, below is a simple example (source code) on how to convert a binary number to a decimal number using Java.

/*  From Techie.Click
    By JP  */
import java.util.Scanner;
import java.lang.Math;
public class Binary2decimal1 {
    public static void main(String[] args) {
        int rem, count = 0;
        int n=0, bin;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a Binary number: ");
        bin = input.nextInt();
        while (bin > 0)
        {
            rem = bin % 10;
            n = n + rem * ((int)Math.pow(2, count));
            count++;
            bin = bin / 10;
        }
        System.out.println("The decimal value is: " + n);
    }
}

Binary to Decimal Java Source Code

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.