0

I am trying to convert decimal to binary numbers from the user's input using Java but so far, I'm running into errors I'm not sure why. What am I doing wrong? Don't mind the naming, ultimately I would like to convert decimal into binary without using if and while statements. Also without using the decimaltobinary string.

Thanks a lot.

package r

public static void main(String[] args) {
int number; 
int remainder;
Scanner in = new Scanner(System.in);

System.out.println("Enter a positive integer");
number=in.nextInt();
remainder= number %2; 
System.out.print(remainder);

{ 
return null;

This is what I have so far.

Atif Shah
  • 183
  • 1
  • 1
  • 10

2 Answers2

0

Try this:

String binaryString = Long.toBinaryString(Double.doubleToRawLongBits(yourDecimalNumber));

Where yourDecimalNumber is the name of the decimal variable that you want to convert to binary.

You can also use a for loop and bitwise operation without using any if or while statements like so:

int number = 10;
String binaryString = "";
final int intBitSize = 32; // 32 is the number of bits in an int.

for(int i=0; i<intBitSize; i++)
{
    binaryString = String.valueOf(number&1) + binaryString;

    number = number >>> 1;
}

System.out.println(binaryString);
Dermot Blair
  • 1,600
  • 10
  • 10
  • This will work but I can't use a single string that will give a decimal number into binary. I can use for loops to get the remainder than use strings to put it as a binary number. – Atif Shah Mar 29 '15 at 16:55
  • Ok I'll leave this answer here anyway in case it is useful to somebody else. – Dermot Blair Mar 29 '15 at 16:57
  • I've spent over three hours trying to figure out this problem and so far I got nothing. :( – Atif Shah Mar 29 '15 at 16:59
  • if and while statements are very straighforward, probably even easier to understand than for loops. I think the only way that you can do this using for loops is by using bitwise operation which is a lot more complex than using if and while statements. – Dermot Blair Mar 29 '15 at 17:02
  • @AtifShah see my updated answer which uses a for loop without any if or while statements as requested. – Dermot Blair Mar 29 '15 at 17:22
  • Thanks that helped out a lot, I'm understanding while loops and if statements a lot better. I don't understand how would I remove the intbitsize. So If i want to convert 42 into bits, it should do it into the right amount of bits, without having to mention the bit size. – Atif Shah Mar 29 '15 at 19:04
  • 32 is the number of bits in an int. It is not the number to be converted to binary. The number to be converted to binary is stored in the `number` variable and is set equal to 10. When we convert an int to binary, we want to iterate through the 32 bits of the integer so the variable `intBitSize` will stay set to 32 and should not be changed. You could also just put the 32 in the for loop like this: `for(int i=0; i<32; i++)` However I stored it in a variable for clarity. – Dermot Blair Mar 29 '15 at 22:10
  • The argument to `Double.toRawLongBits()` is a double, not a decimal number. – user207421 Jun 17 '17 at 10:30
0

This is how to convert a decimal to binary :

Using for loop :

public class DecimalToBinary {

    public void printBinaryFormat(int decimalNumber){
    int remainder = 0;
    for (int i = 1; decimalNumber > 0; i++) {

        remainder = decimalNumber % 2;    
                decimalNumber /= 2;
                System.out.print(remainder);
            }
        }

    public static void main(String a[]){
        DecimalToBinary dtb = new DecimalToBinary();
        dtb.printBinaryFormat(25);
    }
}

Using while loop :

public class DecimalToBinary 
{

    public void printBinaryFormat(int number){
        int binary[] = new int[25];
        int index = 0;
        while(number > 0){
            binary[index++] = number%2;
            number = number/2;
        }
        for(int i = index-1;i >= 0;i--){
            System.out.print(binary[i]);
        }
    }

    public static void main(String a[]){
        DecimalToBinary dtb = new DecimalToBinary();
        dtb.printBinaryFormat(25);
    }
}

Hope I helped .

Happy Coding :D

MCHAppy
  • 1,012
  • 9
  • 15