Hanoi University of Science and Technology
SEEE – Semester 2024.1
Lecturer: Hoàng Đức Chính
EE3491 – Kỹ thuật lập trình
EE3490E – Programming Techniques
Exercises 1
Problem 1:
Write a program to calculate the sum of digits of a three-digit number e.g. 125 is 8
Problem 2:
Write a program to merge three number into one, e.g., a= 1, b= 2, c = 8 are merged to 128
Problem 3:
Write a program to print the reverse of a 3-digit number
Problem 4:
Newton’s law states that the force, 𝐹, between two bodies of masses 𝑀 and 𝑀 is given by:
𝐹=𝐺
𝑀𝑀
𝑑
in which 𝑘 is the gravitational constant and 𝑑 is the distance between the bodies. The value of 𝑘 is
approximately 6.67 × 10 [𝑚 ∙ 𝑘𝑔 ∙ 𝑠 ] . Write a program that prompts the user to input the masses of
the bodies and the distance between the bodies. The program then outputs the force between the bodies.
Problem 5:
If you buy a 40 GB hard drive, then chances are that the actual storage on the hard drive is not 40 GB.
This is due to the fact that, typically, a manufacturer uses 1000 bytes as the value of 1K bytes, 1000 K bytes
as the value of 1 MB, 1 000 MB as the value of 1 GB. Therefore, a 40 GB hard drive contains 40 000 000
000 bytes. However, in computer memory, 1 KB = 1024 bytes, 1MB = 1024 KB and so on. The units from
the smallest to the largest are Byte, Kilobyte (KB), Megabyte (MB), Gigabyte (GB), Terabyte (TB), Petabyte
(PT), Exabyte (EB), Zettabyte (ZB). So the actual storage on a 40 GB hard drive is approximately 37.25 GB.
(You might like to read the fine print next time you buy a hard drive.) Write a program that prompts the user
to enter the size of the hard drive specified by the manufacturer, on the hard drive box, and outputs the actual
storage capacity of the hard drive.
Problem 6:
Write a program to solve the quadratic equation 𝑎𝑥 + 𝑏𝑥 + 𝑐 = 0 with 𝑎, 𝑏, 𝑐 are real numbers
provided by users.
Problem 7:
The short-term, 0-24 hours, parking fee (in USD), F, at an international airport is given by the
following formula:
5,
𝑖𝑓0 ≤ ℎ ≤ 3
𝐹 = 6 × 𝑖𝑛𝑡(ℎ + 1) 𝑖𝑓 3 < ℎ ≤ 9
60,
𝑖𝑓 9 < ℎ ≤ 24
where 𝑖𝑛𝑡(ℎ + 1) is the integer value of ℎ + 1. For example, 𝑖𝑛𝑡(3.2) = 3, 𝑖𝑛𝑡(4.8) = 4. Write a
program that prompts the user to enter the number of hours a car is parked at the airport and outputs the
parking fee.
Problem 8:
Sound travels through air as a result of collisions between the molecules in the air. The temperature
of the air affects the speed of the molecules, which in turn affects the speed of sound. The velocity of sound
in dry air can be approximated by the formula:
𝑣𝑒𝑙𝑜𝑐𝑖𝑡𝑦 ≈ 331.3 + 0.61 × 𝑇𝑐
where 𝑇 is the temperature of the air in degrees Celsius and the velocity is in meters/second.
Write a program that allows the user to input a starting and an ending temperature. Within this
temperature range, the program should output the temperature and the corresponding velocity in 1°
increments. For example, if the user entered 0 as the start temperature and 2 as the end temperature, then the
program should output:
At 0 degrees Celsius the velocity of sound is 331.3 m/s
At 1 degrees Celsius the velocity of sound is 331.9 m/s
At 2 degrees Celsius the velocity of sound is 332.5 m/s
Problem 9:
Write a program to calculate the slope between two points x1, y1 and x2, y2. The points should be
entered as four double values in the order x1, y1, x2, and y2. The formula to calculate the slope, m, between
two points is
𝑚=
(𝑦 − 𝑦 )
𝑥 −𝑥
Output the calculated slope value.
Use this value to output the equation of the line in the form
𝑦 = 𝑚𝑥 + 𝑐
You can calculate the value of c from one of the pair of points entered as input.
Problem 10:
Write an astrology program. The user types in a birthday, and the program responds with the sign and
horoscope for that birthday. The month may be entered as a number from 1 to 12. Then enhance your
program so that if the birthday is only one or two days away from an adjacent sign, the program announces
that the birthday is on a “cusp” and also outputs the horoscope for that nearest adjacent sign. This program
will have a long multiway branch. Make up a horoscope for each sign. Your program should include a loop
that lets the user repeat this calculation until the user says she or he is done.
The horoscope signs and dates are:
Aries
March 21–April 19
Taurus
April 20–May 20
Gemini
May 21–June 21
Cancer
June 22–July 22
Leo
July 23–August 22
Virgo
August 23–September 22
Libra
September 23–October 22
Scorpio
October 23–November 21
Sagittarius
November 22–December 21
Capricorn
December 22–January 19
Aquarius
January 20–February 18
Pisces
February 19–March 20