Você está na página 1de 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 1 2 3 4 5 6 sqrt(x) 1.0000 1.4142 1.7321 2.0000 2.2361 2.4495 x^2 1 4 9 16 25 36 x^3 1 8 27 64 125 216 x^4 1 16 81 256 625 1296 x^5 1 32 243 1024 3125 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 4 4 4 4 4 4 ^ ^ ^ ^ ^ ^ ^ 0 1 2 3 4 5 6 is is is is is is is 1 4 16 64 256 1024 4096

4) Prompt the user to enter a positive integer, n, from the keyboard. Using a for loop print all the positive, even nondivisors less than n. Sample input/output: Enter a positive integer: 30 4 8 12 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 0 1 2 3 4 . . . 17 18 Population 100.0 115.9 134.0 154.4 177.3 . . . 649.8 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

Enter the number of years the principal is invested: 8 Year 0 1 2 3 4 5 6 7 8 Balance 1250.75 1335.05 1425.02 1521.06 1623.58 1733.00 1849.80 1974.47 2107.54

Você também pode gostar