Java method executes last print statement three times even though the method is only called once
up vote
0
down vote
favorite
I'm trying to figure out why this mystery method in Java prints out "43211234". I understand how the program reaches "43211" but I'm not sure how the last System.out.print(x % 10) after the "if" is ran 3 times and for each time, it brings the value of x back to the value it was previous to its current one until it reaches "1234" as the value of x. Could this have something to do with recursion since the method is being called in the "if" 3 times? I'm guessing it's something along those lines because the last executes exactly 3 times as well. I would greatly appreciate your help. Thanks.
class Main
public static void main(String args)
mystery(1234);
public static void mystery(int x)
System.out.print(x % 10);
if((x / 10) != 0)
mystery(x / 10);
System.out.print(x % 10);
java
add a comment |
up vote
0
down vote
favorite
I'm trying to figure out why this mystery method in Java prints out "43211234". I understand how the program reaches "43211" but I'm not sure how the last System.out.print(x % 10) after the "if" is ran 3 times and for each time, it brings the value of x back to the value it was previous to its current one until it reaches "1234" as the value of x. Could this have something to do with recursion since the method is being called in the "if" 3 times? I'm guessing it's something along those lines because the last executes exactly 3 times as well. I would greatly appreciate your help. Thanks.
class Main
public static void main(String args)
mystery(1234);
public static void mystery(int x)
System.out.print(x % 10);
if((x / 10) != 0)
mystery(x / 10);
System.out.print(x % 10);
java
Remove line no. 10System.out.print(x % 10);
& it would be a fine program to reverse the digits of a given number.
– Abhinav
Nov 10 at 14:19
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to figure out why this mystery method in Java prints out "43211234". I understand how the program reaches "43211" but I'm not sure how the last System.out.print(x % 10) after the "if" is ran 3 times and for each time, it brings the value of x back to the value it was previous to its current one until it reaches "1234" as the value of x. Could this have something to do with recursion since the method is being called in the "if" 3 times? I'm guessing it's something along those lines because the last executes exactly 3 times as well. I would greatly appreciate your help. Thanks.
class Main
public static void main(String args)
mystery(1234);
public static void mystery(int x)
System.out.print(x % 10);
if((x / 10) != 0)
mystery(x / 10);
System.out.print(x % 10);
java
I'm trying to figure out why this mystery method in Java prints out "43211234". I understand how the program reaches "43211" but I'm not sure how the last System.out.print(x % 10) after the "if" is ran 3 times and for each time, it brings the value of x back to the value it was previous to its current one until it reaches "1234" as the value of x. Could this have something to do with recursion since the method is being called in the "if" 3 times? I'm guessing it's something along those lines because the last executes exactly 3 times as well. I would greatly appreciate your help. Thanks.
class Main
public static void main(String args)
mystery(1234);
public static void mystery(int x)
System.out.print(x % 10);
if((x / 10) != 0)
mystery(x / 10);
System.out.print(x % 10);
java
java
asked Nov 10 at 14:10
Sahil Bagga
52
52
Remove line no. 10System.out.print(x % 10);
& it would be a fine program to reverse the digits of a given number.
– Abhinav
Nov 10 at 14:19
add a comment |
Remove line no. 10System.out.print(x % 10);
& it would be a fine program to reverse the digits of a given number.
– Abhinav
Nov 10 at 14:19
Remove line no. 10
System.out.print(x % 10);
& it would be a fine program to reverse the digits of a given number.– Abhinav
Nov 10 at 14:19
Remove line no. 10
System.out.print(x % 10);
& it would be a fine program to reverse the digits of a given number.– Abhinav
Nov 10 at 14:19
add a comment |
4 Answers
4
active
oldest
votes
up vote
1
down vote
accepted
I understand how the program reaches "43211"
so you know what recursion is.
Everytime mystery()
is called, the 1st print
is called and then it calls (recursively) itself, before the 2nd print
.
When the recursion stops because (x / 10) != 0
is false
, the 2nd print
is called for the 1st time and then goes back to the previous unfinished recursive calls and executes the remaining print
for each one.
This was what I suspected was happening. Thank you!
– Sahil Bagga
Nov 10 at 14:35
add a comment |
up vote
2
down vote
Not sure if my answer will be any more useful than the previous ones, but I'll try. So, basically, your program uses 2 types of recursion: backward recursion and forward recursion. My answer here is not to describe them to you, but to give you a starting point for more information on them.
Let's trace your program's execution:
mystery(1234) -> print(4) -> mystery(123); At this point, the System.out.print(x % 10);
at the end of the method has not been called yet since your program has gone further in the recursion. It will be executed once the program returns from deep inside your recursion, and will be executed with whatever's left.
mystery(1234):
print(4);
mystery(123):
print(3);
mystery(12):
print(2);
mystery(1);
print(1);
print(1); //This is the first System.out.print(x % 10); from the end of the method, that is executed
print(2);
print(3);
print(4);
add a comment |
up vote
1
down vote
Your mystery()
method does the following:
- print the final digit of the input number (
num
% 10 gives the last digit) - make a recursive call
mystery(x / 10)
, assumingx / 10
is not zero - then on the way out from the recursion, print the final digit of the input again
Putting this together, with an input of 1234
, it means we would print those digits in reverse, then print them again in order.
If this answer still leaves you with doubts, I suggest running your code, beginning with a two digit input like 12
, until it is clear what is happening.
But for the last print statement, the value of x goes from 1 (I know it reaches the last line because 1 / 10 == 0) to 12 to 123 to 1234. How and why does it execute the last print statement 3 and build up the value of x again?
– Sahil Bagga
Nov 10 at 14:25
It prints4
,3
,2
,1
, and when1
gets printed, the input is also1
, which is zero when divided by 10. So, no subsequent calls tomystery
are made, and instead, everything backs out, starting with printing1
again, then2
, etc.
– Tim Biegeleisen
Nov 10 at 14:27
@TimBiegeleisen I agree with that.
– forpas
Nov 10 at 14:46
add a comment |
up vote
0
down vote
This is your recursion stack. This is perfectly fine.
mystery(x / 10); input 1234 prints 4
-> mystery(x / 10); input 123 prints 3
-> mystery(x / 10); input 12 prints 2
-> mystery(x / 10); input 1 prints 1
Just make sure your remove the second sysout that you that you in your code. That is the reason it is printing the same numbers in again.
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I understand how the program reaches "43211"
so you know what recursion is.
Everytime mystery()
is called, the 1st print
is called and then it calls (recursively) itself, before the 2nd print
.
When the recursion stops because (x / 10) != 0
is false
, the 2nd print
is called for the 1st time and then goes back to the previous unfinished recursive calls and executes the remaining print
for each one.
This was what I suspected was happening. Thank you!
– Sahil Bagga
Nov 10 at 14:35
add a comment |
up vote
1
down vote
accepted
I understand how the program reaches "43211"
so you know what recursion is.
Everytime mystery()
is called, the 1st print
is called and then it calls (recursively) itself, before the 2nd print
.
When the recursion stops because (x / 10) != 0
is false
, the 2nd print
is called for the 1st time and then goes back to the previous unfinished recursive calls and executes the remaining print
for each one.
This was what I suspected was happening. Thank you!
– Sahil Bagga
Nov 10 at 14:35
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I understand how the program reaches "43211"
so you know what recursion is.
Everytime mystery()
is called, the 1st print
is called and then it calls (recursively) itself, before the 2nd print
.
When the recursion stops because (x / 10) != 0
is false
, the 2nd print
is called for the 1st time and then goes back to the previous unfinished recursive calls and executes the remaining print
for each one.
I understand how the program reaches "43211"
so you know what recursion is.
Everytime mystery()
is called, the 1st print
is called and then it calls (recursively) itself, before the 2nd print
.
When the recursion stops because (x / 10) != 0
is false
, the 2nd print
is called for the 1st time and then goes back to the previous unfinished recursive calls and executes the remaining print
for each one.
edited Nov 10 at 14:33
answered Nov 10 at 14:21
forpas
5,8831217
5,8831217
This was what I suspected was happening. Thank you!
– Sahil Bagga
Nov 10 at 14:35
add a comment |
This was what I suspected was happening. Thank you!
– Sahil Bagga
Nov 10 at 14:35
This was what I suspected was happening. Thank you!
– Sahil Bagga
Nov 10 at 14:35
This was what I suspected was happening. Thank you!
– Sahil Bagga
Nov 10 at 14:35
add a comment |
up vote
2
down vote
Not sure if my answer will be any more useful than the previous ones, but I'll try. So, basically, your program uses 2 types of recursion: backward recursion and forward recursion. My answer here is not to describe them to you, but to give you a starting point for more information on them.
Let's trace your program's execution:
mystery(1234) -> print(4) -> mystery(123); At this point, the System.out.print(x % 10);
at the end of the method has not been called yet since your program has gone further in the recursion. It will be executed once the program returns from deep inside your recursion, and will be executed with whatever's left.
mystery(1234):
print(4);
mystery(123):
print(3);
mystery(12):
print(2);
mystery(1);
print(1);
print(1); //This is the first System.out.print(x % 10); from the end of the method, that is executed
print(2);
print(3);
print(4);
add a comment |
up vote
2
down vote
Not sure if my answer will be any more useful than the previous ones, but I'll try. So, basically, your program uses 2 types of recursion: backward recursion and forward recursion. My answer here is not to describe them to you, but to give you a starting point for more information on them.
Let's trace your program's execution:
mystery(1234) -> print(4) -> mystery(123); At this point, the System.out.print(x % 10);
at the end of the method has not been called yet since your program has gone further in the recursion. It will be executed once the program returns from deep inside your recursion, and will be executed with whatever's left.
mystery(1234):
print(4);
mystery(123):
print(3);
mystery(12):
print(2);
mystery(1);
print(1);
print(1); //This is the first System.out.print(x % 10); from the end of the method, that is executed
print(2);
print(3);
print(4);
add a comment |
up vote
2
down vote
up vote
2
down vote
Not sure if my answer will be any more useful than the previous ones, but I'll try. So, basically, your program uses 2 types of recursion: backward recursion and forward recursion. My answer here is not to describe them to you, but to give you a starting point for more information on them.
Let's trace your program's execution:
mystery(1234) -> print(4) -> mystery(123); At this point, the System.out.print(x % 10);
at the end of the method has not been called yet since your program has gone further in the recursion. It will be executed once the program returns from deep inside your recursion, and will be executed with whatever's left.
mystery(1234):
print(4);
mystery(123):
print(3);
mystery(12):
print(2);
mystery(1);
print(1);
print(1); //This is the first System.out.print(x % 10); from the end of the method, that is executed
print(2);
print(3);
print(4);
Not sure if my answer will be any more useful than the previous ones, but I'll try. So, basically, your program uses 2 types of recursion: backward recursion and forward recursion. My answer here is not to describe them to you, but to give you a starting point for more information on them.
Let's trace your program's execution:
mystery(1234) -> print(4) -> mystery(123); At this point, the System.out.print(x % 10);
at the end of the method has not been called yet since your program has gone further in the recursion. It will be executed once the program returns from deep inside your recursion, and will be executed with whatever's left.
mystery(1234):
print(4);
mystery(123):
print(3);
mystery(12):
print(2);
mystery(1);
print(1);
print(1); //This is the first System.out.print(x % 10); from the end of the method, that is executed
print(2);
print(3);
print(4);
answered Nov 10 at 14:33
Rakirnd
714
714
add a comment |
add a comment |
up vote
1
down vote
Your mystery()
method does the following:
- print the final digit of the input number (
num
% 10 gives the last digit) - make a recursive call
mystery(x / 10)
, assumingx / 10
is not zero - then on the way out from the recursion, print the final digit of the input again
Putting this together, with an input of 1234
, it means we would print those digits in reverse, then print them again in order.
If this answer still leaves you with doubts, I suggest running your code, beginning with a two digit input like 12
, until it is clear what is happening.
But for the last print statement, the value of x goes from 1 (I know it reaches the last line because 1 / 10 == 0) to 12 to 123 to 1234. How and why does it execute the last print statement 3 and build up the value of x again?
– Sahil Bagga
Nov 10 at 14:25
It prints4
,3
,2
,1
, and when1
gets printed, the input is also1
, which is zero when divided by 10. So, no subsequent calls tomystery
are made, and instead, everything backs out, starting with printing1
again, then2
, etc.
– Tim Biegeleisen
Nov 10 at 14:27
@TimBiegeleisen I agree with that.
– forpas
Nov 10 at 14:46
add a comment |
up vote
1
down vote
Your mystery()
method does the following:
- print the final digit of the input number (
num
% 10 gives the last digit) - make a recursive call
mystery(x / 10)
, assumingx / 10
is not zero - then on the way out from the recursion, print the final digit of the input again
Putting this together, with an input of 1234
, it means we would print those digits in reverse, then print them again in order.
If this answer still leaves you with doubts, I suggest running your code, beginning with a two digit input like 12
, until it is clear what is happening.
But for the last print statement, the value of x goes from 1 (I know it reaches the last line because 1 / 10 == 0) to 12 to 123 to 1234. How and why does it execute the last print statement 3 and build up the value of x again?
– Sahil Bagga
Nov 10 at 14:25
It prints4
,3
,2
,1
, and when1
gets printed, the input is also1
, which is zero when divided by 10. So, no subsequent calls tomystery
are made, and instead, everything backs out, starting with printing1
again, then2
, etc.
– Tim Biegeleisen
Nov 10 at 14:27
@TimBiegeleisen I agree with that.
– forpas
Nov 10 at 14:46
add a comment |
up vote
1
down vote
up vote
1
down vote
Your mystery()
method does the following:
- print the final digit of the input number (
num
% 10 gives the last digit) - make a recursive call
mystery(x / 10)
, assumingx / 10
is not zero - then on the way out from the recursion, print the final digit of the input again
Putting this together, with an input of 1234
, it means we would print those digits in reverse, then print them again in order.
If this answer still leaves you with doubts, I suggest running your code, beginning with a two digit input like 12
, until it is clear what is happening.
Your mystery()
method does the following:
- print the final digit of the input number (
num
% 10 gives the last digit) - make a recursive call
mystery(x / 10)
, assumingx / 10
is not zero - then on the way out from the recursion, print the final digit of the input again
Putting this together, with an input of 1234
, it means we would print those digits in reverse, then print them again in order.
If this answer still leaves you with doubts, I suggest running your code, beginning with a two digit input like 12
, until it is clear what is happening.
edited Nov 10 at 14:54
answered Nov 10 at 14:16
Tim Biegeleisen
212k1384132
212k1384132
But for the last print statement, the value of x goes from 1 (I know it reaches the last line because 1 / 10 == 0) to 12 to 123 to 1234. How and why does it execute the last print statement 3 and build up the value of x again?
– Sahil Bagga
Nov 10 at 14:25
It prints4
,3
,2
,1
, and when1
gets printed, the input is also1
, which is zero when divided by 10. So, no subsequent calls tomystery
are made, and instead, everything backs out, starting with printing1
again, then2
, etc.
– Tim Biegeleisen
Nov 10 at 14:27
@TimBiegeleisen I agree with that.
– forpas
Nov 10 at 14:46
add a comment |
But for the last print statement, the value of x goes from 1 (I know it reaches the last line because 1 / 10 == 0) to 12 to 123 to 1234. How and why does it execute the last print statement 3 and build up the value of x again?
– Sahil Bagga
Nov 10 at 14:25
It prints4
,3
,2
,1
, and when1
gets printed, the input is also1
, which is zero when divided by 10. So, no subsequent calls tomystery
are made, and instead, everything backs out, starting with printing1
again, then2
, etc.
– Tim Biegeleisen
Nov 10 at 14:27
@TimBiegeleisen I agree with that.
– forpas
Nov 10 at 14:46
But for the last print statement, the value of x goes from 1 (I know it reaches the last line because 1 / 10 == 0) to 12 to 123 to 1234. How and why does it execute the last print statement 3 and build up the value of x again?
– Sahil Bagga
Nov 10 at 14:25
But for the last print statement, the value of x goes from 1 (I know it reaches the last line because 1 / 10 == 0) to 12 to 123 to 1234. How and why does it execute the last print statement 3 and build up the value of x again?
– Sahil Bagga
Nov 10 at 14:25
It prints
4
, 3
, 2
, 1
, and when 1
gets printed, the input is also 1
, which is zero when divided by 10. So, no subsequent calls to mystery
are made, and instead, everything backs out, starting with printing 1
again, then 2
, etc.– Tim Biegeleisen
Nov 10 at 14:27
It prints
4
, 3
, 2
, 1
, and when 1
gets printed, the input is also 1
, which is zero when divided by 10. So, no subsequent calls to mystery
are made, and instead, everything backs out, starting with printing 1
again, then 2
, etc.– Tim Biegeleisen
Nov 10 at 14:27
@TimBiegeleisen I agree with that.
– forpas
Nov 10 at 14:46
@TimBiegeleisen I agree with that.
– forpas
Nov 10 at 14:46
add a comment |
up vote
0
down vote
This is your recursion stack. This is perfectly fine.
mystery(x / 10); input 1234 prints 4
-> mystery(x / 10); input 123 prints 3
-> mystery(x / 10); input 12 prints 2
-> mystery(x / 10); input 1 prints 1
Just make sure your remove the second sysout that you that you in your code. That is the reason it is printing the same numbers in again.
add a comment |
up vote
0
down vote
This is your recursion stack. This is perfectly fine.
mystery(x / 10); input 1234 prints 4
-> mystery(x / 10); input 123 prints 3
-> mystery(x / 10); input 12 prints 2
-> mystery(x / 10); input 1 prints 1
Just make sure your remove the second sysout that you that you in your code. That is the reason it is printing the same numbers in again.
add a comment |
up vote
0
down vote
up vote
0
down vote
This is your recursion stack. This is perfectly fine.
mystery(x / 10); input 1234 prints 4
-> mystery(x / 10); input 123 prints 3
-> mystery(x / 10); input 12 prints 2
-> mystery(x / 10); input 1 prints 1
Just make sure your remove the second sysout that you that you in your code. That is the reason it is printing the same numbers in again.
This is your recursion stack. This is perfectly fine.
mystery(x / 10); input 1234 prints 4
-> mystery(x / 10); input 123 prints 3
-> mystery(x / 10); input 12 prints 2
-> mystery(x / 10); input 1 prints 1
Just make sure your remove the second sysout that you that you in your code. That is the reason it is printing the same numbers in again.
answered Nov 10 at 14:36
janardhan sharma
2537
2537
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239788%2fjava-method-executes-last-print-statement-three-times-even-though-the-method-is%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Remove line no. 10
System.out.print(x % 10);
& it would be a fine program to reverse the digits of a given number.– Abhinav
Nov 10 at 14:19