|
| 1 | +/* |
| 2 | +4.31 (Printing the Decimal Equivalent of a Binary Number) Write an application that inputs an |
| 3 | +integer containing only 0s and 1s (i.e., a binary integer) and prints its decimal equivalent. [Hint: Use |
| 4 | +the remainder and division operators to pick off the binary number’s digits one at a time, from right |
| 5 | +to left. In the decimal number system, the rightmost digit has a positional value of 1 and the next |
| 6 | +digit to the left a positional value of 10, then 100, then 1000, and so on. The decimal number 234 |
| 7 | +can be interpreted as 4 * 1 + 3 * 10 + 2 * 100. In the binary number system, the rightmost digit has |
| 8 | +a positional value of 1, the next digit to the left a positional value of 2, then 4, then 8, and so on. |
| 9 | +The decimal equivalent of binary 1101 is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8, or 1 + 0 + 4 + 8 or, 13.] |
| 10 | + */ |
| 11 | + |
| 12 | +package chapter4; |
| 13 | + |
| 14 | +import java.util.Scanner; |
| 15 | + |
| 16 | +public class DecimalEquivalentOfBinary { |
| 17 | + public static void main(String[] args) { |
| 18 | + Scanner input = new Scanner(System.in); |
| 19 | + int binaryNumber = input.nextInt(); |
| 20 | + |
| 21 | + int decimalNumber = 0; |
| 22 | + |
| 23 | + if(binaryNumber >= 10 && binaryNumber <= 99) { |
| 24 | + int firstDigit = binaryNumber % 10; |
| 25 | + int secondDigit = binaryNumber / 10; |
| 26 | + |
| 27 | + if(firstDigit == 1 || firstDigit == 0 && (secondDigit == 1 || secondDigit == 0)) { |
| 28 | + decimalNumber = firstDigit + secondDigit * 2; |
| 29 | + } |
| 30 | + System.out.println("The decimal equivalent of " + binaryNumber + "is " + decimalNumber); |
| 31 | + } else if(binaryNumber >= 100 && binaryNumber <= 999) { |
| 32 | + int firstDigit = binaryNumber % 10; |
| 33 | + int secondDigit = binaryNumber / 10 % 10; |
| 34 | + int thirdDigit = binaryNumber / 100; |
| 35 | + |
| 36 | + if(firstDigit == 1 || firstDigit == 0 && (secondDigit == 1 || secondDigit == 0) && (thirdDigit == 1 || thirdDigit == 0)) { |
| 37 | + decimalNumber = firstDigit + secondDigit * 2 + thirdDigit * 4; |
| 38 | + } |
| 39 | + System.out.println("The decimal equivalent of " + binaryNumber + " is " + decimalNumber); |
| 40 | + } else if(binaryNumber >= 1000 && binaryNumber <= 9999) { |
| 41 | + int firstDigit = binaryNumber % 10; |
| 42 | + int secondDigit = binaryNumber / 10 % 10; |
| 43 | + int thirdDigit = binaryNumber / 100 % 10; |
| 44 | + int fourthDigit = binaryNumber / 1000; |
| 45 | + |
| 46 | + if(firstDigit == 1 || firstDigit == 0 && (secondDigit == 1 || secondDigit == 0) && (thirdDigit == 1 || thirdDigit == 0) && (fourthDigit == 1 || fourthDigit == 0)) { |
| 47 | + decimalNumber = firstDigit + secondDigit * 2 + thirdDigit * 4 + fourthDigit * 8; |
| 48 | + } |
| 49 | + System.out.println("The decimal equivalent of " + binaryNumber + " is " + decimalNumber); |
| 50 | + } else if(binaryNumber >= 10000 && binaryNumber <= 99999) { |
| 51 | + int firstDigit = binaryNumber % 10; |
| 52 | + int secondDigit = binaryNumber / 10 % 10; |
| 53 | + int thirdDigit = binaryNumber / 100 % 10; |
| 54 | + int fourthDigit = binaryNumber / 1000 % 10; |
| 55 | + int fifthDigit = binaryNumber / 10000; |
| 56 | + |
| 57 | + if (firstDigit == 1 || firstDigit == 0 && (secondDigit == 1 || secondDigit == 0) && (thirdDigit == 1 || thirdDigit == 0) && (fourthDigit == 1 || fourthDigit == 0) && (fifthDigit == 1 || fifthDigit == 0)) { |
| 58 | + decimalNumber = firstDigit + secondDigit * 2 + thirdDigit * 4 + fourthDigit * 8 + fifthDigit * 16; |
| 59 | + } |
| 60 | + System.out.println("The decimal equivalent of " + binaryNumber + " is " + decimalNumber); |
| 61 | + } else { |
| 62 | + System.out.println("Number is not a binary number"); |
| 63 | + } |
| 64 | + |
| 65 | + } |
| 66 | +} |
0 commit comments