# Turbo Pascal 7.0 Guide

Assignment

To assign a particular value to a variable, use the following syntax,

*variable*:= *expression*;

For example,

`a := 5;`

c := b;

Total := Total + 1;

k := 9 MOD 5;

## Example Program 1

`PROGRAM sumtotal;`

VAR a,b, total: integer;

BEGIN

writeln;

write('Enter first number ');

readln(a);

write('Enter second number ');

readln(b);

total:= a + b;

writeln('The sum of the numbers is', total);

readln;

END.

Type in and run the above program.

## Exercises

1 Change the example program so that it, (a) subtracts the second number from the first, (b) multiplies the numbers together, and (c) divides the first number by the second.

For part (c), you will have to consider the possibility that the result may not be an integer.

2 Change the program so that it performs all of the operations in one program, displaying the results in a useful way.

## Example Program 2

`PROGRAM meal;`

VAR main, drink, total: real;

BEGIN

writeln;

write('Type in the cost of the main course ');

readln(main);

write('Type in the cost of the drink ');

readln(drink);

total:= main + drink;

writeln('The total bill is £', total);

readln;

END.

## Improving The Layout

The example program displayed the output in standard form. All real numbers are output in this form unless you specify a different format.

Change the output line to read,

`writeln('The total bill is £', total:6:2);`

The 6 means that you want the result to be displayed in a field of 6 spaces with 2 numbers after the decimal point.

## Operator Priority

When performing calculations, the computer will follow the **BODMAS **rules of mathematics regarding the priority of operators.

BRACKETS

* / MOD DIV

+ -

Operators with equal priority are calculated from left to right. Therefore,

3 + 5 * 7 = 38

(3 + 5) * 7 = 56

6 * 4 DIV 3 = 8

## Exercises

1. Find the value of the following expressions or explain why it is not a valid expression.

- 9 - 5 - 3
- 2 DIV 3 + 3 / 5
- 9 DIV 2 / 5
- 9 / 2 DIV 5
- 2.0 / 4
- (2 + 3) MOD 2
- 12 + 2 * 3

2. Write a program that uses the operators DIV and MOD to convert a number of seconds to minutes and seconds.

3. Write a program that allows the user to input the radius of a circle and get the area and circumference.

(π = 3.14159, area = πr2, circumference = 2πr)

4. A class has four exams in one term. Write a program that reads in these scores and returns the student's average mark.

5. A temperature in degrees Celsius can be converted to degrees Fahrenheit using the formula F = (9/5) * C + 32. Write a program that allows the user to input a temperature in Celsius and output the equivalent in degrees Fahrenheit.

6. Write a program that converts temperatures from degrees Fahrenheit to degrees Celsius.

7 Freda paid £4.80 and hour. This rate is for the first 35 hours that she works. Any overtime she works is paid at time and a half. Input the number of hours worked and calculate the wage due. Assume that she works more than 35 hours each week.