From 3bf1ea88ccefaeb626e837d26b02f68bfc6b061e Mon Sep 17 00:00:00 2001 From: Ba5314 <68341196+Ba5314@users.noreply.github.com> Date: Wed, 28 Oct 2020 12:55:10 +0530 Subject: [PATCH] Create Program to convert binary number to decimal --- Program to convert binary number to decimal | 22 +++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Program to convert binary number to decimal diff --git a/Program to convert binary number to decimal b/Program to convert binary number to decimal new file mode 100644 index 0000000..5acc1ee --- /dev/null +++ b/Program to convert binary number to decimal @@ -0,0 +1,22 @@ +public class BinaryDecimal { + + public static void main(String[] args) { + long num = 110110111; + int decimal = convertBinaryToDecimal(num); + System.out.printf("%d in binary = %d in decimal", num, decimal); + } + + public static int convertBinaryToDecimal(long num) + { + int decimalNumber = 0, i = 0; + long remainder; + while (num != 0) + { + remainder = num % 10; + num /= 10; + decimalNumber += remainder * Math.pow(2, i); + ++i; + } + return decimalNumber; + } +}