Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default 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 = 
    	 
    	 
    	 
    	 
    	 }
    
    }
    Thanks in advance for any feedback/guidance you may have.

    Regards,

    NM.
    [A!B]Java

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default Re: Adding individual numbers together from a random number

    If a non-negative 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default 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.

  4. #4
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Adding individual numbers together from a random number

    Quote Originally Posted by joeyvitoro View Post
    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.
    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.

  5. #5
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: Adding individual numbers together from a random number

    Quote Originally Posted by DiamondSoul View Post
    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.
    Good point. Substring would work. Not as elegant, but I'm not a fancy guy :)

  6. #6
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default 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

  7. #7
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default 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).

  8. #8
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default 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

  9. #9
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Adding individual numbers together from a random number

    Pretty much, yeah.

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

    Default 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 indo-arabic chauvanist - it will deal happily with a Roman vee or <your favourite locale>.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Adding individual numbers together from a random number

    Character.digit

    No url, look it up in the API yourself ;)

  12. #12
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default 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);
    	 
    	 
    	 }
    
    }
    I extract the end digit but the variable holding the random generated number stays at the same value. I'm racking my brains to try and figure this out, but I just cannot.

    Regards,

    NM.
    [A!B]Java

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Adding individual numbers together from a random number

    Quote Originally Posted by DiamondSoul View Post
    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.
    You only followed half of the suggestion.

  14. #14
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default 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);
    
    	 }
    
    }
    Regards,

    NM.
    [A!B]Java

  15. #15
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default 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

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default 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.

  17. #17
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default 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

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default 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);
    }
    or use a loop:

    Java Code:
    int sumOfDigits(int n) {
       int s;
       for (s= 0; n >= 10; s+= n%10, n/= 10);
       return s+n;
    }
    I picked a for-loop, just because I like it that way but it could've been a while-loop as well ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default 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.

  20. #20
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Re: Adding individual numbers together from a random number

    With the first bit of code, is line 3 calling the method from within the method?

    I think I will use the second code, but I'm just trying to work out the method first before adding it.

    Thanks for all helps.

    Regards,
    NM
    [A!B]Java

Page 1 of 2 12 LastLast

Similar Threads

  1. Extracting individual numbers from a long integer
    By CurbYourEnthusiasm in forum New To Java
    Replies: 6
    Last Post: 02-18-2011, 04:53 PM
  2. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  3. Calculating Individual Numbers
    By TheKnight in forum New To Java
    Replies: 2
    Last Post: 01-30-2009, 01:51 AM
  4. trying to add up random numbers into one number
    By pjr5043 in forum New To Java
    Replies: 4
    Last Post: 09-15-2008, 03:20 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 11:44 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
  •