DECtoBIN Conversion WITHOUT using String Methods

Hey I am trying to create a programme that determines the binary sequence of an input (unknown) decimal number. I DO NOT want to use a String Method.

I am running into the difficulty that my remainder is stuck on an endless loop. I want to find the divisible value of the decimal number (ie the quotient) and it's remainder, then print the remainder. Then continue calculating the divisible value of the quotient and its subsequent remainder until the quotient is 0.

For example the decimal number 10 should produce a binary sequence 1010.

The code I have written is shown below.

Can anyone please advise?

**//Prompt user to input decimal value**

System.out.print("please enter the decimal value: ");

decimal = keyboardIn.nextInt();

do

{

//Calculations

quotient = decimal/2;//

remainder = decimal%2;

System.out.print(" " +remainder);

quotient = decimal;

counter++;

}while (quotient !=0);

Re: DECtoBIN Conversion WITHOUT using String Methods

Quote:

decimal number 10 should produce a binary sequence 1010.

What type of variable is that "binary sequence" held in? Do you need to drop the leading 0s?

An int variable holds 32 bits that can be shifted bit by bit. any bit of the int can be tested by ANDing the bit with a 1 in and testing if the results is 0. For example: 1010 AND 1000 = 1000 or 1101 AND 1 = 1

Please edit your post and wrap your code with code tags:

[code=java]

YOUR CODE HERE

[/code]

to get highlighting and preserve formatting.

Quote:

stuck on an endless loop

How does quotient ever get to be == 0 at the end of the loop?

Re: DECtoBIN Conversion WITHOUT using String Methods

Hey Norm,

The variable that the binary sequence is held in is an int variable. I have included the full code below.

The quotient would ==0 as shown in the following example:

decimal = 10

quotient = decimal/2;

remainder = decimal%2;

therefore,

quotient = 10/2 = 5

remainder = 10%2 = 0

quotient = 5/2 = 2

remainder = 5%2 = 1

quotient = 2/2 = 1

remainder = 2%2 = 0

quotient = 1/2 = 0 // quotient ==0

remainder = 1%2 = 1

<code>

public class DecToBin

{

public static void main(String[]args)

{

Scanner keyboardIn = new Scanner (System.in);

<var>

// declare variables

int decimal, quotient, remainder, counter =1;

//Prompt user to input decimal value

System.out.print("please enter the decimal value: ");

decimal = keyboardIn.nextInt();

do

{

quotient = decimal/2;

remainder = decimal%2;

System.out.print(" " +remainder);

quotient = decimal;

counter++;

}while (quotient!=0);

}//end main method

}//end class

Re: DECtoBIN Conversion WITHOUT using String Methods

Quote:

The variable that the binary sequence is held in is an int variable.

If the input is in an int variable and the binary sequence is held in an int variable, I don't understand what the code is supposed to do. There wouldn't be any logic needed, just an assignment statement:

int inV = 10;

int binarySeq = inV; // copy the value here

Did you really mean that or did you mean that "binary sequence" will be printed as digits on the console?

Please edit your post and wrap your code with code tags:

[code=java]

YOUR CODE HERE

[/code]

to get highlighting and preserve formatting.

Quote:

The quotient would ==0 as shown in the following example:

To see what the value of quotient is at the end of the loop, just before the while() statement, add a println statement that prints out the value of quotient just before the while()