LAB #3 - Control Flow
LAB #3 - Control Flow
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:
Input: A
Expected output:
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:
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)
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.
8. Given an integer number, n, make the sum of all the numbers from 0 to n.
Input: 5
Expected output:
15
Input: 9
Expected output:
* . . . . . . . *
. * . . . . . * .
. . * . . . * . .
. . . * . * . . .
. . . . * . . . .
. . . * . * . . .
. . * . . . * . .
. * . . . . . * .
* . . . . . . . *
Input: 5
Expected output:
*****
****
***
**
*
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:
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
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:
* . .
* * .
* * *
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.
----------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
Input: base_stars = 15
Expected output:
*
***
*****
*******
*********
***********
*************
***
***
***
Input: 5, 8
Expected output:
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