Mysterious output coming

If i am using line no 19 then the ans outside for loop shows correct output.
if line 19 is commented it shows random number .

just initialize variable ans with 0