please write what is difference between these programs
In the first case, your do-loop terminates prematurely, and you have code following it that fixes it. In the second case, your do-loop terminates prematurely, and you don't have code following it that fixes it.
IMO, neither case is optimal. Maybe it would be better to fix the do-loop.
Another suggestion, instead of generating the output string backwards -- requiring the for-loop to reverse it back -- maybe it would be better to generate it in a forward direction. Meaning, instead of this...