Binary to Decimal conversion

Write a Java program to convert a binary number to decimal number.

import java.util.Scanner;
class BinaryToDecimal
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a binary number");
        int num = in.nextInt();
        
        int d = 0;
        int t = num;
        int i=0;
        while(t>0)
        {
            int r = t%10;
            t = t/10;
            d = d + r *(int) Math.pow(2,i++);
        }
        System.out.println("Decimal Equivalent of " +num+ " is " +d);
    }
}

Video explanation

Leave a Comment

Your email address will not be published. Required fields are marked *