Where is the sum being stored? [duplicate] - java

This question already has answers here:
Understanding recursion [closed]
(20 answers)
In a recursive statment, how does java store the past values?
(3 answers)
Reversing a String with Recursion in Java
(17 answers)
How are the numbers stored, called, and added in this short recursion example?
(3 answers)
Closed 3 years ago.
I'm attempting to create a method that will calculate the sum of a an integer, as the integer is broken down in to single integers.
E.g. 2546 becomes 2, 5, 4, 6. Then I plus those all together.
2 + 5 + 4 + 6 = 17
The method will run recursively.
I'd made this program non-recursively, but in that one, I had a variable to store a sum of the calculation.
public static int calcSum(int n){
if (n>0)
return ((n%10) + calcSum(n/10));
else
return 0;
}
The program works, I just don't understand how the sum is stored.

The sum is not stored in any variable (unless the caller of the recursive method will store the result in some variable).
The recursive method returns the sum to its caller without storing it in a variable:
return ((n%10) + calcSum(n/10));
or returns 0 if the input is 0.
calcSum(2546) returns 6 + calcSum(254)
calcSum(254) returns 4 + calcSum(25)
calcSum(25) returns 5 + calcSum(2)
calcSum(2) returns 2 + calcSum(0)
calSum(0) returns 0
so when the recursion unwinds:
calcSum(2) returns 2 + 0 == 2
calcSum(25) returns 5 + 2 == 7
calcSum(254) returns 4 + 7 == 11
and finally
calcSum(2546) returns 6 + 11 == 17

Related

Why is (13 / 3 == 4) true? [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 4 years ago.
((13 / 3 == 4) == true)
why is this equals true?
13/3 = 4.3333 and
4.333 is not equaled 4.
Is it about auto cast into an integer? and round?
I tested it in Java EE 8.
Because when you write 13 / 3 you have divided two integer, so the result is only int part, so 4.
In this way you have the next condition 4 == 4 is true

Java Operator Precedence issue for a simple equation [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 4 years ago.
I am trying to implement a simple equation in Java but keep getting the wrong answer apparently due to operator precedence which I am unable to understand.
The equation is:
NewMean = ((N-1) / N) * OldMean + (Xn / N)
in a simple example:
N = 6 ; OldMean = 6 ; Xn = 16
So,
NewMean = 5/6 * 6 + 16/6 = 7.6667 (Correct answer)
but in code implementation on Java i get wrong answer (2.6665):
double NewMean = ((N-1)/N)*oldMean + (Xn/N);
If the N variable is type int, then ((N-1) / N) is computed using integer division and will round 5/6 down to 0. Change N to a floating-point type and you should get the correct answer.

I can not figure out why am i getting the result 2 [duplicate]

This question already has answers here:
How is that x=20;x= ++x + ++x + x++ ;final value of x in java is 65 [duplicate]
(8 answers)
Closed 5 years ago.
Maybe I'm missing out but I can't figure out why I am getting the result 2 in this code:
i = 1;
i = i-- - --i;
System.out.println(i);
In i = i-- - --i you have:
i--, a post-decrement, which retrieves the current value of i (1) and then decrements i to 0
-
--i, a pre-decrement, which decrements i again and retrieves the updated value, -1
So you end up with i = 1 - -1 which is 2.
Needless to say, this sort of thing shows up on (silly) Java tests and such, but should never appear in production code.

What is the difference between (++c) & (c++)? [duplicate]

This question already has answers here:
How do the post increment (i++) and pre increment (++i) operators work in Java?
(14 answers)
postfix and prefix increment operator in a for loop [duplicate]
(2 answers)
Closed 7 years ago.
What is the difference between (++c) and (c++)?
Lets say
c = 4
I know that for (++c) you would increment increment 4 by 1 so 5, but for (c++)?
Both c++ and ++c increment the variable they are applied to. The result returned by c++ is the value of the variable before incrementing, whereas the result returned by ++c is the value of the variable after the increment is applied.
example:
public class IncrementTest{
public static void main(String[] args){
System.out.println("***Post increment test***");
int n = 10;
System.out.println(n); // output 10
System.out.println(n++); // output 10
System.out.println(n); // output 11
System.out.println("***Pre increment test***");
int m = 10;
System.out.println(m); // output 10
System.out.println(++m); // output 11
System.out.println(m); // output 11
}
}
For more info, read this: http://www.javawithus.com/tutorial/increment-and-decrement-operators
Or google post increment and pre increment in java.

Using division in recursion [duplicate]

This question already has answers here:
Understanding how recursive functions work
(18 answers)
Closed 7 years ago.
I have the following program
public static int doSomething(int num){
if(Math.abs(num) > 9){
return (1 + doSomething( num / 10));
}
else{
return 1;
}
}
public static void main(String[] args){
System.out.println(doSomething(333));
}
This is how I understand it. If the number is 333.
333 / 10 gives me 33. Since 33 > 9 it runs the recursive loop again giving me 3.
After 3 it enters the else condition.
I don't understand why it prints 3 as answer.
I am new to java so still trying to understand the basics.
I don't believe the question is a duplicate. My question is much simpler being a beginner to java. Also the question I believe is using javascript not java.
Look, what will return doSomething() if Math.abs(num) < 9? 1? yes you are right. Ok lets start.
doSomething(333) => 1 + doSomething(33) // 33 not less than 9
=> 1 + (1 + doSomething(3)) // now 3 is less than 9
=> 1 + (1 + {1})
=> 3
The recursion is not on 1 + 33, but on 33 only, so it's:
doSomething(333)
= 1 + doSomething(33)
= 1 + 1 + doSomething(3)
= 1 + 1 + 1
= 3

Categories