[go: up one dir, main page]

0% found this document useful (0 votes)
6 views18 pages

Lab 005 Ans

The document outlines a series of programming tasks for a computer science lab, focusing on Python programming concepts such as factorial calculation, repeated addition for multiplication, digit sum, factor sum, prime number checking, and basic statistics. Each task includes pseudocode, flowcharts, and Python code examples. Additionally, it covers turtle graphics functions for drawing shapes and polygons.

Uploaded by

bamatti05
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)
6 views18 pages

Lab 005 Ans

The document outlines a series of programming tasks for a computer science lab, focusing on Python programming concepts such as factorial calculation, repeated addition for multiplication, digit sum, factor sum, prime number checking, and basic statistics. Each task includes pseudocode, flowcharts, and Python code examples. Additionally, it covers turtle graphics functions for drawing shapes and polygons.

Uploaded by

bamatti05
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/ 18

Addis Ababa institute of Technology

Center of Information Technology and


Scientific Computing
Fundamentals of computer science and programming

Lab5 Iteration

Write pseudocode then draw flowchart finally code


1. Write a python program that accepts a number and prints its factorial
Pseudocode: Flow chart

Python code
2. Write a python program that allows the user to input two numbers, computes their
product and print out the product. Assume multiplication operation is not primitive
(not allowed) operation for the computer that you are writing the program. i.e. you
are not allowed to compute the product by directly multiplying the numbers. In other
words, your program should compute the product by repeated addition method.

Pseudocode : BEGIN
FUNCTION repeated_addition_product(a, b)
IF b < 0 THEN
a = -a
b = -b
END IF

SET product = 0
FOR i = 1 TO b DO
product = product + a
END FOR

RETURN product
END FUNCTION

PROMPT "Enter the first number: "


INPUT num1
PROMPT "Enter the second number: "
INPUT num2

CALL repeated_addition_product(num1, num2) AND STORE RESULT in result

PRINT "The product of" num1 "and" num2 "is:" result


END

Flow chart:
Python code:

3.
Write a

python program that accepts a positive number and prints the sum of the digits in
the number. i.e. if the number is 576, it should print 18 = 5 + 7 + 6.
pseudocode:
BEGIN
PROMPT "Enter a positive number: "
INPUT num
SET sum_of_digits = 0
WHILE num > 0 DO
SET digit = num % 10
sum_of_digits = sum_of_digits + digit
num = num // 10
END WHILE

PRINT "The sum of the digits is: " sum_of_digits


END
Flow Chart

Python code
4. Write a python program that accepts a number and prints the sum of its factors.
Pseudocode
BEGIN
PROMPT "Enter a number: "
INPUT num

SET sum_of_factors = 0

FOR i FROM 1 TO num (inclusive) DO


IF num % i == 0 THEN
sum_of_factors = sum_of_factors + i
END IF
END FOR

PRINT "The sum of the factors is: " sum_of_factors


END
Flow chart
Python code
5. Write a python program that determines if a given number is prime number or not.

Pseudocode Flow chart


BEGIN

PROMPT "Enter a number: "

INPUT num

IF num <= 1 THEN

PRINT "The number is not prime."

END

END IF

SET is_prime = True

FOR i FROM 2 TO (num // 2) DO

IF num % i == 0 THEN

SET is_prime = False

BREAK

END IF

END FOR

IF is_prime THEN

PRINT "The number is prime."

ELSE

PRINT "The number is not prime."

END IF

END
Python code

6. Write a python program that finds the average, maximum, minimum, and sum of n
numbers input by the user.

Pseudo code

Start

Input: Read the number of elements n

If n <= 0:

Print "The number of elements must be positive."

End

Initialize an empty list numbers


For each number from 1 to n:

Repeat until a valid number is input:

Input a number and add it to numbers

Calculate total_sum as the sum of numbers

Calculate average as total_sum / n

Find maximum and minimum of numbers

Print total_sum, average, maximum, and minimum

End

Flow Chart
Python code
7. Write a python program that generates a random number in the range 0 to 100 and
prompts the user to guess and enter the number continuously until the user’s input
matches the randomly generated number. The program should inform the user if
his/her guess is less/more than the random number. if the user can't guess the
value by entering at most 7 guesses the program should print GAME OVER

BONUS: stop accepting more guesses. Find a strategy that always results in a win
(i.e guess based on the less/greater clues)? Why does it always result in a win?

Pseudocode

START
SET random_number = generate random number between 0 and 100
SET attempts = 0
SET max_attempts = 7

WHILE attempts < max_attempts


PROMPT user to enter a guess
INCREMENT attempts by 1

IF user_guess < random_number THEN


PRINT "Your guess is too low."
ELSE IF user_guess > random_number THEN
PRINT "Your guess is too high."
ELSE
PRINT "Congratulations! You've guessed the number!"
EXIT

PRINT "GAME OVER. You've used all your attempts."


END
Flow chart
Python code
Back to Turtle
1. Modify the function that draws a square to use loop.

2. Write a function that draws a square using input parameters

x, y: x position and y position

length: length of the side of the square


3. Write a program that draws the picture below using the square function
written in question number 2
4. Write a function that could plot any regular polygon with equal side length
given parameters
 Number of sides
 Side length
 Starting point
(Hint: for a regular polygon the rotation angle is given by the formula
360/number_of_sides
)
5. Turtle has a circle function that takes radius as an input
Write a program using the turtle module that draws the picture bel ow

Use the following function to get random colors for the circles

6. Modify the above program to fill the circles with different colors
Use

<turtle_name>.fillcolor(<random color>)
# where the turtle name is the one you gave for the turtle and the random color you can get from
# get_random_color function

<turtle_name>.begin_fill()
# where the turtle starts filling the color

<turtle_name>.end_fill()

You might also like