Worksheet 1 Function
Worksheet 1 Function
1. Write an int function cube () that returns the cube of its single int
formal
parameter.
2. Write a float function triangle() that computes the area of a triangle
using its two
formal parameters h and w, where h is the height and w is the length
of the bases
of the triangle.
3. Write a float function rectangle() that computes and returns the area of
a rectangle
using its two float formal parameters h and w, where h is the height
and w is the
width of the rectangle
4. The formula for a line is normally given as y = mx + b. Write a function
Line()
that expects three float parameters, a slop m, a y-intercept b, and an
x-coordinate
x. the function computes the y-coordinate associated with the line
specified by m
and b at x-coordinate.
5. Write a function Intersect() with four float parameters m1,b1,m2,b2.
The
parameters come conceptually in two pairs. The first pair contains the
coefficients
describing one line; the second pair contains coefficients describing a
second line.
The function returns 1 if the two lines intersect. Otherwise, it should
return 0;
6. Write a program that accepts a positive integer from the user and
displays the
factorial of the given number. You should use a recursive function
called
factorial() to calculate the factorial of the number.
7. Write another program that accepts a number from the user and
returns the
Fibonacci value of that number. You should use recursion in here.
8. Develop a program that uses the function factorial() of exercise 6 to
compute an
approximation of e (Euler’s number). Base your approximation on the
following
formula for e: 1 + 1/1! + 1/2! + 1/3! + …
9. Write a function called isPrime() that accepts a number and determine
whether the
number is prime or not.
10. Write a function called isEven() that uses the remainder
operator(%) to determine
whether an integer is even or not.
11. Modify the calculator problem using four functions sum(),
product(),quotient() and difference().