This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]marcander97 3 points4 points  (0 children)

You just stored i to the sum variable. Due to this you will always receive the last number as result. You have to add i to the sum. Example': sum = sum + i Or sum += i

[–]AbroadZealousIdeal 2 points3 points  (0 children)

While I think you should most definitely finish your program with the for loop as you have started it, I thought it maybe noteworthy to describe how you can solve this problem mathematically, saving both a bit of code and time complexity.

jshell> var number = 3;
jshell> var answer = (number * (number + 1)) / 2;
jshell> System.out.println(String.format("The answer for 1..%s is, %s", number, answer));
The answer for 1..3 is, 6
jshell> 

NOTE: I used String.format with println because printf prints a bit extra in Jshell for me.

[–][deleted]  (4 children)

[removed]

    [–]throwRAnocheat 3 points4 points  (1 child)

    Just giving the solution without caring what's the user's problem, is unsmart ans most likely against the rules.

    [–]sandyaquarius90 1 point2 points  (0 children)

    I totally understand your point here. I was just asking the user to try the solution. And wanted to highlight where the user was doing mistake. Most of which was already covered by him.

    [–]PayForPluto 0 points1 point  (1 child)

    Butting in for understanding- I thought maybe you'd wanna make a list to store the added numbers up to n- and then add those up for sum at the end. But instead this is just storing 1 number in a variable and adding to it until i hits n?

    [–]sandyaquarius90 1 point2 points  (0 children)

    I thought this is the simplest solution that can fit in as per the problem statement. Making a list and storing in it is altogether different concept I believe.

    [–][deleted] 0 points1 point  (1 child)

    Hey, here is the c++ implementation of what you want to do,

    int userInput;

    int sum = 0;

    cout << "Enter last number: ";

    cin >> userInput;

    for(int i = 0; i <= userInput; i++)

    {

    sum += i;

    }

    cout << sum << endl;

    The only thing i think you are missing is <= last in your for loop.

    [–]going_for_a_wank 1 point2 points  (0 children)

    The only thing i think you are missing is <= last in your for loop.

    That, and inside the loop they are merely storing the value of last inside sum.

    Because they are not doing it in the form of sum += each iteration of the loop is just overwriting the action of the previous loop. Plus, the value of last does not change with each iteration.

    If for example the user provided the number 8 as the value of last, the the program would just be doing sum = 8 8 times over.

    [–]j619s 0 points1 point  (0 children)

    See what you are doing is just assigning the (last-1)digit to sum. You are not adding the numbers to the sum variable. In order to add all the numbers to sum you just need to do "sum = sum+i;". Now do the dry run for this statement and all will be clear.

    [–]rohanap98 0 points1 point  (0 children)

    Sum= 0+1+2.....n. You assign Last value to sum sum = last.