Next: Further Assignment Statements & Up: The Assignment statement Previous: Review questions

Exercises

  1. Write a C++ program which reads values for two floats and outputs their sum, product and quotient. Include a sensible input prompt and informative output.

  2. Write a program to evaluate the fuel consumption of a car. The mileage at the start and end of the journey should be read, and also the fuel level in the tank at the start and end of the journey. Calculate fuel used, miles travelled, and hence the overall fuel consumption in miles travelled per gallon of fuel.

  3. In many countries using the metric system, car fuel consumptions are measured in litres of fuel required to travel 100 kilometres. Modify your solution to question 2 so that the output now specifies the distance travelled in kilometres as well as in miles, and the fuel consumed in litres as well as in gallons, and the consumption in litres per 100 kilometres as well as in miles per gallon. Use const for the conversion factors between miles and kilometres, and gallons and litres.

  4. Write a program to convert currency from pounds sterling to deutsch marks. Read the quantity of money in pounds and pence, and output the resulting foreign currency in marks and pfennigs. (There are 100 pfennigs in a mark). Use a const to represent the conversion rate, which is 2.31DM to £1 at the time of writing. Be sure to print suitable headings and or labels for the values to be output.

  5. Modify your answer to question 4 so that a commission of £2 is charged.

  6. A customer's gas bill is calculated by adding a standard charge of 9.02 pence per day to the charge for gas, calculated as 1.433 pence per cubic metre. The whole bill is then liable for VAT at 8%. Write a program that reads the number of days, and the initial and final meter readings, and calculates and prints the bill. Use const for the various rates of charging.

  7. If three integers $a$, $b$ and $c$ are such that $\displaystyle{a^2+b^2=c^2}$ then they constitute a Pythagorean triple. There is an infinite number of such triples. One way of generating them is as follows:
    Consider two integers $m$, and $n$, such that $\displaystyle{m>n}$ then the three numbers $m^2-n^2$, $2mn$ and $m^2+n^2$ are a Pythagorean triple. Write a C++ program that reads values for $m$ and $n$ and prints the values of the corresponding Pythagorean triple.



Next: Further Assignment Statements & Up: The Assignment statement Previous: Review questions