Writing the Code

Now it’s your turn. Using what you’ve learned, can you write the code for this program? Give it a try. Just keep these constraints in mind as you do so:

Constraints

  • Enter the tip as a percentage. For example, a 15% tip would be entered as 15, not 0.15. Your program should handle the division.

  • Round fractions of a cent up to the next cent.

If you can’t figure out how to enforce these constraints, write the program without them and come back to it later. The point of these exercises is to practice and improve.

And if this program is too challenging for you right now, jump ahead and do some of the easier programs in this book first, and then come back to this one.

Get Exercises for Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.