lab while loops and for loops
TRANSCRIPT
-
8/3/2019 Lab While Loops and for Loops
1/3
Computer Programming K
Donovan
Lab 1: While Loops and For Loops
You are to write a program that will perform the tasks below. For each part, you are told which type of loop to use, either a
while loop or a for loop.
1) Prompt the user to enter a positive integer, n, from the keyboard. Using a while loop, for each integer, x, from 1 to n,
print x, square root of x, x^2, x^3, x^4, and x^5. Print the square root values rounded to four decimal places. Your output
should be in table form with headings above each column.
Sample input/output:
Enter a positive integer:
6
x sqrt(x) x^2 x^3 x^4 x^5
1 1.0000 1 1 1 1
2 1.4142 4 8 16 32
3 1.7321 9 27 81 243
4 2.0000 16 64 256 10245 2.2361 25 125 625 3125
6 2.4495 36 216 1296 7776
2) Prompt the user to enter a positive integer, n, from the keyboard. Using a while loop print all the positive, odd divisors
less than n.
Sample input/output:
Enter a positive integer:
420
1
3
5
7
15
21
35
105
3) Prompt the user to enter two positive integers from the keyboard. Using a while loop print all the powers of the first
number entered that are less than or equal to the second number entered.
Sample input/output:
Enter two positive integers:
4 5000
4 ^ 0 is 1
4 ^ 1 is 4
4 ^ 2 is 16
4 ^ 3 is 64
4 ^ 4 is 256
4 ^ 5 is 1024
4 ^ 6 is 4096
-
8/3/2019 Lab While Loops and for Loops
2/3
4) Prompt the user to enter a positive integer, n, from the keyboard. Using a for loop print all the positive, evennon-
divisors less than n.
Sample input/output:
Enter a positive integer:
30
4
812
14
16
18
20
22
24
26
28
5) 100 animals of an endangered species are released into a game preserve. The population growth of the animal pack
can be modeled by the logistic function p(t) = 1000 / (1 + 9e^(-.1656t)) where t is measured in months. Using a
for loop print the animal population for each month from 0 to 18 months. Round the population to one decimal place (yes,
I know you cant have 302.4 animals). Hint: use: double e = Math.E;
Sample input/output:
Month Population
0 100.0
1 115.9
2 134.0
3 154.4
4 177.3
. .
. .
. .
17 649.8
18 686.5
6) Prompt the user to enter the principal amount (the amount invested, a double), the interest rate as a decimal (a
double), the number of times interest is compounded per year (an integer), and the number of years the money will be
invested (an integer). Using a for loop print the balance of the investment for each year the money is invested, rounded
to two decimal places. Use the compound interest formula: b = p( 1 + r/n ) ^ ( nt ) where b is the
balance, p is the principal amount, r is the interest rate as a decimal, n is the number of times
interest is compounded per year, t is the number of years the money has been invested.
Sample input/output:
Enter the principal:
1250.75
Enter the interest rate as a decimal:
.0654
Enter the number of times interest is compounded per year:
12
-
8/3/2019 Lab While Loops and for Loops
3/3
Enter the number of years the principal is invested:
8
Year Balance
0 1250.75
1 1335.05
2 1425.02
3 1521.06
4 1623.585 1733.00
6 1849.80
7 1974.47
8 2107.54