Homework Assignments - Week 1 (Intro) : 0. Warm-Up
Homework Assignments - Week 1 (Intro) : 0. Warm-Up
0. Warm-up:
e. Receives a double value (with some fractional part), should return only the integer
part of it (drop the decimals)
Hint: you need to use casting to transform between these types (double -> int)
f. Receives 3 values (x, min, max) and returns true if first one is in interval between the
other 2 values (min <= x <= max), or false otherwise
1. Triangle Checker:
a. Given three values representing the angles (in degrees) of a triangle, print if the
triangle is right-angled or not (check first if the triangle is a valid one - meaning
the sum of all its angles is 180)
Some tests:
10,30,50 => not a valid triangle
60,60,60 => valid triangle, not right angled
30,60,90 => valid triangle, right angled
b. Given three values representing the length of the edges of a triangle, print if the
triangle is right-angled or not (hint: use Pythagoras theorem; should also first
check if the triangle is a valid one - if the sum of any 2 edges is greater than the
3rd)
Some tests:
10,20,40 => not a valid triangle
10,20,20 => valid, not right angled
30,40,50 => valid, right angled
2. Time Converter:
Print the total number of milliseconds contained in H hours, M minutes and S seconds,
where H, M, S are inputs introduced by the user. (check first that each value is in valid
range, for example 0<= S < 60...)
3. Height Converter:
Write a program to:
a. Convert the height of a person from feet and inches (e.g 5 feet 10 inches) to
centimeters (178cm). The printed value should be an integer (no decimal part)
Hint: to convert (truncate) a double value to an integer, you can use the cast
operator: double d = 2.45; int i = (int) d; //i will be 2
b. Convert the height of a person from centimeters to feet and inches. The printed
values should be two integers.
4. Even-Odd Checker:
Write a program that is given a positive number, not bigger than 1000, and checks if the
number is even or not.
a. Prints 0 if the number is even or 1 if the number is odd.
b. Change your program so it prints “even”/”odd” instead of 0/1 (hint: may use the
ternary operator)
5. Ordered checker:
Write a program that accepts three integers from the user and returns true if they are in
ascending order (first number is less than 2nd which is less than 3rd).
6. Digit remover:
Write a program that is given an integer positive number, of exactly 5 digits, and
computes and prints a new number based on first one from which the middle digit was
removed. Example: 12345 => should print: 1245
7. Common digit:
Write a program that accepts 2 integers values as input between 13 and 89 and prints
true if there is a common digit in both numbers.
Example: x=34, y=48 => should print: true
12. Write a program to find the number of integers within the range of two numbers received
as input and that are divisible by another number (also received as input). Example: x=5,
y=20 and p=3, the output should be 5. For x=6, y=22 and p=2, the output should be 9
13. Given the frequency of some photons in hertzs (e.g 4.3MHz = 4300000), print the
corresponding wavelength of the light. If its value is in range of the visible spectrum, also
print the corresponding color (eg “green”, “red”, etc.)