Results 1 to 20 of 27
 10272011, 02:36 PM #1
Adding individual numbers together from a random number
Hi, I am jumping back on the Java for a while after doing some web design stuff.
I am trying to improve my usage with the Math class.
What I would like to do is create a random number. Then take that number and add together the individual digits.
For example, if my number generator outputs "765", I would like to add these numbers 7+6+5 together, to get an answer of 18.
Here is what I have so far:
Java Code:import java.util.Random; public class ImproveMaths { public static void main(String[] args) { int randomNum; int sumRandom; Random numGen = new Random(); randomNum = numGen.nextInt(1000); randomNum++; System.out.println(randomNum); sumRandom = } }
Regards,
NM.[A!B]Java
 10272011, 03:50 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,372
 Blog Entries
 7
 Rep Power
 25
Re: Adding individual numbers together from a random number
If a nonnegative number n is less than 10, the result is n itself, otherwise the result will be the last digit of n plus the sum of all digits in n/10. (< hint: recursion is your friend)
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 10272011, 06:31 PM #3Member
 Join Date
 Oct 2011
 Posts
 90
 Rep Power
 0
Re: Adding individual numbers together from a random number
You're going to want to look at each digit individually. I'd treat the output as a string, cast each char as an int, and just add them.
 10272011, 06:36 PM #4Member
 Join Date
 Oct 2011
 Posts
 83
 Rep Power
 0
Re: Adding individual numbers together from a random number
No, this won't work, because if you cast a char to an int, you get the ASCII value of the char, not the numeric value of the char (assuming the char even is numeric). If you subtract '0' from each char, then it would work, but JosAH's solution is much more elegant anyway.
 10272011, 07:00 PM #5Member
 Join Date
 Oct 2011
 Posts
 90
 Rep Power
 0
 10272011, 07:21 PM #6
Re: Adding individual numbers together from a random number
Thanks for all the feedback guys. I was wondering if JosAH would be able to elaborate a little on what you're saying. I've sat here for a good 20 minutes trying to figure out what you mean.
Regards,
NM.[A!B]Java
 10272011, 07:56 PM #7Member
 Join Date
 Oct 2011
 Posts
 83
 Rep Power
 0
Re: Adding individual numbers together from a random number
Consider the number 3795. Suppose you have this number stored in an int variable called n. You can extract the last digit (5) by saying n%10 and you can get the rest of the digits (379) by saying n/10. What Jos was saying is that sum(3795) = 5 + sum(379). Furthermore, you can do this same process repeatedly. So, sum(3795) = 5 + sum(379) = 5 + (9 + sum(37)) = 14 + sum(37) = 14 + (7 + sum(3)) = 21 + sum(3) = 21 + (3 + sum(0)) = 24 + sum(0). The sum of all digits in 0 is obviously 0, so sum(3795) = 24 + sum(0) = 24 + 0 = 24. Now see if you can translate that into Java code. As Jos said, a recursive method would be one way to do it, though I personally would use a while loop instead (uses slightly less memory).
 10272011, 10:01 PM #8
Re: Adding individual numbers together from a random number
Ah thank you for that explanation. I think I understand, so because you are essentially dividing 3795 by 10, it can only go in 379 times, so that leaves the remainder of 5. Then you save that in a variable then do the same for the rest?
[A!B]Java
 10282011, 12:17 AM #9Member
 Join Date
 Oct 2011
 Posts
 83
 Rep Power
 0
Re: Adding individual numbers together from a random number
Pretty much, yeah.
 10282011, 01:15 AM #10Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 14
Re: Adding individual numbers together from a random number
Somewhat offtopic in as much as I agree with using numeric techniques, but...
Character has a method for obtaining the numeric value associated with a digit. '0' is not wrong, but, quick, who can prove it with a URL? The Character method is also nice because it is not indoarabic chauvanist  it will deal happily with a Roman vee or <your favourite locale>.
 10282011, 01:22 AM #11
Re: Adding individual numbers together from a random number
Character.digit
No url, look it up in the API yourself ;)
 10282011, 01:39 AM #12
Re: Adding individual numbers together from a random number
Ok, so this is what I have so far:
Java Code:import java.util.Random; public class DigitSum { public static void main(String[] args) { int randomNum; int a; int b; int c; Random numGen = new Random(); randomNum = numGen.nextInt(1000); randomNum++; System.out.println(randomNum); a = randomNum % 10; System.out.println(a); System.out.println(randomNum); } }
Regards,
NM.[A!B]Java
 10282011, 01:45 AM #13
 10282011, 01:56 AM #14
Re: Adding individual numbers together from a random number
Awesome, I got it!
Thanks so much for the help, couldn't have figured it out with all of your help.
Here's the finished code:
Java Code:import java.util.Random; public class DigitSum { public static void main(String[] args) { int randomNum; int a; int b; int c; int d; int e; Random numGen = new Random(); randomNum = numGen.nextInt(1000); randomNum++; System.out.println(randomNum); a = randomNum % 10; b = randomNum / 10; c = b % 10; d = b / 10; e = a + c + d; System.out.println(a); System.out.println(c); System.out.println(d); System.out.println(""); System.out.println(e); } }
NM.[A!B]Java
 10282011, 01:58 AM #15
Re: Adding individual numbers together from a random number
My next task I am going to try and create a method where I can send the random number and it calculates the sums then sends me back the result.
Regards,
NM.[A!B]Java
 10282011, 02:11 AM #16
Re: Adding individual numbers together from a random number
Jos mentioned that this problem was perfect for recursion. If you are not familiar with recursion then you would just use a while loop. Both solutions would work for a number of any length. Your solution is extremely limited.
 10282011, 07:54 PM #17
Re: Adding individual numbers together from a random number
See I literally only wanted to be able to add the numbers together from a generated number. What would be the use of the while loop? I don't understand how it would benefit me.
Regards,
NM.[A!B]Java
 10282011, 08:01 PM #18
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,372
 Blog Entries
 7
 Rep Power
 25
Re: Adding individual numbers together from a random number
Either use this:
Java Code:int sumOfDigits(int n) { if (n < 10) return n; return n%10+sumOfDigits(n/10); }
Java Code:int sumOfDigits(int n) { int s; for (s= 0; n >= 10; s+= n%10, n/= 10); return s+n; }
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 10282011, 08:03 PM #19Member
 Join Date
 Oct 2011
 Posts
 83
 Rep Power
 0
Re: Adding individual numbers together from a random number
A while loop would make the program more flexible, as well as shorter. The way you have it now, it only works if the number is 3 digits or less.
 10282011, 08:47 PM #20
Similar Threads

Extracting individual numbers from a long integer
By CurbYourEnthusiasm in forum New To JavaReplies: 6Last Post: 02182011, 04:53 PM 
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04192009, 05:50 PM 
Calculating Individual Numbers
By TheKnight in forum New To JavaReplies: 2Last Post: 01302009, 01:51 AM 
trying to add up random numbers into one number
By pjr5043 in forum New To JavaReplies: 4Last Post: 09152008, 02:20 PM 
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01172008, 11:44 PM
Bookmarks