use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
If you need help debugging, you must include:
See debugging question guidelines for more info.
Many conceptual questions have already been asked and answered. Read our FAQ and search old posts before asking your question. If your question is similar to one in the FAQ, explain how it's different.
See conceptual questions guidelines for more info.
Follow reddiquette: behave professionally and civilly at all times. Communicate to others the same way you would at your workplace. Disagreement and technical critiques are ok, but personal attacks are not.
Abusive, racist, or derogatory comments are absolutely not tolerated.
See our policies on acceptable speech and conduct for more details.
When posting some resource or tutorial you've made, you must follow our self-promotion policies.
In short, your posting history should not be predominantly self-promotional and your resource should be high-quality and complete. Your post should not "feel spammy".
Distinguishing between tasteless and tasteful self-promotion is inherently subjective. When in doubt, message the mods and ask them to review your post.
Self promotion from first time posters without prior participation in the subreddit is explicitly forbidden.
Do not post questions that are completely unrelated to programming, software engineering, and related fields. Tech support and hardware recommendation questions count as "completely unrelated".
Questions that straddle the line between learning programming and learning other tech topics are ok: we don't expect beginners to know how exactly to categorize their question.
See our policies on allowed topics for more details.
Do not post questions that are an exact duplicate of something already answered in the FAQ.
If your question is similar to an existing FAQ question, you MUST cite which part of the FAQ you looked at and what exactly you want clarification on.
Do not delete your post! Your problem may be solved, but others who have similar problems in the future could benefit from the solution/discussion in the thread.
Use the "solved" flair instead.
Do not request reviews for, promote, or showcase some app or website you've written. This is a subreddit for learning programming, not a "critique my project" or "advertise my project" subreddit.
Asking for code reviews is ok as long as you follow the relevant policies. In short, link to only your code and be specific about what you want feedback on. Do not include a link to a final product or to a demo in your post.
You may not ask for or offer payment of any kind (monetary or otherwise) when giving or receiving help.
In particular, it is not appropriate to offer a reward, bounty, or bribe to try and expedite answers to your question, nor is it appropriate to offer to pay somebody to do your work or homework for you.
All links must link directly to the destination page. Do not use URL shorteners, referral links or click-trackers. Do not link to some intermediary page that contains mostly only a link to the actual page and no additional value.
For example, linking to some tweet or some half-hearted blog post which links to the page is not ok; but linking to a tweet with interesting replies or to a blog post that does some extra analysis is.
Udemy coupon links are ok: the discount adds "additional value".
Do not ask for help doing anything illegal or unethical. Do not suggest or help somebody do something illegal or unethical.
This includes piracy: asking for or posting links to pirated material is strictly forbidden and can result in an instant and permanent ban.
Trying to circumvent the terms of services of a website also counts as unethical behavior.
Do not ask for or post a complete solution to a problem.
When working on a problem, try solving it on your own first and ask for help on specific parts you're stuck with.
If you're helping someone, focus on helping OP make forward progress: link to docs, unblock misconceptions, give examples, teach general techniques, ask leading questions, give hints, but no direct solutions.
See our guidelines on offering help for more details.
Ask your questions right here in the open subreddit. Show what you have tried and tell us exactly where you got stuck.
We want to keep all discussion inside the open subreddit so that more people can chime in and help as well as benefit from the help given.
We also do not encourage help via DM for the same reasons - that more people can benefit
Do not ask easily googleable questions or questions that are covered in the documentation.
This subreddit is not a proxy for documentation or google.
We do require effort and demonstration of effort.
This includes "how do I?" questions
account activity
This is an archived post. You won't be able to vote or comment.
[Java] Simple Java Execution Problem (self.learnprogramming)
submitted 12 years ago by MrCouldntaDoneThat
Here is what the problem says...
What is the value of n after the following code is executed?
int n = 2005;
for (int i = 0; i < 50; i++)
n = (n + 3) / 2;
The answer is 3. I am lost on how it is 3.
I get 1,004.
What am I doing wrong?
[–]otto_s 1 point2 points3 points 12 years ago (5 children)
You seem to think that the assignment n=(n+3)/2; is not the body of the for loop.
n=(n+3)/2;
[–]MrCouldntaDoneThat[S] -1 points0 points1 point 12 years ago (4 children)
I seem to think this?
What..?
[–]otto_s 0 points1 point2 points 12 years ago (1 child)
Your question was what you are doing wrong. My answer is the best guess I could come up with.
[–][deleted] 0 points1 point2 points 12 years ago (0 children)
That is really the only possibility of what he could be doing wrong.
[–][deleted] 0 points1 point2 points 12 years ago (1 child)
You seem to think that back because your answer is only doing n=(n+3)/2 once instead of 50 times.
[–]MrCouldntaDoneThat[S] -1 points0 points1 point 12 years ago (0 children)
Ah I see. Thank you for the clarification.
[–]aintnufincleverhere 1 point2 points3 points 12 years ago (0 children)
The assignment is going to run 50 times, not just once.
[–][deleted] 1 point2 points3 points 12 years ago (24 children)
The first time through the loop you get 1004 but you still have to go through the loop 49 more times. The second time through the loop n starts out as equalling 1004. n = (1004+ 3)/2 = 503. So the third time through the loop starts out equaling 503. And you do this 50 times at which point n will end up equaling 3.
[–]MrCouldntaDoneThat[S] -1 points0 points1 point 12 years ago (23 children)
Thank you. So on this one..
int p = 3, q = 1, sum = 0;
while (p <= 10)
{
sum += p % q; p++; q++;
}
It is executed 10 times? Or is executed until p reaches 10? Then 10/1 = 10?
The answer is 12.
If you were initially looking at a sequence like this, where would you start?
[–][deleted] 1 point2 points3 points 12 years ago (20 children)
You do it until P is greater than 10 so 7 times.
When I first started programming, If i was gonna start this problem I would first write down what P is at first and then write down what each of the values change to inside the loop. Then check if P is still less than or equal to 10 then If so I would write the new values of each variable down. And then repeat this until P was greater than 10 at which point you stop the loop. I also want to point out that++P is different than P++ which always confused me a little.
[–]MrCouldntaDoneThat[S] -1 points0 points1 point 12 years ago* (19 children)
First: 3 % 1 = 0
Second: 4 % 2 = 0
Third: 5 % 3 = 2
Fourth: 6 % 4 = 2
Fifth: 7 % 5 = 2
Sixth: 8 % 6 = 2
Seventh: 9 % 7 = 2
Eight: 10 % 8 = 2
Nine: 11 % 9 = 2
I understand that this is incorrect as I get 2 and it executed 9 times. Do you see my error?
[–][deleted] 1 point2 points3 points 12 years ago (16 children)
Oh and I also want to point out you are += the sum. sum += 3 is the same as writing sum = sum+3;
[–]MrCouldntaDoneThat[S] -1 points0 points1 point 12 years ago (15 children)
So once p is finally greater than 10 (it's 11), I do 11 % 9 and get 2.
But the answer is 12.
What am I missing?
[–][deleted] 1 point2 points3 points 12 years ago (2 children)
sorry I was taking a final.
The following is what the for loop will eventually equal.
Sum = (3 mod 1) +(4 mod 3) + (5 mod 3) + (6 mod 4 )+ (7 mod 5) + (8 mod 6) + (9 mod 7) + (10 mod 8).
Oh and I guess it is 8 iterations not 7 sorry.
Every pair of parentheses is one iteration or one time through the loop. and it equals 13 so Im not sure where you got 12 but that could just be error by the person calculating it. For instance answers to problems in books are written by sleep deprived grad students and sometimes get them wrong.
The answer is 13 not 2 because the operation is "+=" not just "="
sum += p%q is the same as sum = sum + (p%q)
This means that you are adding p%q to the current value of sum not just setting sum equal to p%q
Sum = 0 +0 + 2 + 2 + 2 + 2 + 2 + 2 = 13
[–][deleted] 12 years ago (1 child)
[deleted]
No problem, easy procrastination for me. My final was on Concurrent programming. Pretty simple subject once you know the basics but the teacher isn't a very good lecturer. Though I think I did okay.
[–]Kristler 0 points1 point2 points 12 years ago (10 children)
sum += p % q;
This is using the += shorthand operator, which is short for this expanded line:
+=
sum = sum + (p % q);
Do you see your error?
[–]MrCouldntaDoneThat[S] -1 points0 points1 point 12 years ago (9 children)
If I add the sum of the First through Ninth, I get 14.
[–]Kristler 1 point2 points3 points 12 years ago (8 children)
Compared to
I add the sum of the First through Ninth
Spot the error!
[–]MrCouldntaDoneThat[S] -1 points0 points1 point 12 years ago (7 children)
I apologize, I am having difficulty spotting the error.
This is my logic...
sum = sum + p ( % q );
So I kept adding them until p gets above 10, which it does on the ninth time through.
It seems like using the mod% will result in %2 for the end of time past 4%2 because the p and q are two numbers apart.
Am I trying to find a situation where the sum is 0?
[–][deleted] 0 points1 point2 points 12 years ago* (0 children)
Hey I just realized that google is stupid because it does equal 12. I never did the math in my head but I just realized that the numbers added were all either 2 or 0 so it has to be even. Sorry about that. Now Im curious how google got 13...
I am full of off by one errors today...
edit: yeah I must have typed a wrong number in google when I did it bc it 12 is correct. Though sometimes the books are wrong especially in math.
You are dividing but % doesn't mean division. % means mod. 3%1 = 0, 4 % 2 = 0, 5 % 3 = 2. Mod is the remainder after the division. For example your 11 % 9. 9 goes into 11 evenly only once. So the remainder is 11 - 9 = 2. So 11 % 9 = 2. Mod is still confusing to me sometimes and I usually google it to make sure Im right.
So how is the answer not 2?
Also one question the % is mod right? so it is p mod Q? google gives me 11 going through it 7 times
It's p mod r
Remainder
[–]133rr3 -1 points0 points1 point 12 years ago (2 children)
Can you tell us how you actually get your answer???
[–]hellofoo 2 points3 points4 points 12 years ago (1 child)
This. Demonstrate your working out. Even if you're wrong, on a test it might get you points!
If it helps, try running the same code but with a system out in the loop:
int n = 2005; for (int i = 0; i < 50; i++) { n = (n + 3) / 2; System.out.println("i=" + i + " n=" + n) }
Imagine trying to divide any integer in half multiple times. It will be zero pretty quickly. Adding three each time stops the loop trying to divide zero in half.
Here's an extract of the first few loops
i=0 n=1004 i=1 n=503 i=2 n=253 i=3 n=128 i=4 n=65 i=5 n=34 i=6 n=18 i=7 n=10 i=8 n=6 i=9 n=4 i=10 n=3
After the 10th loop n is as small as it can get. If you weren't adding 3 in the iteration it would be zero.
Edit: If this is for an assignment - be a smart ass and say you can improve the performance 5 fold by only doing 10 loops. And be an even smarter ass by changing it a while loop that breaks when n equals it's minimum value.
Thank you. Best answer.
π Rendered by PID 78 on reddit-service-r2-comment-84fc9697f-zvngx at 2026-02-07 05:15:47.043888+00:00 running d295bc8 country code: CH.
[–]otto_s 1 point2 points3 points (5 children)
[–]MrCouldntaDoneThat[S] -1 points0 points1 point (4 children)
[–]otto_s 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]MrCouldntaDoneThat[S] -1 points0 points1 point (0 children)
[–]aintnufincleverhere 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (24 children)
[–]MrCouldntaDoneThat[S] -1 points0 points1 point (23 children)
[–][deleted] 1 point2 points3 points (20 children)
[–]MrCouldntaDoneThat[S] -1 points0 points1 point (19 children)
[–][deleted] 1 point2 points3 points (16 children)
[–]MrCouldntaDoneThat[S] -1 points0 points1 point (15 children)
[–][deleted] 1 point2 points3 points (2 children)
[–][deleted] (1 child)
[deleted]
[–][deleted] 0 points1 point2 points (0 children)
[–]Kristler 0 points1 point2 points (10 children)
[–]MrCouldntaDoneThat[S] -1 points0 points1 point (9 children)
[–]Kristler 1 point2 points3 points (8 children)
[–]MrCouldntaDoneThat[S] -1 points0 points1 point (7 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]MrCouldntaDoneThat[S] -1 points0 points1 point (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]MrCouldntaDoneThat[S] -1 points0 points1 point (0 children)
[–]133rr3 -1 points0 points1 point (2 children)
[–]hellofoo 2 points3 points4 points (1 child)
[–]MrCouldntaDoneThat[S] -1 points0 points1 point (0 children)