You are viewing a single comment's thread. Return to all comments →
i think this would fail for the case of 1, n+1 will be 2 where the correct answer is just 1
To account for a special case like 1, you one can leverage the ternary operator to assign value to sum like so:
int sum = (n==1) ? 1 : n+1;
This will mitigate logic errors in code later on.