Computer Science – More While Loop Labs 1. Write a program that asks the user for a starting value and an ending value and then writes all the integers (inclusive) between those two values. (StartToEnd.java) Enter Start: 5 Enter End: 9

5 6 7 8 9

2. Write a program that writes a wedge of stars. The user enters the initial number of stars, and the program writes out lines of stars. Each line has one less star than the previous line: (Stars.java) Initial number of stars: 7

******* ****** ***** **** *** ** * 3. Write a program that adds up integers that the user enters. First the programs asks how many numbers will be added up. Then the program prompts the user for each number. Finally it prints the sum. (AddIntegers.java) How many integers will be added: 5 Enter an integer: 3 Enter an integer: 4 Enter an integer: -4 Enter an integer: -3 Enter an integer: 7

The sum is 7 Be careful not to add the number of integers (in the example, 5) into the sum. 4. Write a program that adds up the squares and adds up the cubes of integers from 1 to N, where N is entered by the user: (SumOfSquaresAndCubes.java) Upper Limit: 5 The sum of Squares is 55 The sum of Cubes is 225 Do this by using just one loop that generates the integers. Of course, if you really needed to calculate these sums you would use the appropriate formulas: 12 + 22 + 32 + ... + n2 = n(n+1)(2n+1)/6 13 + 23 + 33 + ... + n3 = n2(n+1)2/4 Add these formulas to your program and print out their results as well as that of the explicit summations.

5. (Powers.java) Write a program that computes XN where X is a floating point number and N is a positive integer. The program informs the user that N must be positive if the user enters a negative value. Use a loop instead of Math.pow(x, n). Of course, XN = X * X * X * ... * X ------N times The user dialog will look something like this: Enter X 1.3 Enter N 5

1.3 raised to the power 5 is: 3.71293

------

Enter X 5.6 Enter N -3

N must be a positive integer.

6. A mail order company charges $3.00 for handling, free shipping for orders 10 pounds or less, plus $0.25 for each pound over 10. Write a program that repeatedly asks the user for the weight of an order, then writes out the shipping charge. The program stops when a weight of zero or less is entered. (ShippingCosts.java) Weight of Order: 5 Shipping Cost: $3.00

Weight of Order 20 Shipping Cost: $5.50 Weight of Order 0

Goodbye!

7. Say that you owe the credit card company $1000.00. The company charges you 1.5% per month on the unpaid balance. You have decided to stop using the card and to pay off the debt by making a monthly payment of N dollars a month. Write a program that asks for the monthly payment, then writes out the balance and total payments so far for every succeeding month until the balance is zero or less. (Debt.java) Enter the monthly payment: 100 Month: 1 balance: 915.0 total payments: 100.0 Month: 2 balance: 828.725 total payments: 200.0 Month: 3 balance: 741.155875 total payments: 300.0 Month: 4 balance: 652.273213125 total payments: 400.0 Month: 5 balance: 562.057311321875 total payments: 500.0 Month: 6 balance: 470.4881709917031 total payments: 600.0 Month: 7 balance: 377.54549355657866 total payments: 700.0 Month: 8 balance: 283.20867595992735 total payments: 800.0 Month: 9 balance: 187.4568060993263 total payments: 900.0 Month: 10 balance: 90.26865819081618 total payments: 1000.0 Month: 11 balance: -8.377311936321576 total payments: 1100.0 For each month, calculate the interest due on the unpaid balance. Then calculate the new balance by adding the interest and subtracting the payment.