Algorithm
Binary to Decimal Conversion Algorithm:
- Read a binary number as input.
- Initialize a variable
decimal
to 0. - Initialize a variable
base
to 1. - Iterate through each digit of the binary number from right to left.
- Multiply the current digit by the
base
. - Add the result to the
decimal
variable. - Multiply the
base
by 2.
- Multiply the current digit by the
- Print or return the
decimal
value as the result.
Decimal to Binary Conversion Algorithm:
- Read a decimal number as input.
- Initialize an empty string
binary
to store the binary representation. - While the decimal number is greater than 0:
- Take the remainder when dividing the decimal number by 2.
- Convert the remainder to a character and prepend it to the
binary
string. - Divide the decimal number by 2.
- Print or return the
binary
string as the result.
Code Examples
#1 Code Example- Binary to Decimal Conversion Using Custom Method
Code -
Java Programming
class Main {
public static void main(String[] args) {
// binary number
long num = 110110111;
// call method by passing the binary number
int decimal = convertBinaryToDecimal(num);
System.out.println("Binary to Decimal");
System.out.println(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;
}
}
Copy The Code &
Try With Live Editor
Output
110110111 in binary = 439 in decimal
#2 Code Example- Binary to Decimal Conversion Using parseInt()
Code -
Java Programming
class Main {
public static void main(String[] args) {
// binary number
String binary = "01011011";
// convert to decimal
int decimal = Integer.parseInt(binary, 2);
System.out.println(binary + " in binary = " + decimal + " in decimal.");
}
}
Copy The Code &
Try With Live Editor
Output
01011011 in binary = 91 in decimal.
#3 Code Example- Decimal to Binary Conversion using Custom Method
Code -
Java Programming
class Main {
public static void main(String[] args) {
// decimal number
int num = 19;
System.out.println("Decimal to Binary");
// call method to convert to binary
long binary = convertDecimalToBinary(num);
System.out.println("\n" + num + " = " + binary);
}
public static long convertDecimalToBinary(int n) {
long binaryNumber = 0;
int remainder, i = 1, step = 1;
while (n!=0) {
remainder = n % 2;
System.out.println("Step " + step++ + ": " + n + "/2");
System.out.println("Quotient = " + n/2 + ", Remainder = " + remainder);
n /= 2;
binaryNumber += remainder * i;
i *= 10;
}
return binaryNumber;
}
}
Copy The Code &
Try With Live Editor
Output
Decimal to Binary
Step 1: 19/2
Quotient = 9, Remainder = 1
Step 2: 9/2
Quotient = 4, Remainder = 1
Step 3: 4/2
Quotient = 2, Remainder = 0
Step 4: 2/2
Quotient = 1, Remainder = 0
Step 5: 1/2
Quotient = 0, Remainder = 1
19 = 10011
Step 1: 19/2
Quotient = 9, Remainder = 1
Step 2: 9/2
Quotient = 4, Remainder = 1
Step 3: 4/2
Quotient = 2, Remainder = 0
Step 4: 2/2
Quotient = 1, Remainder = 0
Step 5: 1/2
Quotient = 0, Remainder = 1
19 = 10011
#4 Code Example- Decimal to Binary Conversion using toBinaryString()
Code -
Java Programming
class Main {
public static void main(String[] args) {
// decimal number
int decimal = 91;
// convert decimal to binary
String binary = Integer.toBinaryString(decimal);
System.out.println(decimal + " in decimal = " + binary + " in binary.");
}
}
Copy The Code &
Try With Live Editor
Output
91 in decimal = 1011011 in binary.
Demonstration
Java Programing Example to Convert Binary Number to Decimal and vice-versa-DevsEnv