Yes. If only n made it to the end of the loop with that value, but it does not.
n becomes 1 in the 1st if code. This makes it enter the 2nd if code and it emerges with n = 4.
The while(n > 1) part never sees n = 1.
EDIT: Try else if instead for the 2nd code. It should work then.
Or just else. This because if n%2 != 0 then n%2 = 1.