# Removing digits

• 03-26-2011, 04:01 AM
jim01
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.

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);     } }```
• 03-26-2011, 04:37 AM
pbrockway2
Quote:

I know that my output isn't correct

Could you describe what the output is?
• 03-26-2011, 04:37 AM
camickr
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.
• 03-26-2011, 04:55 AM
jim01
Quote:

Originally Posted by pbrockway2
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.
• 03-26-2011, 04:56 AM
jim01
Quote:

Originally Posted by camickr
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.
• 03-26-2011, 05:00 AM
pbrockway2
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?
• 03-26-2011, 05:35 AM
jim01
Quote:

Originally Posted by pbrockway2
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.
• 03-26-2011, 05:53 AM
camickr
Quote:

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.
• 03-26-2011, 09:42 AM
Iron Lion
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:

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.
• 03-26-2011, 09:57 AM
sunde887
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.
• 03-26-2011, 05:29 PM
jim01
Quote:

Originally Posted by camickr
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.
• 03-26-2011, 05:32 PM
jim01
Quote:

Originally Posted by Iron Lion
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.

Quote:

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

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.
• 03-26-2011, 05:35 PM
jim01
Quote:

Originally Posted by sunde887
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.
• 03-26-2011, 10:12 PM
jim01
Got it!!! It took forever to figure it out but I finally got it. Thank you all for your help! Here's the code:

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));     } }```
• 03-26-2011, 10:18 PM
jjth39347
Congrats, mark it as solved by clicking thread tools up top