Programs for HL Math Summer Homework

Input/Output, Assignment Statement, Math Functions

1) Write a program that prints Hello World to the screen.

2) Write a program that prompts the user for the radius of a circle and prints out both the circle's perimeter and area.

3) Write a program that prompts the user for the length of the side of a regular hexagon and prints out the area of that hexagon.

4) You are attempting to determine the height of a building (that is more than 5 stories tall) as well as your distance from the building. Here is the information you know:

a) The angle of elevation to the fifth story window from your location on the ground.

b) The angle of elevation to the top of the building from your location on the ground.

c) The distance from the fifth story window to the top of the building.

Write a program that prompts the user for these three pieces of information. Have the user enter the first two measurements in degrees. Your program should output both the height of the building and the distance the user is from the building.

If Statement

5) Write a program that does temperature conversion from either Fahrenheit to Celsius, or the other way around. Your program should first prompt the user for which type of conversion they want to do. Then your program should prompt the user for the temperature they want to convert. Finally, your program should output the proper converted temperature. Incidentally, the formula for conversion from celsius to fahrenheit is:

F = 1.8*C + 32

6) Debbie likes numbers that have the same tens digit and units digit. For example, Debbie likes 133 and 812355, but she does not like 137 or 4. Write a program that asks the user for a number and then prints out whether or not Debbie likes the number.

Loops

7) Write a program that asks the user for a positive even integer input n, and the outputs the sum 2+4+6+8+...+n, the sum of all the positive even integers up to n. (Note: See if you can also derive a formula for this sum in terms of n.)

8) Write a program that calculates the amount of money stored in a bank account after a certain number of years. The user should enter in the initial amount deposited into the account, along with the annual interest percentage and the number of years the account will mature. The output should provide the amount of money in the account after every year. (Note: See if you can derive a formula for the amount of money in the bank account at the end of the time entered. Also, if you'd like, add the ability to compound the interest, and continually compound the interest.)

9) Write a program to take in a positive integer n > 1 from the user and print out whether or not the number is prime. (A prime number is only divisible by 1 and itself, and no other positive integers.) (Note: Adjust the program so that it prints out the first 200 prime numbers.)

10) A pythagorean triple is a set of three integers that are the lengths of the sides of a right triangle. For example, (3,4,5) and (8,15,17) are both pythagorean triples. Write a program that prints out all pythagorean triples that have at least two of their three values less than 100. (Hint: Loop through all possible pairs of lengths of the legs of the triangle, but only print the ones where the corresponding hypotenuse is an integer.)

11) Write a program that prints out all ordered triplets of integers (a,b,c) with a < b < c such that a+b+c = 15. (If you'd like, instead of the sum being 15, you can have the user enter an integer greater than or equal to 6.) You should print out one ordered triplet per line. (Note: See if you can figure out the answer to this question analytically in terms of n, where n is the sum of the three numbers. This is a hard question!)

Functions

12) Write a function that takes in a positive integer and returns true if that integer is prime and false otherwise. Here is the function prototype:

bool isprime(int n);

13) Write a function that calculates n! given n as input. Here is the prototype:

int factorial(int n);

14) The Fibonacci numbers are defined as follows: F0=0, F1=1, Fn= Fn-1+ Fn-2, for all n>1. Write a function that takes in a non-negative integer n, and returns the nth Fibonacci number. Here is the prototype:

int fibonacci(int n);