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


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