-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarytoDecimal.java
More file actions
30 lines (19 loc) · 876 Bytes
/
BinarytoDecimal.java
File metadata and controls
30 lines (19 loc) · 876 Bytes
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
28
29
30
import java.io.*;
import java.math.*;
public class BinarytoDecimal {
public static void main(String[] args)throws IOException
{
DataInputStream in =new DataInputStream(System.in);
int binary_number = Integer.parseInt(in.readLine());
int i = 0;
double decimal = 0.0;
while(binary_number!=0)
{
int digit = binary_number % 10; //take out the digits from the end
decimal = decimal + digit*(Math.pow(2, i)); // use a accumulator to add up the digits while multiplying it with the correct power of 2
i++; // use a counter for using for the correct value to raise the power of 2 to.
binary_number = binary_number/10; // divide the number by ten so that the last digit used in calculation is no more used
}
System.out.println(decimal);
}
}