120 likes | 138 Views
A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change some value so that the condition becomes false. Otherwise, if the loop does run, it becomes an infinite loop. General Condition Loop. int a = 19; while (a > 0) {
E N D
A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change some value so that the condition becomes false. Otherwise, if the loop does run, it becomes an infinite loop. General Condition Loop
int a = 19; while (a > 0) { System.out.println(a % 2); a = a / 2; } Example
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. Loop Exercise 1
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 Table
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. Amortization Calculation
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. Program
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 Sample Output
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. Loop Exercise 2
Primes between 1 and 100 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 Sample Output
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 boolean variable, isPrime, to record whether or not the number is prime. Prime Calculation
The third Java statement for implementing loops in the Do-While statement, which looks like: do <stmt> while(<test>) The only difference between the Do-While statement and the While Statement is that the test is checked after the loop body is executed. Thus, the loop body is always done at least one time. Do-While Loop
One common usage of the Do-While statement is to read in input until a valid value is read. For example, in the guessing game, ask the user for a number be 1 and 10 (inclusive): do { System.out.print("Pick a number between 1 and 10 >"); n = sc.nextInt(); } while(n < 1 || n > 10); Example