# 5.16. Coding Practice¶

Write a function called calculator which takes two doubles, first and second and a char operation as parameters. calculator performs addition, subtraction, multiplication, or division with the two doubles depending on what operation is passed in (+, -, *, /). It then returns the result. Run and test your code!

Below is one way to implement the calculator function. Using conditionals, we return the correct result depending on which operation was given.

Selecting from: cp_5_AC_2q, cp_5_AC_2q_pp

An interior angle of a polygon is the angle between two adjacent sides of the polygon. Each interior angle in an equilateral triangle measures 60 degree, each interior angle in a square measures 90 degrees, and in a regular pentagon, each interior angle measures 108 degrees. Write the function calculateIntAngle, which takes a numSides as a parameter and returns a double. calculateIntAngle finds the interior angle of a regular polygon with numSides sides. The formula to find the interior angle of a regular ngon is (n - 2) x 180 / n. Run and test your code!

Below is one way to implement the program. Using the formula given, we can find the interior angle and return it. Notice how we use 180.0 instead of 180 to avoid integer division.

Selecting from: cp_5_AC_4q, cp_5_AC_4q_pp

Dog owners will know that figuring out a dog’s age is more complicated than just counting age directly. Dogs mature faster than humans do, so to get a more accurate calculation of a dog’s age, write the dogToHumanYears function, which takes an dogAge as a parameter. dogToHumanYears converts and returns the dog’s age to human years. A one year old dog is 15 years old in human years; a two year old dog is 24 years old in human years. Each year after the second year counts as 4 additional human years. For example, a dog that is 3 years old is actually 28 years old in human years. Run and test your code!

Below is one way to implement the program. We can use a conditional to check to see if the dog is one year old. If it is older than one, then we can use the formula to return the correct age in human years.

Selecting from: cp_5_AC_6q, cp_5_AC_6q_pp

If a year is divisible by 4, then it is a leap year. However, if it is also divisible by 100, then it is not a leap year. However, if it is also divisible by 400, then it is a leap year. Thus, 2001 is not a leap year, 2004 is a leap year, 2100 is not a leap year, and 2000 is a leap year. Write the boolean function isLeapYear, which takes a year as a parameter and returns true if the year is a leap year and false otherwise. Run and test your code!

Below is one way to implement the program. We can use conditionals in this order to efficiently determine whether or not a given year is a leap year.

We know that a factorial is the product of an integer and all the integers below it. For example, four factorial (4!) is 24. A triangular number is the same as a factorial, except you add all the numbers instead of multiplying. For example, the 1st triangular number is 1, the 2nd is 3, the 3rd is 6, the 4th is 10, the 5th is 15, etc. You can imagine rows of dots, where each successive row has one more dot, thus forming a triangular shape. Write the triangularNum function, which takes an int n as a parameter and returns the nth triangular number. Use recursion. Run and test your code!
Below is one way to implement the program. We can use conditionals to separate the base case and recursive cases. Our base case is when n is 1, and in that case we return 1. Otherwise, we recursively call triangularNum on n-1.