Docsity

Programming - Lab Session 4: Control flow II, loops and conditional statements, Ejercicios de Programación Informática

Toggle sidebar
bg1
Programming.
Bachelor in Aerospace Engineering.
2020-21
Lab Session 4. Control Flow (II). Loop and
conditional statements
Exercise 1.
Write a program that prints the integer numbers from 1 to 100.
Write a program that prints the integer numbers from 1 to 100 counting by twos (1, 3, 4, 5, …).
Write a program that prints the integer numbers from 100 to 1.
Write a program that prints the multiples of 5 from 1 to 100 (5, 10, 15, …) (counting by fives)
Exercise 2. Series
Modify the last program of the previous exercise to first ask the user how many terms of the
series to print, instead of printing until 100. (Example 4 terms: 5, 10, 15 ,20).
Exercise 3.
Write a program that asks the user for a number and then displays the message "the number
you entered is ___". The program must repeat this until the user enters the value 0.
Extend the program to count and print the total number of values entered by the user (excluding
value 0) and their sum.
Exercise 4. Ascii
Write a program that displays the ASCII table on the screen. To do this, print a table showing the
integer values 33 to 255 (%i), their associated ASCII character (%c), and the hexadecimal
encoding (%x). This can be done by writing the same number three times, using the appropriate
format specifiers, separated by a tab (tabulator \t).
fprintf ('%c \t\t %i \t\t %x \n', i, i, i);
Exercise 5. Mean
Write a program that reads 10 positive numbers from the keyboard and prints the following
values:
1. Sum of the numbers
2. Arithmetic mean of the numbers
3. Biggest number
4. Smallest number
Exercise 6. Power
Write a program that calculates the n-th power of a float value x (being n a positive integer
value) using the following two methods:
a) use myVar= x^n
b) multiplying the number by itself n times (using loop structures).
pf3

Vista previa parcial del texto

¡Descarga Programming - Lab Session 4: Control flow II, loops and conditional statements y más Ejercicios en PDF de Programación Informática solo en Docsity!

Bachelor in Aerospace Engineering. 2020 - 21

Lab Session 4. Control Flow (II). Loop and

conditional statements

Exercise 1.

Write a program that prints the integer numbers from 1 to 100. Write a program that prints the integer numbers from 1 to 100 counting by twos (1, 3, 4, 5, …). Write a program that prints the integer numbers from 100 to 1. Write a program that prints the multiples of 5 from 1 to 100 (5, 10, 15, …) (counting by fives)

Exercise 2. Series

Modify the last program of the previous exercise to first ask the user how many terms of the series to print, instead of printing until 100. (Example 4 terms: 5, 10, 15 ,20).

Exercise 3.

Write a program that asks the user for a number and then displays the message "the number you entered is ___". The program must repeat this until the user enters the value 0. Extend the program to count and print the total number of values entered by the user (excluding value 0) and their sum.

Exercise 4. Ascii

Write a program that displays the ASCII table on the screen. To do this, print a table showing the integer values 33 to 255 (%i), their associated ASCII character (%c), and the hexadecimal encoding (%x). This can be done by writing the same number three times, using the appropriate format specifiers, separated by a tab (tabulator \t). fprintf ('%c \t\t %i \t\t %x \n', i, i, i);

Exercise 5. Mean

Write a program that reads 10 positive numbers from the keyboard and prints the following values:

  1. Sum of the numbers
  2. Arithmetic mean of the numbers
  3. Biggest number
  4. Smallest number

Exercise 6. Power

Write a program that calculates the n-th power of a float value x (being n a positive integer value) using the following two methods: a) use myVar= x^n b) multiplying the number by itself n times (using loop structures).

Bachelor in Aerospace Engineering. 2020 - 21

Exercise 7. 100 numbers

Write a program that reads positive and/or negative integer numbers from the keyboard until the user enters 10 numbers or the sum of the numbers entered is larger than 100. The program must tell which condition was satisfied (“you entered 10 numbers”/ “sum larger than 100”) or if both were true.

Exercise 8. Divisible

Write a program to print the numbers in the range 1, …, 100 that are divisible by 5 and by 7. Note: You can use the built-in function mod(a,b)that we used in the previous lab session

Exercise 9. Guess secret number

Write a program that plays the guess the number game. The program asks the user to enter a number between 1 and 100. Next, this number is compared with the secret number, which is defined as a constant. If the user number is different from the secret number, the program must display a hint message indicating if the user value is bigger or smaller than the secret number. If the user number is equal to the secret number, the program prints the number of attempts and a feedback message according to the following:

  • if the user used less than 5 attempts, it prints Well done;
  • if the user used between 5 and 10 attempts, it prints Not bad;
  • if the user needed more than 10 attempts, You need to improve After printing the corresponding message, the program ends.

Exercise 10. Guess a random number

Modify the previous program to generate the secret number in a random way in the range 1- 100 , using for that the built-in MATLAB function described in the previous lab session.

Exercise 11. Guess a random number with limited attempts

Extend the previous program to limit the number of attempts to 5. The only feedback to the user will now be if they won or not (if they were able to guess the number in 5 turns or less).

Exercise 1 2. Even numbers

Write a program that prompts the user for positive integer numbers. If the number has 3 or more digits, the program prints Large even or Large odd number, and reads another value. If the number is positive but has less than 3 digits, the program directly asks for another value. If the number is 0 or negative, the program ends.

Exercise 1 3. Password

Write a program that reads a user name and a password value (as strings) and prints the message Log in successful if name is “root” and password is “1234”. After three unsuccessful attempts, the program prints to the screen No more attempts possible.