BIS1003-Introduction to Programming Intensive

profileAbdulmalek
BIS1003_Workshop_Topic5.pdf

BIS1003 Introduction to Programming

Workshop 5

Topic 5: Functions I

Apply and test your knowledge of the current and previous topics by attempting the questions below.

Completing these questions will help you to succeed in your subject.

Workshop Questions

1. Design a program using a flowchart. This program will accept a number between 1

and 100. Pass this value to a function. This function should display “The number is

odd” if the passed value is an odd number, and display “The number is even” if the

passed value is even number.

2. Write a python program for your program design in question 1.

3. Write a python program that will accept a positive number and pass this number to a

function called “prime_function”. This function takes one argument as a positive

number and prints out to the user if the number is prime or not. You need to call this

function from the main().

Hint: a prime number is a whole number greater than 1, that has only two factors one

and the number itself. A prime number is divisible only by number 1 or itself.

For example, 2, 3, 5, 7 and 11 are the first few prime numbers.

To check 3 is a prime number. Start from number 2 and check if 3 is divisible by 2

(use remainder operator: %). If not divisible by 2, move to next divisor, 3. If divisible

by 3, stop iterating and report the number as prime. However, if you have a big

number (e.g. 375), if the divisor value exceeds the number’s square root, stop

iterating and print it as prime.

If a number divisible by any number except 1 and itself, stop and report the number

as non-prime.