created 05/24/03; edited 11/09/2012, 06/10/2018, 01/21/2019


Chapter 24 Programming Exercises

Many of your programs from previous chapters can modified by changing while loops into the equivalent for loops. Some of these exercises are repeats of previous ones.

Exercise 1 — Sheep Herd Size

A breeding group of 20 bighorn sheep is released in a protected area in Colorado. It is expected that with careful management the number of sheep, N, after t years will be given by the formula:

N = 220/(1 + 10(0.83)t )

and that the sheep population will be able to maintain itself without further supervision once the population reaches a size of 80.

Write a program (using a for loop) that writes out the value of N for t starting at zero and going up to 25. How many years must the sheep heard be supervised?

Hint: don't calculate (0.83)t   "from scratch" each time the formula is used. Use a variable power that is multiplied by 0.83 in each iteration of the loop. What value should it be initialized to?

(Problem from Howard Anton, Calculus, 6th ed., p. 105. )

Click here to go back to the main menu.


Exercise 2 — Sum of Odd integers 1 to N

Write a program that asks the user for an integer N and then calculates two things: (a) the sum of all odd integers from 1 to N, and (b) N2. Use a for loop with appropriate increment.

Click here to go back to the main menu.


Exercise 3 — Sum of Divisors of N

Write a program that asks the user for an integer N and then calculates the sum of all integers from 1 to N that divide N with zero left over. Use the % operator.

For example, if N is 10, then the sum of divisors is 1+2+5 = 8. Notice that 1 is considered a divisor and than no integer greater than N/2 is a divisor of N.

Another example: if N is 6, then the sum of divisors is 1+2+3 = 6. In this case, 6 is considered a perfect number because the sum of divisors is the number itself.

Include an if statement that determines if N is a perfect number.

Click here to go back to the main menu.


Exercise 4 — Is an Integer the Sum of Two Squares?

Write a program that asks the user for an integer N and then determines if N is the sum of two the squares of two integers.

Integers that are a sum of two squares:

Integers that are not a sum of two squares:

One way to do this is with a doubly-nested loop that generates trial integers a and b, squares each, and tests if the sum is equal to N. Of course, when a2 exceeds N the outer loop is done. Decide on a termination condition for the inner loop.

Click here to go back to the main menu.


Exercise 5 — Is an Integer the Sum of Two Squares in two different Ways?

The integer 125 is the sum of two positive squares in two different ways. Changing the order of the squares is not regarded as a different way.

125 = 102 + 52 = 100 + 25
125 = 112 + 22 = 121 + 4

Write a program that asks the user for an integer N and then determines if it is the sum of two positive squares in two different ways.

Another way to check if an integer is the sum of two squares is with a loop that generates trial integers from one up to the square root of N. For each trial, check if (N-trial*trial) is a perfect square. Do this by calculating its square root and checking that it is an integer. Math.sqrt() and a type cast with (int) will be useful (see chapter 13). Increment a counter each time two squares sum to N.

PS>java SumTwoSquares
N --> 50
50 is the sum of 2 squares
50 == 1*1 + 7*7
50 == 5*5 + 5*5

PS>

You might think that using Math.sqrt() is more efficient than the doubly nested loop used in Exercise 4. However, Math.sqrt() is an expensive operation that is, in fact, implemented with a loop. You just don't see it.

Click here to go back to the main menu.


Exercise 6 — Find all integers that are the Sum of Two Squares in two different Ways

Ask the user for an upper limit and then write out those integers up to the limit that are the sum of two positive squares.

Do this by wrapping a loop around the previous program (and making a few other changes.)

PS>java SumTwoSquares
enter upper limit --> 100
50 is the sum of 2 squares
50 == 1*1 + 7*7
50 == 5*5 + 5*5

65 is the sum of 2 squares
65 == 1*1 + 8*8
65 == 4*4 + 7*7

85 is the sum of 2 squares
85 == 2*2 + 9*9
85 == 6*6 + 7*7

PS>

Can you find any integers that are the sum of two squares in three different ways?

Click here to go back to the main menu.


Exercise 7 — Find cubes that are the Sum of Two Squares in two different Ways

Ask the user for an upper limit, LIMIT and then writes out those integers up to the limit that are cubes which are the sum of two positive squares.

Do this by modifying the previous program so that it tests if N is a perfect cube. If it is, then it tests if it is the sum of two squares as before.

PS>java CubicSumTwoSquares
 
enter upper limit --> 1000
125 is a cube: 5^3
125 is the sum of 2 squares
125 == 2*2 + 11*11
125 == 5*5 + 10*10

1000 is a cube: 10^3
1000 is the sum of 2 squares
1000 == 10*10 + 30*30
1000 == 18*18 + 26*26

PS>

Click here to go back to the main menu.


Exercise 8 — Maximum of Integers in a File

Write a program that reads 5 integers from a file, computes their sum and their maximum and prints these values to the monitor. Do this by modifying the summing program from the chapter. Use an int variable called max which is initialized to the first value in the file. This calls for an extra set of input statements before the loop starts. To compute the maximum use an if statement nested inside the loop.

Click here to go back to the main menu.