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

LAB #3 - Control Flow

Ejercicios Python control
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

LAB #3 - Control Flow

Ejercicios Python control
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

LAB #3 - Exercises

Use Google Colab to write the notebooks. Enter your questions in the following notebook (copy & paste):
Colab_blackboard https://colab.research.google.com/drive/1m6AVA9C7oOGXG93ZwoTsg0CThVq2oaXd?usp=sharing

1. Write a program that given an integer numbers, prints out if it is divisible by 5 and 11.

Input: 55
Expected output:

The number 55 is divisible by 5 and 11.

2. Write a program to check that a character is a letter in uppercase.

Make use of these functions:

value.isupper() -->Returns True if the value is in uppercase.

value.isalpha() -->Returns True if the value is a letter.

Input: A
Expected output:

The character A is in uppercase.

3. Write a program that given a month name, prints the number of days (if the name is not correct, it shall print out some error
message). Remember: April, June, September, November have 30 days.

Input: January
Expected output:

January has 31 days.

5. Write a program that prints out the numbers from 10 to 0 (only odd numbers). Use both: while and for loops.

Input: no input

Expected output:

9
7
5
3
1

6. Write a program to calculate the factorial of an integer number. Use both: while and for loops.

factorial(n) = n * factorial(n-1)

Input: an integer number, 5

Expected output:

120

7. Write a program to calculate the exponentiation of a number with base b, and exponent n, both: while and for loops.

Input: base 2, exponent 3


Expected output:

8. Given an integer number, n, make the sum of all the numbers from 0 to n.

Input: 5

Expected output:

15

9. Given an integer number, n, print a cross of radius n as follows.

Input: 9

Expected output:

* . . . . . . . *
. * . . . . . * .
. . * . . . * . .
. . . * . * . . .
. . . . * . . . .
. . . * . * . . .
. . * . . . * . .
. * . . . . . * .
* . . . . . . . *

10. Given an integer number, n, print a wedge of stars as follows.

Input: 5

Expected output:

*****
****
***
**
*

11. Make a program to display the multiplication table (1-10).

Input: no input
Expected output:

1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100

12. Write a program to sum all odd numbers between n and 100 (included).

Input: 11
Expected output:

The sum between 11-100 is 2475


13. Write a program to show the square of the first 10 numbers.

Input: no input
Expected output:

The square of 0 is 0
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
The square of 6 is 36
The square of 7 is 49
The square of 8 is 64
The square of 9 is 81
The square of 10 is 100

14. Write a program to calculate the combinatorial number

Input: m = 10, n = 5

Expected output:

252

17. Write a program to print an * in the lower diagonal of an implicit matrix of size n.

Input: n = 3

Expected output:

* . .
* * .
* * *

18. Write a program to print a diamond of radius n.

Input: n = 7

Expected output:

. . . * . . .
. . * * * . .
. * * * * * .
* * * * * * *
. * * * * * .
. . * * * . .
. . . * . . .

19. Write a program that displays a table of size (n x n) in which each cell will have * if i is a divisor of j or "" otherwise.

Input: n = 10
Expected output:
* * * * * * * * * * 1
* * * * * * 2
* * * * 3
* * * * 4
* * * 5
* * * * 6
* * 7
* * * * 8
* * * 9
* * * * 10

20. Write a program to display a menu with 3 options (to say hello in English, Spanish and French) and to finish, the user shall
introduce the keyword "quit". If any other option is introduced, the program shall display that the input value is not a valid
option.

Input: test the options


Expected output:

----------MENU OPTIONS----------
1-Say Hello!
2-Say ¡Hola!
3-Say Salut!
> introduce an option or quit to exit...

21. The number finder: write a program that asks the user to find out a target number. If the input value is less than the target
number, the program shall say "the target value is less than X", otherwise, the program shall say "the target value is greater
than X. The program shall finish once the user finds out the target number.

Input: target = 10

Expected output:

Introduce a number: 3
The target value is greater than 3
Introduce a number: 5
The target value is greater than 5
Introduce a number: 12
The target value is less than 12
Introduce a number: 10
Homework
4. Write a program that given the 3 integer numbers, it prints out the sorted values (in descending order) using conditional
statements..

Input: 3, 5, 2
Expected output:

5, 3, 2

15. Write a program to print a Christmas tree of 15 base stars.

Input: base_stars = 15

Expected output:

*
***
*****
*******
*********
***********
*************
***
***
***

11. Write a program to detect if a number is a prime number.

Input: 5, 8
Expected output:

The number 5 is prime.


The number 8 is not prime.

22. Write a program to find greatest common divisor (GCD) of two numbers.

Input: x = 54, y = 24
Expected output:
Tip: try to improve the algorithm following the strategies here: https://en.wikipedia.org/wiki/Greatest_common_divisor

The GCD of 54 and 24 is: 6

You might also like