Results 1 to 10 of 10
  1. #1
    RobertF is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default how to discard remainder on division?

    Java Code:
    int number = 25;
    int newNumber = number/2;
    Hey peeps,
    Anyone know how I can make this so newNumber is a whole number without a remainder but also rounds it down so the outcome is 22?

    Thanks!

    Rob

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    int 25/2 isn't 22. it's 12. i don't get any kind of mathematical process that would result in 22 using division and the numbers 25 and 2.

    what have you tried and gotten as your output so far?

  3. #3
    RobertF is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default

    sorry made typo! I mean 12

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    so what does your code that you gave do? i'm pretty sure you already have your answer.

  5. #5
    RobertF is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default

    ah damn.. sorry, It must be something else in my code :( Been stuck on it all night

    Java Code:
    int decimal = 100;
    int[] array = {};
    
    for(int i=0; decimal>= 0; i++){
    											
    	if(decimal%2 == 0){		
    		array[i]=0;
    	}else{
    		array[i]=1;
    	}
    	decimal= decimal/2;
    }
    I'm trying to get the binary of 100 (in this snippet) into an array but it just doesn't seem to work. It should accept any decimal the user inputs. Not a clue if I'm on the right track, been stuck at it for hours now and my eyes are going blury!
    Last edited by RobertF; 03-13-2009 at 01:15 AM.

  6. #6
    anteup is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    Your original question was "how to discard a remainder?" is there another question you have or is it still the same one??

  7. #7
    RobertF is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default

    It's another one, I realised it must have been something else in my code to why it wasn't working.. I've updated it now though its:

    Java Code:
    		int decimalNum = 100;
    		int[] array;
    		array = new int[38];
    
    		for(int i=0; decimalNum> 0; i++){
    													
    			if(decimalNum%2 == 0){		
    				array[i]=0; System.out.println(array[i]);
    			}else{
    				array[i]=1; System.out.println(array[i]);
    			}
    			decimalNum = decimalNum/2;
    		}
    It works, comes out like:
    array[1] = 0
    array[2] = 0
    array[3] = 1
    array[4] = 0
    array[5] = 0
    array[6] = 1
    array[7] = 1

    but then there's also array [8] to [37] which contain "0" is there anyway of not having to tell the array how many spaces to allocate? so the loop will add to it, instead of replace the current ones?

    the binary is backwards in the array but I can probably sort that out somehow.

  8. #8
    anteup is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    If im understanding what your asking Im going to tell you to instead of using a for loop, because you use a for loop when you know how many times you what your loop to run, try using a while loop, so while(your condition) {
    your code
    }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    It might be easier to manipulate a String and char array via
    Java Code:
    String binaryString = Integer.toBinaryString(decimalNum);

  10. #10
    RobertF is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

Similar Threads

  1. [SOLVED] Modulus/Remainder results
    By antgaudi in forum New To Java
    Replies: 9
    Last Post: 10-13-2008, 03:49 AM
  2. BigInteger remainder results in zero
    By perito in forum New To Java
    Replies: 1
    Last Post: 03-21-2008, 04:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •