Header

divider
Links
• Home
• Webgrades
• Contact Me

• Royal High School
• Simi Valley USD


divider

AP Statistics

• Homework Log
• Test Schedule
• Chapter Reviews
• Practice Tests
• Research Project
• Statistics Links

divider
AP Comp-Sci
 Course Content
• Programs
• Practice Tests
• Comp-Sci Links

divider

Geometry
• Homework Log
• Test Schedule
• Chapter Reviews
• Practice Tests
• Geometry Links


AP Computer Science Unit 3 Programs
Repetition Statements


(1)    Write a program which uses loops to output a table of x1, x2 and x3 for the integers from 1 to 10. Output should match that below.

     X^1        X^2         X^3
  ---        ---         ---
    1          1           1
    2          4           8
    3          9          27
etc...

 

(2)    Write a program that accepts a positive integer x and calculates and displays the value of  X + (X-1) + (X-2) + ... + 2 + 1. Give an error message if a non-positive integer is input. For example, if the input is 6, the result will be 

6 + 5 + 4 + 3 + 2 + 1 = 21 

Output must match the format above (meaning a ”+” should not appear after 1).

(3)    Modify your original Easter Sunday program (2-1) so that it will output a table of dates for several consecutive years, starting and ending with years input by the user. For example, if the user enters 1995 and 1999, the table will list all Easter dates for the years 1995 to 1999. Another modification - before our program would only work for the years 1982 - 2048. We can extend the algorithm to work for the years 1900 - 2099. There are four years, 1954, 1981, 2049 and 2076 for which the original algorithm gives a date that is 7 days later than it should be. The change does not affect the month, however.

Results for those years:  1954-April 18, 1981-April 19, 2049-April 18, 2076-April 19

Test Data for loop (display vertically, not as shown here):

1995-April 16,    1996-April 7,    1997-March 30,    1998-April 12,    1999-April 4

(4)    Write a program that accepts 5 positive integers (using a loop) and determines and prints the highest one entered. This program does not require you to store the 5 numbers entered. You may only use 3 variables in this program.

         Test Data:  5, 17, 34, 19, 27              Output:  Highest number entered was 34

 

(5)    Write a program to simulate rolling a 6-sided die 1000 times and keep track of the frequency of each number possible (1-6). Display a table of results as shown.

1’s = ###

2’s = ###

3’s = ###

4’s = ###

5’s = ###

6’s = ###

(6)    Write a program that finds and displays all factors of a given integer. If the integer has no factors other than 1 and itself, display “PRIME NUMBER”.

   Test Data and Output Format:

60: Factors are 1,2,3,4,5,6,10,12,15,20,30,60.

467: Factors are 1,467. PRIME NUMBER

(7)    The following is called the “Fibonacci” Sequence:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...

The sequence has the characteristic that each term is the sum of the two previous terms in the sequence (this assumes 1 as the starting term).

Write a program that will print the first n Fibonacci numbers based on the input integer n. For example, if  the input is 7, the output will be  1 1 2 3 5 8 13.  Be sure to separate each number by a space.

(8)    Write a program to simulate the activity in a savings account. Given an initial deposit and an interest rate, determine the yearly interest and account balance in the savings account for the next 10 years. Assume that interest is compounded annually and that no other money is deposited or withdrawn. Display the yearly results in a table as shown below (assume 10000.00 @ 5.5% interest):

        Year     Beg. Balance       Interest        End Balance

      1         10000.00         550.00           10550.00

      2         10550.00         580.25           11130.25

    etc              etc            etc                etc

     10         16190.94         890.50           17081.44

 

(9)    Write a modified version of #8 that takes an initial deposit, an interest rate and a goal value, then determines the number of years needed for a savings account to meet or exceed the goal value. Assume that interest is compounded annually and that no other money is deposited or withdrawn.

         Test Data:  $1000 invested at 5.25%, with a goal of $10,000 takes 46 years.

 

(10)  Write a program to find the digital root of a given integer (maximum 4 digits). The digital root of a number can be found by adding its digits together until the result is a single digit. For example, to find the digital root of 347:  3+4+7=14 then 1+4=5. So the digital root of 347 is 5.  You MUST use a WHILE loop.

         Test Data:        a)  343 = 1                 b)  3252 = 3               c)  987 = 6

         Output Format: The digital root of #### is #.

 

(11)  Write a program to find the Greatest Common Divisor (GCD) for two positive integers. For the GCD use the Euclidean algorithm, as follows:

         1)  Let x and y be positive integers

         2)  Let r = remainder of x divided by y

         3)  If r = 0, then y is the GCD. Otherwise, let x=y, y=r and go back to step 2

         Test Data:  a)  64,44: GCD=4        b)  32, 128: GCD=32         c)  315,113: GCD=1

         Output Format:  For ## and ##, the GCD is ##.

 

(12)  Write a program that uses nested loops to print out the following designs.

         a) 1                 b) 987654321          c) 1

      12                  98765432              21       

      123                 9876543               321

      1234                987654                4321

      12345               98765                 54321

      123456              9876                  654321

      1234567             987                   7654321

      12345678            98                    87654321

      123456789           9                     987654321

 

(13)  When a player lands in jail in the game of Monopoly, he/she has the opportunity to “get out” by rolling doubles with the dice. The player must pay $50 if they are unsuccessful after 3 tries. Assume that they could roll as long as they wanted. How long, on average, would one expect to take to roll doubles? To answer this question, simulate this situation 10,000 times and find the average number of rolls needed per attempt to achieve doubles. For example, if we ran three trials and they took 9, 5 and 6 rolls, the average would be 6.7 (20/3) rolls. To see this another way, look at it in table form:

TRIAL
Rolls to achieve doubles
1
9
2
5
3
6
Total
20

       It took 20 total rolls for 3 trials. So the average number of rolls was 20/3 = 6.7

In addition to the average, also find the highest number of rolls taken to get doubles.

      Since this is random, results will vary...

 

(14)  Write a program that uses nested loops to print the following multiplication table. All output MUST be generated using loops and must look EXACTLY as shown.

X

1

2

3

4

5

6

7

8

9

1

1

2

3

4

5

6

7

8

9

2

2

4

6

8

10

12

14

16

18

3

3

6

9

12

15

18

21

24

27

4

4

8

12

16

20

24

28

32

36

5

5

10

15

20

25

30

35

40

45

6

6

12

18

24

30

36

42

48

54

7

7

14

21

28

35

42

49

56

63

8

8

16

24

32

40

48

56

64

72

9

9

18

27

36

45

54

63

72

81

 

(15)  Write a program to determine all possible ways to make change for a dollar using only quarters, dimes, nickels and pennies. The output must show the results, one line at a time, without showing zeros. Count the number of ways that the program finds to make change and print this result at the end of the program.

         For example:  2 quarters, 5 dimes not 2 quarters, 5 dimes, 0 nickels, 0 pennies.

         The total number of combinations is 242 (I think!)

         Hint: This program requires 4 FOR LOOPS (one for each type of coin).

(16)  A perfect number is any integer that is equal to the sum of its factors, not including itself. For example, 6 is a perfect number because 1+2+3=6. Write a program to find the first four perfect numbers (6 is the first). This is a tough one!

 

 


 






Home  •  About Me  •  Webgrades  •  Contact Me
© DanShuster.com