Lab 005 Ans
Lab 005 Ans
Lab5 Iteration
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
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
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
INPUT num
END
END IF
IF num % i == 0 THEN
BREAK
END IF
END FOR
IF is_prime THEN
ELSE
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
If n <= 0:
End
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
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()