This group of playbacks discusses repeatedly executing the same code over and over again in a loop. I show how to create count controlled and event controlled loops with the `while`

keyword, nested loops, a `for`

loop, and how to exit a loop with `break`

and `continue`

.

### Call to Action

Now that you have reviewed the guided code walk-throughs I ask that you write a few programs:

#### Problem 1

Problem 1 asks you to write a program that will calculate the sum of the squares from 1 up to and including that number. For example if the user entered in the value 5 then the sum of the squares for the numbers one through five would be (1 + 4 + 9 + 16 + 25) = 55.

Your program should repeatedly calculate this value until the user enters in a value of -99. This is the sentinel value that the program uses to determine when to quit.

```
Enter in an integer number(ex. 10), -99 to quit: 5
The sum of the squares up to 5 is 55
Enter in an integer number(ex. 10), -99 to quit: 4
The sum of the squares up to 4 is 30
Enter in an integer number(ex. 10), -99 to quit: -99
Have a nice day!
```

#### Problem 2

Problem 2 asks you to write a program that will determine whether a number is prime or not. A prime number is any number that is evenly divisible only by the number one and itself.

7 is prime because the only numbers that divide into it without a remainder are 1 and 7.

12 is not prime because the numbers that divide into it evenly are 1, 2, 3, 4, 6, and 12.

Your program will prompt for a number and then display whether the number is prime or not. The number entered must be a positive number. Repeatedly prompt for a number until a positive number is entered.

#### Problem 3

Problem 3 asks you to calculate a mortgage schedule for someone thinking of buying a new house. The inputs to determine a monthly schedule are the principal loan amount and the annual interest rate. Assume this will be a conventional 30 year loan.

Your program should prompt for these inputs and find a monthly payment using this calculation:

```
monthly interest rate
monthly payment = ------------------------------------------------- * principal
1 - (1 + monthly interest rate)^-number of months
```

Notice that you will have to calculate the monthly interest rate (the annual interest rate divided by 12.0) and number of months (360 for a 30 year loan). The ^ in this formula means raise one number to a power. There is a function called `pow()`

which raises one number to another and returns the result. For example, if I wanted to raise 2 to the -3rd power I would do this:

```
float result = pow(2.0, -3.0);
```

After you have calculated the monthly payment create a summary of the loan characteristics. Display the loan amount, the interest rate, the monthly payment, the total amount paid for the loan, the total amount of interest paid, and the ratio of amount paid over the principal.

After you have printed the summary you can begin to make the schedule. Prompt the user for the ending month to display in the schedule. The schedule should display the month number, the monthly payment, the amount paid in principal in that month, the amount paid in interest in that month, and the amount remaining in the principal (the amount paid in principle each month is deducted from the remaining principle). A month's interest amount is equal to monthly interest rate times the remaining principal. The monthly principal is the difference between the monthly payment and the monthly interest paid. Remember to update the remaining principal after every month.

After each year of the schedule has been printed display a message with the year number.

### Comments and Feedback

You can find all of these code playbacks in my free 'book', An Animated Introduction to Programming in C++. I am always looking for feedback so please feel free to comment here or to send me a message. You can follow me on twitter @markm208.

## Top comments (0)