Re: Adding individual numbers together from a random number

Quote:

Originally Posted by

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

If for instance the amount of random digits never went above 3, would you still use the methods above, or would you create something more simple? I'm trying to work out these methods and i'm guessing that I could pass these methods a number with 6 digits and it would calculate the sum, whereas mine, like you say will only calculate 3 or less?

Regards,

NM.

Re: Adding individual numbers together from a random number

Quote:

Originally Posted by

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

Yup, the method is calling itself, that's what recursive methods do; it calls itself with a smaller value of n (nearer to zero) so it always terminates because it doesn't call itself if n < 10 (one digit).

kind regards,

Jos

Re: Adding individual numbers together from a random number

Wow some intellectual methods here, I think i'll be here in a few hours working it out haha.

Thanks though. I think i'll read over my other threads here regarding passing in variables to methods because it's all new again.

Regards,

NM.

Re: Adding individual numbers together from a random number

This is what I have. Just looked over my old thread for my Math game and found loads of assistance :(giggle): so it is working perfect and I hope my explanation of this is somewhat near correct.

Code:

`import java.util.Random;`

public class DigitSum {

public static void main(String[] args) {

int rNum;

int result;

Random numGen = new Random();

rNum = numGen.nextInt(1000);

rNum++;

System.out.println("Generated Number: " + rNum);

System.out.println("");

System.out.println("The value of this variable gets passed into our calculate method, where the ");

System.out.println("calculations are done before being returned to the main method and printing ");

System.out.println("the sum of the numbers.");

System.out.println("");

result = calculate(rNum);

System.out.println(result);

}

static int calculate(int n) {

if (n < 10) return n;

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

}

}

Many thanks,

NM.

Re: Adding individual numbers together from a random number

Code:

`import java.util.Random;`

public class DigitSum {

public static void main(String[] args) {

int rNum;

int result;

Random numGen = new Random();

rNum = numGen.nextInt(1000);

rNum++;

System.out.println("");

System.out.println("Generated Number: " + rNum);

System.out.println("");

result = calculate(rNum);

System.out.println(result);

}

static int calculate(int n) {

if (n < 10) return n;

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

}

}

AHA i have worked it out!!!

Ok so, what this all does is the value of the variable rNum is passed into the calculate method. The calculate method accepts a parameter, an int variable 'n'. I don't know how but i'm sure n holds the value of rNum. Anyway, the first line checks if the number is below 10, if so it returns n because it is only a single digit so there is no need to calculate on it. If it's larger we go to the second line.

As an example let's say n holds 246. So we do 246%10 which is 6. we then go back into the calculate method with the result of n/10 which is 24. So now at the start of our method again, n now equals 24, so again we get to the second line 24%10 equals 4. Meanwhile these values are being added together somewhere along the way. It then takes the value of 24/10 which is 2 back into the method. Because n equals 2, the if statement is true so we automatically return n back to the calling method, where the result of the 3 added digits is saved in the result variable before being printed to the page?

I'm sure i'm nearly there. Could someone explain at what point do the 3 numbers get added to one another and where does the value of the expression n%10 get stored? Because when we re-enter the method later on that line of code, we do another calculation n/10but again n is the original number.

Regards,

NM>

Re: Adding individual numbers together from a random number

Quote:

Originally Posted by

**Nanomech** I'm sure i'm nearly there. Could someone explain at what point do the 3 numbers get added to one another and where does the value of the expression n%10 get stored? Because when we re-enter the method later on that line of code, we do another calculation n/10but again n is the original number.

That's the mechanism of the Java stack; when you call the method calculate with a parameter 246, that number is put on the data stack and that method calls it 'n'. If n < 10 the method simply returns that value; otherwise it stores the value n%10 in a register, pushes the value 246/10 ( == 24) on the stack and calls itself; it's new 'incarnation' calls that value 'n' again. It is larger than 10 so it stores the value n%10 ( == 24%10 == 4) in a register and pushes the value n/10 ( == 24/10 == 2) on the stack again and calls itself. At this point the values 246, 24 and 2 are pushed on the stack and the latest 'incarnation' of the method decides to return that value because it's less than 10. The previous incarnation receives that return value (2) and adds it to the value (4) it kept in a register. It returns 6 which is received by the first incarnation of the method which add the return value 6 to the value it has kept in a register (also 6) and returns the result: 12. More complex recursion can make you dizzy ;-)

kind regards,

Jos

Re: Adding individual numbers together from a random number

Hehe I kinda understand what you mean. Like I say this method is quite sophisticated for someone of my standing but of course this information is invaluable for me to have someone explaining this.

Thanks once again to everyone who guided me with this problem.

Regards,

NM.