I'm doing a number systems code right now and on my decimalToBinary, whenever I enter a decimal number it always converts it to a 1 no matter what decimal number it is. The code is down here.
public static void decimalToBinary() {
do {
System.out.println("Enter your decimal number");
numDecimal = input.nextInt();
if (numDecimal < 0) {
System.out.println("Enter a valid number!");
}
} while (numDecimal < 0);
int intNum = Integer.valueOf(numDecimal);
int counter = 0;
binaryVal[counter++] = intNum % 2;
intNum = intNum/2;
for (int i = counter-1; i >= 0; i--) {
System.out.println("Your binary number is " + binaryVal[i]);
}
}
I recommend you to change the code by the following snippet, you also need to consider to create
binaryValarray with a sufficient dimension in order to hold the complete number digits that it is going to generate the transformation to binary.