Why this program is not showing correct output

Hi
your workflow had mistakes. I just updated your code & add some comments too.
I suggest to dry run your code with yourself again.

If you have any other doubts, please feel free to ask.