78. JAVA Program to Convert Binary value into Decimal number.
How does this program work?
- This program is used to convert Binary value into equivalent of Decimal number using Java.
- That means convert number with base value 2 to base value 10.
- The integer entered by user will store in one variable. Divide that number by 10.
- Store the remainder and that will be multiplied by base value, Declare base value and initialize with 1 means (2^0).
Here is the code
/* Java Program - Binary to Decimal Conversion */
import java.util.Scanner;public class binary
{
public static void main(String args[])
{
int binnum, decnum=0, i=1, rem;
System.out.print("Enter Binary Number:\n");
Scanner skill = new Scanner(System.in);
binnum = skill.nextInt();
// Converting the number into decimal format
while(binnum != 0){
rem = binnum%10;
decnum = decnum + rem*i;
i = i*2;
binnum = binnum/10;
}
System.out.print( "Equivalent Decimal number is:\n");
System.out.print(decnum );
}
}