19.2.3.1. Exercises: Iteration

  1. Write the function isPrime(int n) that returns true if n is prime and false otherwise. Supply an interactive main() to test your function.

  2. Write the function primesBetween(int min, int max) that displays on the screen all the prime numbers that are between min and max. Supply an interactive main() to test your function.

  3. Write the function power2(int n) that computes and returns the value of 2 raised to the power n. Supply an interactive main() to test your function.

  4. Write a binary logarithm function binLog(int n) that computes and returns an int equal to the integral part of log2(n), where n is positive. This is equivalent to finding the exponent of the largest power of 2 that is less than or equal to n. For example, binLog(25) is 4. There are at least two simple, iterative ways to do this computation. Supply an interactive main() to test your function.

[ fromfile: controlstructures.xml id: None ]