Write a program in java to accept a Binary number and convert into its decimal equivalent.
import java.util.Scanner;
class bin_deci
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int binary,a,i=0;
double decimal=0;
System.out.println("\nEnter the Binary Number:- ");
binary=sc.nextInt();
while(binary!=0)
{
a=binary % 10;
decimal=decimal+a*(Math.pow(2,i));
binary=binary/10;
i++;
}
System.out.println("\nDecimal Number = "+decimal);
}
}
OUTPUT:--
MORE PROGRAMS
class bin_deci
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int binary,a,i=0;
double decimal=0;
System.out.println("\nEnter the Binary Number:- ");
binary=sc.nextInt();
while(binary!=0)
{
a=binary % 10;
decimal=decimal+a*(Math.pow(2,i));
binary=binary/10;
i++;
}
System.out.println("\nDecimal Number = "+decimal);
}
}
OUTPUT:--
MORE PROGRAMS
3 comments