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 WHILE Loops
Jump to:

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

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

Enter an integer up to 4 digits: 343

The digital root of 343 is 1

Here are a couple more to try:

The digital root of 3252 is 3
The digital root of 987 is 6

12. Write a program to find the Greatest Common Divisor (GCD) for two positive integers. To find the GCD you will 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

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

Enter integer 1: 64
Enter integer 2: 44

The GCD of 64 and 44 is 4

Here are a couple more to try:

The GCD of 32 and 128 is 32
The GCD of 35 and 113 is 1

13. The so-called Hailstone numbers are sequences of integers generated in the Collatz problem, which was first proposed by L. Collatz in 1937.

The Hailstone sequence of numbers can be generated from a starting positive integer, n by:

1. If n is 1 then the sequence ends.
2. If n is even then the next n of the sequence = n/2
    If n is odd then the next n of the sequence = (3 * n) + 1
3. Repeat Step 2 with the next n

For example, for a starting number of 5, the sequence is 5, 16, 8, 4, 2, 1, 4, 2, 1, ...

Such sequences are called hailstone sequences because the values typically rise and fall, somewhat analogously to a hailstone inside a cloud. While a hailstone eventually becomes so heavy that it falls to ground, every starting positive integer ever tested has produced a hailstone sequence that eventually drops down to the number 1 and then "bounces" into the small loop 4, 2, 1, ....

Your task - write a program that will accept a positive integer, then generate and display the sequence that results from that seed value - stopping when the number gets to 1. Include the number of cycles required to get to 1.

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

Enter an integer seed value for the Hailstone sequence: 5

5, 16, 8, 4, 2, 1 (6 cycles)

Here are some others to try:

A seed of 12 produces: 12, 6, 3, 10, 5, 16, 8, 4, 2, 1 (10 cycles)

A seed of 7 produces: 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 (17 cycles)

14. Write a modified version of program AP305 in which the user enters an unknown number of integers. An entry of 0 will serve as a signal that the user is done. The 0 will NOT count as one of the numbers, however. See the example.

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
Enter integer #6: 3
Enter integer #7: 36
Enter integer #8: 0

Lowest Number: 3
Highest Number: 36
Average Value: 20.4

15. Write a program that will allow the user to play a guessing game with the computer. The computer will pick a random number from 1 to 100. The user then is asked to guess the computer’s number. If the user is right, say so, output the number of guesses it took and end the program. If the user is wrong, state whether the guess is TOO LOW or TOO HIGH and allow the user to guess again, until they get it right.

Sample Run of the Program (input values are in bold type):

Guess #1: 50
TOO HIGH. TRY AGAIN.
Guess #2: 25
TOO LOW. TRY AGAIN.
Guess #3: 37
TOO HIGH. TRY AGAIN.
Guess #4: 32
RIGHT ON! YOU GOT IT IN 4 GUESSES.

16. Write a program that will allow you to play a game of Rock-Paper-Scissors against the computer. In the game, the computer randomly selects rock, paper or scissors. Yopu also make a choice. If both choose the same, it is a tie and the game repeats. Otherwise, the winner is based on the following:

ROCK beats SCISSORS
SCISSORS beats PAPER
PAPER beats ROCK

Use a random number for the computer's choice. You will make your choice by entering a number from 1-3 based on the following (provide a menu):

1 = ROCK
2 = PAPER
3 = SCISSORS

Sample Run of the Program (input values are in bold type):

Your choice: 2
PAPER
Computer:
PAPER
Result: TIE

Your choice: 3
SCISSORS
Computer:
ROCK
Result: Computer Wins!

 
 

 

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