Header

divider
Links
• Home
• Contact Me
• 
Aeries Portal
• Royal High School
• Simi Valley USD

divider

AP Statistics

• Course Calendar
• Chapter Notes
• Practice Tests
• Statistics Links

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

divider

CP Statistics
• Course Calendar
• Chapter Notes
• Practice Tests
• Statistics Links


divider

Algebra
• Table of Contents
Geometry
• Table of Contents

Math Power
• Table of Contents

AP Computer Science Unit 3 Programs
Repetition Statements

Working with FOR Loops
Jump to:

1. Write a program that uses a for loop to display a table of squares and cubes for the integers from 1 to 10, as shown below. Make it look pretty! There is no input in this program.

X
X^2
X^3
1
1
1
2
4
8
3
9
27
4
16
64
5
25
125
6
36
216
7
49
343
8
64
512
9
81
729
10
100
1000

2. Suppose the height (h) of a rocket at any given time (t, in seconds) can be modeled by the equation

formula

Write a program that will display the height of the rocket in 5-second intervals from 0 to 60 seconds. Output should appear in table form as shown. There is no input.

Time
Height
0
10
5
35
10
860
etc
etc
60
53110


3. Write a program that asks the user for an integer, then displays a backwards count from that integer down to 1, finishing with the sum of all of those integers. See below to better understand what is being asked.

Sample Run of the Program (input and output values in bold):

Enter an integer: 6

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

4. Modify the Easter Sunday program (AP201) 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 2005 and 2009, the table will list all Easter dates for the years 2005 to 2009.

Sample Run of the Program (input and output values in bold):

Enter the beginning year: 2005
Enter the ending year: 2009

2005 - March 27
2006 - April 16
2007 - April 8
2008 - March 23
2009 - April 12

5. Write a program that uses a for loop to ask for and receive 5 positive integer values. Determine and display the highest and lowest integers input as well as the average value of the 5 integers. See the example below. This program requires no more than 6 variables (5 ints and 1 double).

Sample Run of the Program (input and output values in bold):

Enter integer #1: 5
Enter integer #2: 17
Enter integer #3: 34
Enter integer #4: 19
Enter integer #5: 29

Lowest Number: 5
Highest Number: 34
Average Value: 20.8

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”.

Sample Run of the Program (TRY BOTH - input and output values in bold):

Enter an integer: 60

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

Enter an integer: 467

Factors are: 1, 467 - this is a 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 two 1's as the starting values of the sequence).

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 the first 7 Fibonacci numbers: 1 1 2 3 5 8 13. 

Be sure to separate each number by a space. Use the above example to test your program. Below is another test. Note: this is a tricky program. The key is to use 3 integer variables to handle the sum operations. KEY POINT - what value should you initialize them to before the loop? Happy puzzle solving!

Sample Run of the Program (input and output values in bold):

How many Fibonacci numbers do you want? 11

Here they are: 1 1 2 3 5 8 13 21 34 55 89

8. Write a program to determine the number of even and odd numbers as entered by the user. The program should ask how many integers to be entered, and then allow the user to enter the integers, using a loop. As the integers are entered, keep track of how many are even and how many are odd. At the end, display the results as shown in the example below. You may assume that the user will only enter positive integers.

Sample Run of the Program (input and output values in bold):

How many numbers will be entered: 4

Enter number 1: 10
Enter number 2: 11
Enter number 3: 18
Enter number 4: 20

Evens: 3
Odds: 1

9. Write a program that simulates flipping a coin a given number of times. Count the number of heads and tails that come up and output the results as counts. See the example below:

Sample Run of the Program (input and output values in bold):

How many flips would you like: 25

Heads: 15
Tails: 10

10. Write a program to simulate rolling a 6-sided die 1000 times, counting how many 1's, 2's, 3's etc... that the "die" rolls. Display a table of results as shown. There is no input in this program.

Sample Run of the Program

1’s = 160
2’s = 182
3’s = 165
4’s = 177
5’s = 155
6’s = 161

 

Home  •  About Me  •  Aeries Portal  •  Contact Me
© DanShuster.com