Look at your requirements. Set the code up to output the first required line. Put it in a loop. Inside the loop put code that will modify the each line to the next. Construct the loop so that it exits at the appropriate time.

Review what you have been taught concerning for and while constructs. If you have problems getting your attempt to work, then post the results of your genuine efforts for more help.