Conversion of binary number to decimal number and its vice versa is one of the most important concept in understanding computer logics and it designs.
Binary number is expressed in base 2. It is made up of 0s or 1s.
Example of binary number = 10101
A bit is a single binary digit. The example given above has 5 bits.
Decimal number is expressed in base 10. We can use digits from 0 to 9.
Example of decimal number = 21
Below is the program to do the conversion from Binary to Decimal:
import java.util.Scanner;
public class BinaryToDecimal
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a Binary number : ");
int binNum = sc.nextInt();
int temp = binNum;
int sum=0,i=0;
while(temp>0)
{
int rem = temp%10;
sum = sum+rem*(int)Math.pow(2,i);
temp = temp/10;
i++;
}
System.out.println("Decimal conversion of "+binNum+" = "+sum);
sc.close();
}
}
No comments:
Post a Comment