Write a program to convert decimal to octal or decimal to hexadecimal or decimal to binary digits and then display the decimal number along with its changed form. Use only for loop or while loop and java.util.* package
(E.g.)The output must be like below-
Press A to convert the decimal into octal:
Press B to convert the decimal into hexadecimal:
Press C to convert the decimal into binary:
Enter a decimal number: 4.8
Its binary form: 100.11001100110
_______________________________________________________________________
**I pressed C so I got its binary form, If I have pressed B then it would have been like this-
Its hexadecimal form: 4.CCCCCCCCCCC
BTW above is for 4.8e10
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static String[] binaryTable = {
"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
public static char[] charTable = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
public static String decToBinary(double value) {
return intToBinary(value) + "." + floatToBinary(value);
}
public static String decToHex(double value) {
String intBinary = intToBinary(value);
intBinary = "0000".substring(intBinary.length() % 4 == 0 ? 4 : intBinary.length() % 4) + intBinary;
String floatBinary = floatToBinary(value);
floatBinary += "0000".substring(floatBinary.length() % 4 == 0 ? 4 : floatBinary.length() % 4);
return binToHex(intBinary) + "." + binToHex(floatBinary);
}
public static String decToOctal(double value) {
String intBinary = intToBinary(value);
intBinary = "000".substring(intBinary.length() % 3 == 0 ? 3 : intBinary.length() % 3) + intBinary;
String floatBinary = floatToBinary(value);
floatBinary += "000".substring(floatBinary.length() % 3 == 0 ? 3 : floatBinary.length() % 3);
return binToOctal(intBinary) + "." + binToOctal(floatBinary);
}
public static String intToBinary(double value) {
int whole = (int) value;
Stack<Integer> binary = new Stack<>();
do {
binary.push(whole - (whole / 2 * 2));
whole /= 2;
} while (whole > 0);
char[] binaryChar = new char[binary.size()];
for (int i = 0; i < binaryChar.length; i++) {
binaryChar[i] = (char) ('0' + binary.pop());
}
return new String(binaryChar);
}
public static String floatToBinary(double value) {
double decimal = value - (int) value;
LinkedList<Integer> binary = new LinkedList<>();
do {
binary.add((int) (decimal * 2));
decimal = decimal * 2 - (int) (decimal * 2);
} while (decimal > 0);
char[] binaryChar = new char[binary.size()];
int i = 0;
for (Integer digit : binary) {
binaryChar[i++] = (char) ('0' + digit);
}
return new String(binaryChar);
}
public static String binToHex(String binary) {
char[] hex = new char[binary.length() / 4];
for (int i = 0, j = 0; i < binary.length(); i += 4) {
for (int k = 0; k < binaryTable.length; k++) {
if (binary.substring(i, i + 4).equals(binaryTable[k])) {
hex[j++] = charTable[k];
}
}
}
return new String(hex);
}
public static String binToOctal(String binary) {
char[] hex = new char[binary.length() / 3];
for (int i = 0, j = 0; i < binary.length(); i += 3) {
for (int k = 0; k < 8; k++) {
if (binary.substring(i, i + 3).equals(binaryTable[k].substring(1))) {
hex[j++] = charTable[k];
}
}
}
return new String(hex);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Press A to convert the decimal into octal\n" +
"Press B to convert the decimal into hexadecimal\n" +
"Press C to convert the decimal into binary");
String choice = in.next();
System.out.println("Enter a decimal number: ");
double value = in.nextDouble();
switch (choice) {
case "A":
System.out.println("Its(" + value + ") octal form: " + decToOctal(value));
break;
case "B":
System.out.println("Its(" + value + ") hexadecimal form: " + decToHex(value));
break;
case "C":
System.out.println("Its(" + value + ") binary form: " + decToBinary(value));
break;
}
}
}
Comments
Leave a comment