General Condition Loop A general condition loop just

  • Slides: 12
Download presentation
General Condition Loop A general condition loop just loops while some condition remains true.

General Condition Loop 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.

Example int a = 19; while (a > 0) { System. out. println(a %

Example int a = 19; while (a > 0) { System. out. println(a % 2); a = a / 2; }

Loop Exercise 1 Suppose that you want to take out a loan for $10,

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

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 =

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

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

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

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

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

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 boolean variable, is. Prime, to record whether or not the number is prime.

Do-While Loop The third Java statement for implementing loops in the Do-While statement, which

Do-While Loop 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.

Example One common usage of the Do-While statement is to read in input until

Example 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. next. Int(); } while(n < 1 || n > 10);