Question
Program to convert binary to decimal
ENTER BINARY NUMBER:1001
THE DECIMAL VALUE OF BINARY 1001 IS 9
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
Java
Python
Java
import java.util.Scanner;
public class BinaryToDecimal
{
public static void main()
{
int count=0;
Scanner sc=new Scanner(System.in);
long decimal=0,binary=0,temp=0;
count=0;
System.out.print("ENTER BINARY NUMBER:");
binary=sc.nextInt();
temp=binary;
while(temp>0)
{
decimal=decimal+(temp%10)*((long)Math.pow(2,count));
count++;
temp=temp/10;
}
System.out.println("THE DECIMAL VALUE OF BINARY "+binary+" IS "+decimal);
}
}
Python
binaryNumber=int(input("Enter positive Binary number:"))
count=0
decimalNumber=0
temp=binaryNumber
while(temp>0):
rem=temp%10
decimalNumber=decimalNumber+rem*(2**count)
count=count+1
temp=temp//10
print("Decimal number of binary number:",binaryNumber,"is:",decimalNumber)