0% found this document useful (0 votes)
15 views2 pages

Worksheet 1 Function

The document outlines a series of programming exercises that involve creating various functions in Python. These include functions for calculating the cube of a number, the area of geometric shapes, determining line intersections, computing factorials and Fibonacci values, and checking for prime and even numbers. Additionally, it suggests modifying a calculator program to include basic arithmetic operations through defined functions.

Uploaded by

Chanyalew 21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Worksheet 1 Function

The document outlines a series of programming exercises that involve creating various functions in Python. These include functions for calculating the cube of a number, the area of geometric shapes, determining line intersections, computing factorials and Fibonacci values, and checking for prime and even numbers. Additionally, it suggests modifying a calculator program to include basic arithmetic operations through defined functions.

Uploaded by

Chanyalew 21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Worksheet

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().

You might also like