Write a program to convert binary to decimal number in java.
Write java code Convert binary number to decimal number.
Convert binary to decimal Number using java.
//convert
binary to decimal
import
java.io.*;
public
class binary2decimal
{
public static void
main(String[]args)throws IOException
{
int num,i=0,k,r;
BufferedReader br=new
BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter
the number to convert binary to decimal");
num=Integer.parseInt(br.readLine());
int a[]=new int[20];
//int b[]=new int[20];
while(num>0)
{
a[i]=num%10;
num=num/10;
i++;
}
k=i-1;
r=0;
while(k>=0)
{
r=(int)(r+(a[k]*Math.pow(2,k)));
k--;
}
System.out.print(r);
}
}
No comments:
Post a Comment