Lab
Simple Algorithms

Goal
To explore various algorithms for solving problems using the built-in simple types in C++

  1. Use only the four simple types: String, Integer, Double, Boolean
  2. Do not use any functions in java.math;
  3. Verify your work with a menu-driven program ("simple.java") from your teacher.

  1. Write a function factors (Integer num) that will print all the factors of a given positive integer. For example, factors (30) should produce the following formatted output (note that a period terminates the list):
        The factors of 30 are 1, 2, 3, 5, 6, 10, 15, 30.
  2. Write a function GCD(Integer a, Integer b) that returns the greatest common divisor of its two positive integer parameters.
  3. Write a Boolean function prime(Integer num) that determines whether a given integer greater than one is a prime number. Use this header:
    Boolean prime(Integer num)
  4. Write a function Double power (Double base, Integer exponent) that raises a given number (floating point or integer) to a given (positive, negative, or zero) integer power. Do NOT use pow (x) or log (x) in your solution. Note that if the base is zero, the exponent must be positive.
  5. Write a function findDigit (Integer num, Integer n) that returns the nth digit from the right of a given integer where n is a positive integer. For example,
    findDigit (30568,2) will return 6
    findDigit(234,5) will return 0
    findDigit(-4532,3) will return 5
  6. Write a procedure downDigits(Integer num) that will list the digits of a positive integer in one column. For Example:
    downDigits (560) will produce this output:
    The digits of 560 are:
    5
    6
    0

Bonus!
Write a function countDigits (Double num) that returns the number of digits to the left of the decimal point of a valid floating point number. Note that countDigits (0.74) returns 1.


Continue to:  Unit 1  / Prev  / Next