Results 1 to 15 of 15
Thread: Removing digits
 03262011, 03:01 AM #1Member
 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); } }
 03262011, 03:37 AM #2Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,565
 Rep Power
 12
I know that my output isn't correct
Could you describe what the output is?
 03262011, 03:37 AM #3Senior Member
 Join Date
 Jul 2009
 Posts
 1,226
 Rep Power
 6
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.
 03262011, 03:55 AM #4Member
 Join Date
 Sep 2010
 Location
 Southwest Missouri
 Posts
 97
 Rep Power
 0
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.
 03262011, 03:56 AM #5Member
 Join Date
 Sep 2010
 Location
 Southwest Missouri
 Posts
 97
 Rep Power
 0
 03262011, 04:00 AM #6Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,565
 Rep Power
 12
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?
 03262011, 04:35 AM #7Member
 Join Date
 Sep 2010
 Location
 Southwest Missouri
 Posts
 97
 Rep Power
 0
 03262011, 04:53 AM #8Senior Member
 Join Date
 Jul 2009
 Posts
 1,226
 Rep Power
 6
That works if you know the number but I don't know how I would do that with a randomly generated 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.
 03262011, 08:42 AM #9Senior Member
 Join Date
 Nov 2010
 Posts
 210
 Rep Power
 4
Java Code:int ones = tens / 1;
Instead, use the modulus operator. To isolate the last digit, use:
Java Code:int ones = n % 10;
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.
 03262011, 08:57 AM #10
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 8
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.
 03262011, 04:29 PM #11Member
 Join Date
 Sep 2010
 Location
 Southwest Missouri
 Posts
 97
 Rep Power
 0
 03262011, 04:32 PM #12Member
 Join Date
 Sep 2010
 Location
 Southwest Missouri
 Posts
 97
 Rep Power
 0
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;
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.
 03262011, 04:35 PM #13Member
 Join Date
 Sep 2010
 Location
 Southwest Missouri
 Posts
 97
 Rep Power
 0
 03262011, 09:12 PM #14Member
 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)); } }
 03262011, 09:18 PM #15Member
 Join Date
 Feb 2011
 Posts
 71
 Rep Power
 0
Similar Threads

code for five digits
By TheAisBack in forum New To JavaReplies: 2Last Post: 02132011, 02:25 AM 
Value should be 7 or 8 digits .If 8 digits, the last should be a character
By renu in forum New To JavaReplies: 1Last Post: 01192011, 09:23 PM 
Digits of an integer.
By Allgorythm in forum New To JavaReplies: 8Last Post: 01012010, 02:34 AM 
split() by digits
By RobertF in forum New To JavaReplies: 2Last Post: 03122009, 02:16 AM 
sum of digits depreciation
By jleas in forum New To JavaReplies: 13Last Post: 11092008, 01:37 PM
Bookmarks