# DECtoBIN Conversion WITHOUT using String Methods

• October 28th, 2013, 02:09 PM
Putsie
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.

//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);
• October 28th, 2013, 02:54 PM
Norm
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

[code=java]
[/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?
• October 28th, 2013, 04:09 PM
Putsie
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
• October 28th, 2013, 04:18 PM
Norm
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?