The error in Karl’s code is his statement “if (n % 2 == 0)”, which really means that the action is only performed if the remainder of “n ÷ 2 = 0”. Therefore, the program will only correctly output n if n is an even, non−zero number. If n is a negative number, the output will always be what Karl wants it to be. The solution to this problem is to simply eliminate the statement in question.
−Phoenix
Concise and correct. 15 XPs awarded for completion.
ReplyDelete