For a given number in binary, having
combinations of 1s and Os only, write a
program that outputs the decimal equivalent
of the binary number represented by the
binary number. The input binary number will
always be of length 4. YoŲ can write your
program accordingly
For example,
for the input provided as follows:
1111
The function should return,
15
Answers
Answered by
0
Answer:
Explanation:
The binary numbers are given here:
01110010001.
Answered by
0
The input:
1111
The function should return,
15
Explanation:
public class abc
{
public static int getDec(int bin)
{
int decimal = 0;
int n = 0;
while(true)
{
if(binary == 0)
{
break;
}
else
{
int temp = binary%10;
decimal += temp*Math.pow(2, n);
binary = binary/10;
n++;
}
}
return decimal;
}
public static void main(String args[])
{
System.out.println("Decimal of 1111 is: "+getDec(1111));
}
}
Similar questions