1. Member Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0

## 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);
}
}```  Reply With Quote

2. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## I know that my output isn't correct

Could you describe what the output is?  Reply With Quote

3. Senior Member Join Date
Jul 2009
Posts
1,236
Rep Power
13

## 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.  Reply With Quote

4. Member Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0

##  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.  Reply With Quote

5. Member Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0

##  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.  Reply With Quote

6. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## 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?  Reply With Quote

7. Member Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0

##  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.  Reply With Quote

8. Senior Member Join Date
Jul 2009
Posts
1,236
Rep Power
13

## 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.  Reply With Quote

9. Senior Member Join Date
Nov 2010
Posts
210
Rep Power
10

## 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.  Reply With Quote

10. ## 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.  Reply With Quote

11. Member Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0

##  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.  Reply With Quote

12. Member Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0

##  Originally Posted by Iron Lion 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.  Reply With Quote

13. Member Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0

##  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.  Reply With Quote

14. Member Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0

## 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));
}
}```  Reply With Quote

15. Member Join Date
Feb 2011
Posts
71
Rep Power
0

## Congrats, mark it as solved by clicking thread tools up top  Reply With Quote

#### Posting Permissions

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