Binary to Decimal Conversion in Java
In this article, We will see how to convert binary to decimal in java with the help of simple examples. Binary to decimal conversion in java is the most asked program in java written interviews.
In the last articles we saw java programs to covert decimal to binary number and decimal number to hexadecimal and hexadecimal to decimal conversion.
Let's start with java program to convert binary to decimal.
As we know, Binary number is a number which is represented in the form of 0 and 1. In other words you can say, true/false or on/off.
Decimal number is number which starts from 0 to 9 with the base 10.
There are two ways to convert binary number to decimal number in java.
In the last articles we saw java programs to covert decimal to binary number and decimal number to hexadecimal and hexadecimal to decimal conversion.
Let's start with java program to convert binary to decimal.
As we know, Binary number is a number which is represented in the form of 0 and 1. In other words you can say, true/false or on/off.
Decimal number is number which starts from 0 to 9 with the base 10.
There are two ways to convert binary number to decimal number in java.
- By using Integer.parseInt() method of Integer class.
- By creating your own logic without using any predefined method.
Let's start with first method which is using Integer.parseInt() method to converting binary to decimal in java.
(1) Binary to decimal conversion program in java by using Integer.parseInt() method.
This is the first example of binary to decimal conversion by using Integer.parseInt() method.
class Example1
{
public static void main(String args[])
{
//declaring string containing binary number
String str = "101101";//binary number
int decimalNo = Integer.parseInt(str, 2);
System.out.println("Decimal no is: "+decimalNo);
}
}
Output: Decimal no is: 45
When we will convert binary number which is 101101 into decimal number it will produce 45.
Output: Decimal no is: 45
When we will convert binary number which is 101101 into decimal number it will produce 45.
(2) Binary to decimal conversion program in java by using Integer.parseInt() method but with Scanner class.
This is another example of Integer.parseInt() method but with Scanner class because here we are going to take input from the user from console and then convert binary to decimal number.
import java.util.*;
class Example2
{
public static void main(String args[])
{
//declaring Scanner class
Scanner sc = new Scanner(System.in);
System.out.println("Enter your binary number: ");
String str = sc.nextLine();
System.out.println("Decimal number is: "+Integer.parseInt(str,2));
}
}
Output: Enter your binary number
1100
Decimal number is: 12
Now moving on second method which is create your own logic for converting binary to decimal conversion program in java.
import java.util.*;
class Example2
{
public static void main(String args[])
{
//declaring Scanner class
Scanner sc = new Scanner(System.in);
System.out.println("Enter your binary number: ");
String str = sc.nextLine();
System.out.println("Decimal number is: "+Integer.parseInt(str,2));
}
}
Output: Enter your binary number
1100
Decimal number is: 12
Now moving on second method which is create your own logic for converting binary to decimal conversion program in java.
(3) Binary to decimal conversion program in java without using any predefined method.
In this example we are not going to use any predefined method just like Integer.parseInt() of Integer wrapper class but here we will create own logic for conversion of binary to decimal.
import java.util.Scanner;
class Example3
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter your binary number: ");
int binaryno = sc.nextInt();
int binary = binaryno;
int decimal = 0;
int power = 0;
while(true)
{
if(binary == 0)
{
break;
break;
}
else
{
int temp = binary%10;
decimal += temp*Math.pow(2,power);
binary = binary/10;
power++;
}
}
System.out.println("Decimal number is: "+decimal);
}
}
Output: Enter your binary number:
110
Decimal number is: 6
Now above all the examples of binary to decimal conversion programs in java are quite useful for both fresher and experienced for core java interviews.
Output: Enter your binary number:
110
Decimal number is: 6
Now above all the examples of binary to decimal conversion programs in java are quite useful for both fresher and experienced for core java interviews.
Awesome java program for binary to decimal conversion. Best java blog
ReplyDeleteThanks for sharing this informative blog java training in chennai
ReplyDelete