Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: DECtoBIN Conversion WITHOUT using String Methods

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    2
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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);


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: DECtoBIN Conversion WITHOUT using String Methods

    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.

    stuck on an endless loop
    How does quotient ever get to be == 0 at the end of the loop?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    2
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: DECtoBIN Conversion WITHOUT using String Methods

    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.

    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()
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. String to Double Conversion
    By suteki73 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 21st, 2013, 05:51 AM
  2. Question about methods, temperature conversion program
    By aivory616 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 23rd, 2012, 01:27 PM
  3. Need help regarding String to int Conversion
    By fredsilvester93 in forum Java Theory & Questions
    Replies: 1
    Last Post: July 20th, 2012, 04:39 PM
  4. numerical conversion methods..
    By Neo in forum Algorithms & Recursion
    Replies: 1
    Last Post: February 17th, 2011, 11:51 PM
  5. numerical conversion methods..
    By chronoz13 in forum Java SE APIs
    Replies: 12
    Last Post: September 27th, 2009, 04:29 AM