Docsity

Programming - Lab Session 3: Control flow I, conditional statements (if and switch), Ejercicios de Programación Informática

Toggle sidebar
bg1
Programming.
Bachelor in Aerospace Engineering.
2020-21
1
Lab Session 3. Control Flow (I). Conditional
statements (if and switch)
Exercise 1.
Write a program that reads an integer number from the keyboard and prints a message to the
screen if the number is larger than 100. If the number is smaller or equal than 100 the program
must do nothing.
Exercise 2.
Write a program where you declare the variables day, month and year (integer values) and
initialize them with your date of birth. Then, the program must ask the user for the current day
and month (current_day, current_month) and display “Happy Birthday” if the current day is your
birthday.
Exercise 3.
Write a program that reads two integer numbers and displays which one is the largest ("the first
is the largest" or "the second is the largest"). Consider the case as well where both numbers are
equal.
Exercise 4.
Write a program that reads two integer numbers and tells the user if the first one is divisible by
the second.
Exercise 5.
Write a program that reads an integer value and displays a message telling the user if it is an
even or an odd number. If the number is even, then the program has to say if it is divisible by 3.
Exercise 6.
Write a program that reads three integer values (a, b, and c) and prints “Ordered” if they have
been provided in decreasing order (a>b>c) and “Not ordered” otherwise.
Exercise 7.
Write a program that reads three integer values and finds the smallest of them.
(the max built-in function is not allowed for the subject).
Exercise 8.
Write a program to compute the value of the function below for a value x entered by the user
pf3
pf4

Vista previa parcial del texto

¡Descarga Programming - Lab Session 3: Control flow I, conditional statements (if and switch) y más Ejercicios en PDF de Programación Informática solo en Docsity!

Bachelor in Aerospace Engineering. 2020 - 21

Lab Session 3. Control Flow (I). Conditional

statements (if and switch)

Exercise 1.

Write a program that reads an integer number from the keyboard and prints a message to the screen if the number is larger than 100. If the number is smaller or equal than 100 the program must do nothing.

Exercise 2.

Write a program where you declare the variables day, month and year (integer values) and initialize them with your date of birth. Then, the program must ask the user for the current day and month ( current_day , current_month ) and display “Happy Birthday” if the current day is your birthday.

Exercise 3.

Write a program that reads two integer numbers and displays which one is the largest ("the first is the largest" or "the second is the largest"). Consider the case as well where both numbers are equal.

Exercise 4.

Write a program that reads two integer numbers and tells the user if the first one is divisible by the second.

Exercise 5.

Write a program that reads an integer value and displays a message telling the user if it is an even or an odd number. If the number is even, then the program has to say if it is divisible by 3.

Exercise 6.

Write a program that reads three integer values ( a , b , and c ) and prints “Ordered” if they have been provided in decreasing order ( a>b>c ) and “Not ordered” otherwise.

Exercise 7.

Write a program that reads three integer values and finds the smallest of them. (the max built-in function is not allowed for the subject).

Exercise 8.

Write a program to compute the value of the function below for a value x entered by the user

Bachelor in Aerospace Engineering. 2020 - 21 𝑓(𝑥) = {

𝑥^2 + 2 𝑥 𝑖𝑓 𝑥 > 0

Exercise 9.

Write a program that prints to the screen the price of a cinema ticket according to the age of the customer. The program must read the user’s age and calculate the price as follows: (a) Normal ticket: 7 euros; (b) Children under 5: 60% discount; (c) People over 60: 55% discount. Use a variable to define the normal ticket price.

Exercise 10.

Write a program that reads a password with four chars and prints on the screen the message “Access granted” if the keyword 'u' 'c' '3' 'm' is provided and "access denied" otherwise. Hint 1: To read a character or string using the function input, you must include the string 's' as the second argument. Example: S2 = input('Type in some character(s) or string','s') Hint 2 : In order to determine string equality, you can use the isequal() function. Get information from the command window (help isequal).

Exercise 1 1.

Write a program that reads an integer value. If the value is positive and has two digits, the program must then display both the sum of the two digits, and the number of even digits. Note: to find out if the number has two digits, compare it to 9 and to 99. To separate the digits, use the division and mod operations.

Exercise 1 2.

Write a program that reads a character and, using the switch instruction, prints to the screen if it is a vowel or not. Repeat the exercise using the if instruction. Which is the best option?

Exercise 1 3.

Write a program that prompts the user for a number between 1 and 7 and shows the corresponding day of the week.

Exercise 1 4.

Write a program that prompts the user for an integer number num representing a card of the Spanish deck (Baraja Española) and prints on the screen the corresponding card name:

  • “Ace (As)” if num is 1
  • “Jack (Sota)” if num is 10
  • “Knight (Caballo)” if num is 11

Bachelor in Aerospace Engineering. 2020 - 21 % (set by uniform distribution)