java program to convert binary number to its decimal equivalent

Programming Example

Write a Java Program which accept a binary number and print is equivalent Decimal Number.

import java.util.*;
class Binary2Decimal
{
   public static void main(String arr[])
   {
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter any Binary Number :");
       int num=sc.nextInt();
       int dec=0,c=0,r;
       while(num!=0)
       {
           r=num%10;
           dec=dec+r*(int)Math.pow(2,c);
           num=num/10;
           c=c+1;
        }
       System.out.println("The Decimal Equivalent ="+dec);
   }
}

Solution


							
Output

Enter any Binary Number :

101

The Decimal Equivalent =5

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Website development Company in Allahabad Sarkari Exam Quiz