BINARY TO DECIMAL

This is the programe converting Binary Number to Decimal.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.lang.*;
import java.io.*;
public class BinaryToDecimal
{
   public static void main(String[] args) throws IOException
   {
       BufferedReader bf= new BufferedReader(new InputStreamReader(System.in));
       System.out.print("Enter the Binary value: ");
       String bin = bf.readLine();
       long num = Long.parseLong(bin);
       long rem;
       while(num > 0)
       {
            rem = num % 10;
            num = num / 10;
            if(rem != 0 && rem != 1)
            {
                System.out.println("This is not a binary number.");
                System.out.println("Please try once again.");
                System.exit(0);}}
                int i= Integer.parseInt(bin,2);
                System.out.println("Decimal:="+ i);
            }
        }
    }
}

Output :Binary Number to Decimal

gcdn