0% found this document useful (0 votes)
36 views

Lab 6 Functions Solution

The document is a laboratory manual for an experiment on functions. It provides examples of functions, such as computing factorials, checking for prime numbers, calculating distance between points, converting between Celsius and Fahrenheit, and summing the digits of an integer. It also includes sample solutions for homework problems involving writing functions.

Uploaded by

Sudesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Lab 6 Functions Solution

The document is a laboratory manual for an experiment on functions. It provides examples of functions, such as computing factorials, checking for prime numbers, calculating distance between points, converting between Celsius and Fahrenheit, and summing the digits of an integer. It also includes sample solutions for homework problems involving writing functions.

Uploaded by

Sudesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Think Twice

Code Once

The Islamic University of Gaza


Engineering Faculty
Department of Computer Engineering
Fall 2017
LNGG 1003
Khaleel I. Shaheen

Introduction to Computers

Laboratory Manual

Experiment #6 Solution

Functions
Experiment #6: Functions

Homework
1. Write factorial(num) function that computes the factorial of an integer.
Solution:
def factorial(num):
fact = 1
for i in range(1, num + 1):
fact *= i
return fact
2. Write a function print_prime_numbers(num) that prints all the prime numbers up to
num. (hint: use isPrime function from the lab work.)
Solution:
def isPrime(num):
if num < 2:
return False
for i in range(2, num):
if (num % i) == 0:
return False
return True

def print_prime_numbers(num):
for i in range(1, num + 1):
if isPrime(i):
print(i)
3. Using the following function header, write a function that computes the distance
between two points.
def distance(x1, y1, x2, y2):
Solution:
def distance(x1, y1, x2, y2):
return ((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)) ** 0.5

4. Complete the following two functions implementation:


# Converts from Celsius to Fahrenheit
def celsiusToFahrenheit(celsius):

# Converts from Fahrenheit to Celsius


def fahrenheitToCelsius(fahrenheit):

2
Experiment #6: Functions

The formulas for the conversion are:


celsius = (5 / 9) * (fahrenheit – 32)
fahrenheit = (9 / 5) * celsius + 32
Solution:
# Converts from Celsius to Fahrenheit
def celsiusToFahrenheit(celsius):
return (9.0 / 5.0) * celsius + 32

# Converts from Fahrenheit to Celsius


def fahrenheitToCelsius(fahrenheit):
return (5.0 / 9) * (fahrenheit - 32)

5. Write a function that computes the sum of the digits in an integer. Use the following
function header:
def sum_digits(n):
For example, sum_digits(234) returns 9 (2 + 3 + 4).
Solution:
def sumDigits(n):
temp = abs(n)
sum = 0

while temp != 0:
remainder = temp % 10
sum += remainder
temp = temp // 10

return sum

Good Luck

You might also like