Transcript Document
Loop Exercise 1
Suppose that you want to take out a loan for $10,000,
with 18% APR, and you're willing to make payments
of $1,200/month. How long will it take you to pay off
the loan and how much interest will you pay? An
amortization table will show you how much you owe
each month, the number of months to pay off the
loan, and the total interest.
Amortization Table
Mth Payment
Amount
0
10000.00
1 1200.00 8950.00
2 1200.00 7884.25
3 1200.00 6802.51
4 1200.00 5704.55
5 1200.00 4590.12
6 1200.00 3458.97
7 1200.00 2310.86
8 1200.00 1145.52
9 1162.70
0.00
Total Interest = 762.70
Amortization Calculation
The interest owed at the end of a month is:
interest = amount * APR/1200
The amount owed at the end of a month is:
amount += interest
The payment is the either the fixed payment amount
($1,200 in this case) or the total amount, if the total
amount is less than the fixed payment.
The new amount is the amount - payment.
Program
Write a program that will read in the amount of the
loan, the Annual Percentage Rate (APR), and the
amount of the fixed payment, and then will calculate
and print out the amortization table and then the total
amount of interest paid on the loan.
The program should prompt the user for the inputs
and print out the table formatted properly with
labeled columns.
Sample Output
Enter amount of loan> 10000
Enter APR> 18
Enter monthly payment> 1200
Mth Payment
Amount
0
10000.00
1 1200.00 8950.00
2 1200.00 7884.25
3 1200.00 6802.51
4 1200.00 5704.55
5 1200.00 4590.12
...
9 1162.70
0.00
Total Interest = 762.70
Loop Exercise 2
A prime number is an integer greater 1 than is
divisible only by 1 and itself. For example, 2 is a
prime, 3 is a prime, but 4 is not a prime (4 is divisible
by 2). Write a program to print out all the prime
numbers between 1 and 100, printing ten numbers per
line.
Sample Output
Primes between 1 and 100
2
31
73
3
37
79
5
41
83
7
43
89
11
47
97
13
53
17
59
19
61
23
67
29
71
Prime Calculation
To check if one number divides another, just check if
the remainder after division is 0. For example if
(i % j == 0)
then j is a factor of i (and if j is neither i nor 1, i
is not prime).
Hint: It would be helpful to use a bool variable,
isPrime, to record whether or not the number is
prime.
Program Design
Consider how to develop this program top-down, that
is, starting with the problem statement:
Clearly, we will need to loop over all numbers from
1 to 100. What kind of loop is this?
Then, for each number, we need to decide whether
or not it is prime, and print it out if it is. This requires
a second, nested, loop.