• If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

View

# Programming Exercises - Mathematical Functions

last edited by 1 year, 7 months ago

1. Write a program that reads a time interval in hours, minutes, and seconds, and converts this measurement to hours only.

For example, if the inputs values were 13 hours and 15 minutes and 30 seconds, the output should be something like:

13 hours, 15 minutes, 30 seconds is 13.26 hours

Hints (select the text to highlight and make visible):
1. How would you convert 15 minutes to hours? Can you program this?
2. How would you convert 30 seconds to minutes? Then to hours?
3. Once you have converted minToHours and secToHours, add your three hour values together.

2. In trigonometry, angles are typically represented in degrees and radians.  Write a program which asks for an angle in degrees and converts it to radians.  Confirm your program is working by converting your first answer back to degrees.
Hint: Your programming language may have a way to automatically convert between degrees and radians.
Extend: Research how to do this calculation without using any built-in conversion tools (i.e., just use add, subtract, multiply, divide).

3. The general quadratic formula can be used to solve for the zeroes of a quadratic equation.  Write a program which asks the user for values of A, B, and C from a quadratic equation, and then uses the general quadratic formula to solve for the zeroes.
Warning: Depending upon the values for A, B, and C, you may get errors or crashes when you run your program.  This is okay for this exercise!

4. Some problems require rounding to a specific number of decimal places (e.g., money should always be rounded to two decimal places to represent dollars and cents).  In other cases, rounding is simply used to provide a nicer looking output.  While many programming languages provide for rounding as part of their output protocols, it is also possible to achieve rounding using the rounding functions provided by the programming language (proper rounding, round up, round down).
(a) Write a program which will prompt the user for a decimal value, round this value to two decimal places, and then output the newly rounded value.
(b) Modify the program to allow the user to specify how many decimal places to use.

5. (a) Create a program generates a random value between 1 and N, where N is an integer specified by the user.
(b) Modify your program to allow the user to specify the starting value and the ending value, both integers.
(c) Modify your program to allow the user to specify the integer step between values.  For example, numbers between -5 and 5 with a step of 2 could be (at random) -5, -3, -1, 1, 3, 5.

(d) Modify your program so all values are real (decimal) values.

6. (challenge) In geography, latitude and longitude are expressed in degrees.  For example, a latitude of 38.47°.  It is possible to express the decimal part in other units based on a clock, where 1 degree is broken into 60 minutes, and 1 minute is broken into 60 seconds.

Write a program that reads an angle measurement in degrees and converts it to a measurement using degrees, minutes, and seconds.

For example:
input:  38.47
output:  38.47 degrees  = 38 degrees, 28 minutes, 12 seconds

Hints:
1. Split the user input into integer (e.g., 38) and decimal (e.g., 0.47) using a combination of rounding functions (proper rounding, round up, round down) and arithmetic. The integer part is degrees.
2. Convert the decimal part (e.g., 0.47) into minutes using multiplication (see Ex.1 for help).
3. Split the minutes into an integer and decimal part. The integer is minutes. Convert the decimal part of minutes into seconds using multiplication.