For binary 100 decimal is 4.
For binary 10101 decimal is 21
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import java.util.Scanner; public class BinToDec{ public static void main(String []args){ Scanner sc = new Scanner(System.in); System.out.println("Enter a binary value:"); int binary = sc.nextInt(); int decimal = 0, i = 0; while(binary!=0){ decimal += (binary % 10) * (int)Math.pow(2,i); i++; binary = binary / 10; } System.out.println("Decimal value:"+decimal); } } |
Python
binary = int(input("Enter a binary value:")) decimal = i = 0 while binary!=0: decimal += (binary%10)*(2**i) i += 1 binary = binary // 10 print("decimal value :",decimal) |