Results 1 to 15 of 15

Thread: Removing digits

  1. #1
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default Removing digits

    I am trying to write a program that randomly generates a number up to five digits and then prints the sum of the digits. I know how to generate the number and I think I am removing the first number correctly, but I get lost after that. Here's what I have tried. I know that my output isn't correct but I have it set up like this so that I can see if I am pulling the numbers out correctly. Once I have that figured out I'll change the output to sum the individual digits.




    Java Code:
    public class ProgrammingProblemOne {
    
        public static void main(String[] args) {
    
        	//generate random number
    	int n = (int)Math.floor(Math.random()*100000+1);
    
            //pull out first digit
    	int tenThousands = n / 10000;
    
            //pull out second digit
    	int thousands = n / 1000;
    
            /pull out third digit
    	int hundreds = n / 100;
    
            //pull out fourth digit
    	int tens =  n / 10;
    
            //pull out fifth digit
    	int ones = tens / 1;
    
        	System.out.println("Generated number: " + n);
    
        	System.out.println("First digit: " + tenThousands);
    
        	System.out.println("Second digit: " + thousands);
    
    	System.out.println("Third digit : " + hundreds);
    
    	System.out.println("Fouth digit: " + tens);
    
    	System.out.println("Fifth digit: " + ones);
        }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I know that my output isn't correct

    Could you describe what the output is?

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    If you have a 3 digit number like "321", then you know you have 3 hundreds.

    To find out how many tens you have, you need to subtract 300 from 321 and you have 21 left. Now you can find out how many tens you have.

  4. #4
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Could you describe what the output is?
    The output is eventually going to be the sum of the individual digits within the randomly generated number. For instance if the number is 97634, then I need to add 9 + 7 + 6 + 3 + 4.

    I know how to do that part. It's breaking the numbers down into their single digits that I'm having problems with. So to help with that, I have made the output to see whether I am successfully pulling out the single digits.

  5. #5
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    If you have a 3 digit number like "321", then you know you have 3 hundreds.

    To find out how many tens you have, you need to subtract 300 from 321 and you have 21 left. Now you can find out how many tens you have.
    That works if you know the number but I don't know how I would do that with a randomly generated number.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I understand what you want the output to be eventually. I was wondering if you could describe what the output is. You said the output is not correct, but what is incorrect about it?

  7. #7
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    I understand what you want the output to be eventually. I was wondering if you could describe what the output is. You said the output is not correct, but what is incorrect about it?
    When I said the output was incorrect, I meant that it wasn't the correct output to to sum the digits.

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    That works if you know the number but I don't know how I would do that with a randomly generated number.
    You do know the number.

    In my example you start by dividing the random number by 100 to get the first digit. Then you multiply that digit by 100 to get the amount to subtract from the random number. You repeat for the 10's until all you are left with is a single digit.

  9. #9
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    Java Code:
    int ones = tens / 1;
    What do you expect will happen when you divide something by 1?

    Instead, use the modulus operator. To isolate the last digit, use:

    Java Code:
    int ones = n % 10;
    The other digits can be found with a combination of division and modulus operations.

    Rather than having five separate variables for the digits, I'd recommend using an array. That way, you can loop over it, and if you write your code properly it can adapt to numbers of any length instead of being fixed at 5 digits.

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    As iron lion stated, can easily be done with a loop, a 5 item array and %(modulus) and /(division). That being said, give it a shot, start by a number like 321 and try and isolate the 1, then change the number to 32. Do this with %, / and you are mostly done.

  11. #11
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    You do know the number.

    In my example you start by dividing the random number by 100 to get the first digit. Then you multiply that digit by 100 to get the amount to subtract from the random number. You repeat for the 10's until all you are left with is a single digit.
    Ok. I think I get it. I'll work with that and see what I can come up with. Thank you very much.

  12. #12
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by Iron Lion View Post
    Java Code:
    int ones = tens / 1;
    What do you expect will happen when you divide something by 1?
    LOL! I guess that was pretty stupid wasn't it. I'm not sure what I was thinking when I did that.

    Instead, use the modulus operator. To isolate the last digit, use:

    Java Code:
    int ones = n % 10;
    The other digits can be found with a combination of division and modulus operations.

    Rather than having five separate variables for the digits, I'd recommend using an array. That way, you can loop over it, and if you write your code properly it can adapt to numbers of any length instead of being fixed at 5 digits.
    I haven't made it to arrays yet. Thank you for your help. I think I may be able to figure it out now.

  13. #13
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    As iron lion stated, can easily be done with a loop, a 5 item array and %(modulus) and /(division). That being said, give it a shot, start by a number like 321 and try and isolate the 1, then change the number to 32. Do this with %, / and you are mostly done.
    I'm not that far along. I haven't made it to arrays yet.

    Thank you for your help. I think that with everyone's advice I have enough information to experiment and figure it out.

    Thanks to all.

  14. #14
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Got it!!! It took forever to figure it out but I finally got it. Thank you all for your help! Here's the code:

    Java Code:
    public class ProgrammingProblemOne {
    
        public static void main(String[] args) {
    
        	//generate random number
            int n = (int)Math.floor(Math.random()*100000+1);
    
    	//isolate first digit
    	int tenThousands = n / 10000;
    
    	//isolate second digit
    	int a = n / 1000;
    	int thousands = a % 10;
    
    	//isolate third data
    	int b = n /100;
    	int hundreds = b % 10;
    
    	//isolate fourth digit
    	int c = n / 10;
    	int tens = c % 10;
    
    	//fifth digit
    	int d = tens % 10;
            int ones = n % 10;
    
    	//output
        	System.out.println("Generated number: " + n);
        	System.out.println("First digit: " + tenThousands);
        	System.out.println("Second digit: " + thousands);
            System.out.println("Third digit : " + hundreds);
    	System.out.println("Fourth digit: " + tens);
    	System.out.println("Fifth digit: " + ones);
    	System.out.println("The sum of all digits is " + (tenThousands +
                   thousands + hundreds + tens + ones));
        }
    }

  15. #15
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    Congrats, mark it as solved by clicking thread tools up top

Similar Threads

  1. code for five digits
    By TheAisBack in forum New To Java
    Replies: 2
    Last Post: 02-13-2011, 02:25 AM
  2. Replies: 1
    Last Post: 01-19-2011, 09:23 PM
  3. Digits of an integer.
    By Allgorythm in forum New To Java
    Replies: 8
    Last Post: 01-01-2010, 02:34 AM
  4. split() by digits
    By RobertF in forum New To Java
    Replies: 2
    Last Post: 03-12-2009, 02:16 AM
  5. sum of digits depreciation
    By jleas in forum New To Java
    Replies: 13
    Last Post: 11-09-2008, 01:37 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
  •