To write a program that converts a binary number into the eq

To write a program that converts a binary number into the equivalent decimal number, we note two things: (1) The weight of each bit in the binary number must be known, and (2) the weight is assigned from right to left. Because we do not know in advance how many bits are in the binary number, we must process the bits from right to left. After processing a bit, we can add 1 to its weight, giving the weight of the bit immediately to its left. Also, each bit must be extracted from the binary number and multiplied by 2 to the power of its weight. To extract a bit, you can use the mod operator.

Write a program that uses a stack to convert a binary number into an equivalent decimal number and test your function for the following values: 11000101, 10101010, 11111111, 10000000, 1111100000.

Program plan:

• Define a findDecimalNo function which calculates the decimal equivalent of a binary number.

• Input the binary number from the user.

• Make a call to findDecimalNo passing the stack containing the binary bits and the weight of the leftmost bit.

• Display the decimal equivalent to binary number.

Solution

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;

public class DecimalToBinary {

public static void main(String[] args) {
alok a = new alok();
List<Integer> ints = new ArrayList<>();
Stack<Integer> stack = new Stack<>();
int i;
String[] tokens;
Scanner in = new Scanner(System.in);
try {
System.out.println(\"Please Enter the Binary Number{o/1} please keep space between every two binary Input\");

tokens = in.nextLine().split(\"\\\\s\");

for (i = 0; i < tokens.length; i++) {
ints.add(Integer.parseInt(tokens[i]));
}
} catch (Exception ex) {
System.out.println(ex.toString());
}

System.out.println(\"The Number You Are Enterd Are \" + ints);
ints.stream().map((int1) -> int1).forEach((num) -> {
stack.push(num);
});
System.out.println(\"\ \");
System.out.println(stack);
int d = a.convertBinary(stack);
System.out.println(\"Decimal Number is :\" + d);
}
}

class alok {

public int convertBinary(Stack<Integer> stack) {
int k = 0, dec, bin;
List<Integer> deci = new ArrayList<>();
int p = 0;
while (!stack.empty()) {
bin = stack.pop();
if (bin == 1) {
dec = (int) Math.pow(2, k);
deci.add(dec);
k++;
} else {
k++;
}

}
int sum = 0;
sum = deci.stream().map((i) -> i).reduce(sum, Integer::sum);
return sum;
}

}

the Output is :

Please Enter the Binary Number{o/1} please keep space between every two binary Input
1 1 1 1 1 1 1 0
The Number You Are Enterd Are [1, 1, 1, 1, 1, 1, 1, 0]


[1, 1, 1, 1, 1, 1, 1, 0]
Decimal Number is :254

thanks..

To write a program that converts a binary number into the equivalent decimal number, we note two things: (1) The weight of each bit in the binary number must be
To write a program that converts a binary number into the equivalent decimal number, we note two things: (1) The weight of each bit in the binary number must be

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site