Exercises - Functions


 

  1. (a) Create a function that determines the circumference of a circle given the radius.  Write a program that will let you test your function repeatedly (preferably using a loop).

    (b) Add a function for determining the area of a circle, and add this information to your output for the user.

    (c) Are there values for the radius that don't make sense?  Disallow this type of input and notify the user of the problem.  Since you should be using a loop, the user will be asked to enter their value again.

    (d) Build in a way to exit the program.

     

  2. (a) Write a function min2() that will accept two integer values and returns the smaller of the two values.

    (b) Write a function max2() that will accept two integer values and returns the larger of the two values.

    Turing Solution
    (c) Write a function min3() and max3() which work for three integer values.
    (d) Use method overloading to modify your methods to be named max() and min(), with different code to handle two or three input integer parameters.

     

  3. (a) There is a concept in mathematics called "factorial", which is the product of all positive integers from the selected number down to 1.  This operation is denoted in mathematics with the exclamation point (!).

    For example, 5! = 5 x 4 x 3 x 2 x 1 = 120

    Create a function to implement the factorial operation and write a program to test it.

    (b) Enhance your program to disallow invalid values.

     

  4. (a) Create a boolean function isEven() that will accept an integer value and return true if it is an even number (i.e., evenly divisible by two), and otherwise it will return a false.  Write a program that will let you test your program using a loop.

    (b) Use a random number instead of user input, and let the user specify how many numbers to run.  At the end of the program, report the total number of evens and odds.

     

  5. *** (a) Create an integer function called digit() that accepts a positive integer (N) and requests the kth digit (k).  The function should return the digit as an integer.

    For example, digit(54321, 2) would return the second digit, which is the integer value '4'

    (b) Create a boolean function (true/false) called allDigitsEven() that will accept a positive integer value and return true if each digit is even, and false if any digit is not even. (Hint: You will need to use string functions to accomplish this task).

    (c) Use this function to write the program for Exercises - String Operations #6.

     

  6. (a) Create a boolean function called isPrime() that will accept a positive integer value and determine if it is a prime number. Recall: a prime number is a number that is divisible only by one and itself.  Write a program that will allow you to test your function.

    (b) Write a program to allow the user to specify a number, and display all of the prime numbers between 1 and the user's number.  This program will use your isPrime() function developed in part (a).

     

  7. In a leap year, February has 29 days.  A leap year can be determined as follows:

         - a year that is divisible by 4 (e.g., 1988, 1992, 1996 are all leap years)

         - centennial years (divisible by 100) do not follow this rule (e.g., 1800, 1900 are NOT leap years)

         - except centennial years divisible by 400, which are leap years (e.g., 2000 is a leap year)

    (a) Create a boolean function that will determine if a given year is a leap year by returning true or false for a given year.

     

  8. The absolute value is a mathematical function for determining the size, or magnitude, of a value.  The answer is always a positive number.

    For example, the absolute value of 5 is 5.  The absolute value of -11.2 is 11.2.

    (a) Write a function called absolute() that accepts a real input and gives a real output.  Write a simple program to test your function.  If your programming language already has an absolute value function, do not use it.  The goal here to is write your own version of the function. (Turing Solution)