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:-

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.

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

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.

Re: Adding individual numbers together from a random number

Quote:

Originally Posted by

**joeyvitoro** 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.

Re: Adding individual numbers together from a random number

Quote:

Originally Posted by

**DiamondSoul** 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 :)

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.

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

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?

Re: Adding individual numbers together from a random number

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

Re: Adding individual numbers together from a random number

Character.digit

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

Re: Adding individual numbers together from a random number

Ok, so this is what I have so far:

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.

Re: Adding individual numbers together from a random number

Quote:

Originally Posted by

**DiamondSoul** 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.

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:

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.

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.

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.

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.

Re: Adding individual numbers together from a random number

Either use this:

Code:

`int sumOfDigits(int n) {`

if (n < 10) return n;

return n%10+sumOfDigits(n/10);

}

or use a loop:

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

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.

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