Lab Exercises - Loops

Lab Exercises - Loops

Lab Exercises - Loops

Exercise 0 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.

Enter Start:

5

Enter End:

9

5

6

7

8

9

Exercise 1 Write a program that asks the user to enter a word. The program will then repeat word for as many times as it has characters:

Enter a word:Hello

Hello

Hello

Hello

Hello

Hello

To do this you will need to use thelength()method that counts the number of characters in a string:

String inputString;

int times;

. . . .

times = inputString.length()

Exercise 2 Write a program that asks the user to enter two words. The program then prints out both words on one line. The words will be separated by enough dots so that the total line length is 30:

Enter first word:

turtle

Enter second word

153

turtle...... 153

This could be used as part of an index for a book. To print out the dots, useSystem.out.print(".")inside a loop body.

Exercise 3 --- Adding up Integers Write a program that adds up integers that the user enters. First the program asks how many numbers will be added up. Then the program prompts the user for each number. Finally it prints the sum.

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.

Exercise 4 — Adding up Squares and Cubes 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:

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.

Exercise 5 — Power of a number Write a program that computesxnwherexis a floating point number andnis a positive integer. The program informs the user thatnmust be positive if the user enters a negative value. Of course you could use a Math method to do this, but instead use a loop. In other words, DO NOT USE Math.pow().

x n = 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

------(Another Run)

Enter x:

5.6

Enter n

-3

n must be a positive integer.

Exercise 6 — Credit Card Bill 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, and then writes out the balance and total payments so far for every succeeding month until the balance is zero or less.

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.

Improved Program: Have the program prompt for the beginning balance, the monthly interest, and the payment amount. Also, when the balance falls below the amount of the monthly payment, write out the final payment that will bring the balance to exactly zero.

Exercise 7 — Drug Potency A certain drug looses 4% of its effectiveness every month it is in storage. When its effectiveness is below 50% it is considered expired and must be discarded. Write a program that determines how many months the drug can remain in storage.

month: 0 effectiveness: 100.0

month: 1 effectiveness: 96.0

month: 2 effectiveness: 92.16

month: 3 effectiveness: 88.47359999999999

month: 4 effectiveness: 84.93465599999999

month: 5 effectiveness: 81.53726975999999

month: 6 effectiveness: 78.27577896959998

month: 7 effectiveness: 75.14474781081599

month: 8 effectiveness: 72.13895789838334

month: 9 effectiveness: 69.253399582448

month: 10 effectiveness: 66.48326359915008

month: 11 effectiveness: 63.82393305518407

month: 12 effectiveness: 61.27097573297671

month: 13 effectiveness: 58.82013670365764

month: 14 effectiveness: 56.46733123551133

month: 15 effectiveness: 54.20863798609088

month: 16 effectiveness: 52.04029246664724

month: 17 effectiveness: 49.95868076798135 DISCARDED

Top of Form

Bottom of Form

Exercise 8 — Wedge of Stars

Write a program that uses loops to write a wedge of stars. The user enters the initial number of stars, and the program writes out lines of stars. Each line has one fewer star than the previous line:

This must, of course be done in loops.

Initial number of stars:

7

*******

******

*****

****

***

**

*

Exercise 9 — Stars and Stripes

Now do this:

Initial number: 6

******

*****/

****//

***///

**////

*/////

//////

Exercise 10 — Wedge 2

Now do this:

Initial number: 6

*

**

***

****

*****

******

Exercise 9 — Pine Tree Write a program that writes a tree made of stars on the terminal. Yes, you must use loops! For added challenge, prompt the user for the size of the tree!

*

***

*****

*******

*********

***********

*************

***************

***

***

***