1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

116
1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone

Transcript of 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

Page 1: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

1

Programming in Pascal

Presented by:-Zonal ICT Section-Kurunegala Education

Zone

Page 2: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

2

Machine Languages

Assembly Languages

Procedure Languages

Object Oriented

Languages

Non-procedural Languages

Visual

Programming

Languages

AI Languages

1st Generation

2nd

Generation

3rd

Generation

4th

Generation

5th

Generation

High Level Languages

Low Level Languages

Easier to program

Generation of Programming Languages

Page 3: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

3

Today’s Content

• Introduction to Programming– Classification, Generations,

compilers/interpreters• Sample Pascal Program• Data types & Operators• Algorithms • Program Structure • Data Input/output

Page 4: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

4

Pascal Background

• Developed by Swiss scientist Nikalus Wirth in 1970.

• Named after the 17th- Century French mathematician Blaise Pascal.

• Developed to teach programming as a systematic and structured activity.

• Easy to learn.

Page 5: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

5

Sample Pascal Program

Program Hello;(* My First Pascal Prg *) Begin Writeln('Hello World');End.

Program Hello;(* My First Pascal Prg *) Begin Writeln('Hello World');End.

BackHello World Hello World

Output Screen

Page 6: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

6

Sample Pascal Program

Program Hello;(* My First Pascal Prg *) Begin Writeln('Hello World');End.

Program Hello;(* My First Pascal Prg *) Begin Writeln('Hello World');End.

Every Pascal program must have a name.

Every Pascal program must have a name.

Page 7: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

7

Sample Pascal Program

Program Hello;(* My First Pascal Prg *) Begin Writeln('Hello World');End.

Program Hello;(* My First Pascal Prg *) Begin Writeln('Hello World');End.

The name of thisprogram is called Hello

The name of thisprogram is called Hello

Page 8: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

8

Sample Pascal Program

Program Hello;(* My First Pascal Prg *)Begin Writeln('Hello World');End.

Program Hello;(* My First Pascal Prg *)Begin Writeln('Hello World');End.

Pascal Command are separated by Semi colons

Pascal Command are separated by Semi colons

Page 9: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

9

Sample Pascal Program

Program Hello;(* My First Pascal Prg *) Begin Writeln('Hello World');End.

Program Hello;(* My First Pascal Prg *) Begin Writeln('Hello World');End.

•Text between (* and *) is taken as aComment. •These are ignored by the compiler.•Comments are used for documentation.

•Text between (* and *) is taken as aComment. •These are ignored by the compiler.•Comments are used for documentation.

Page 10: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

10

Sample Pascal Program

Program Hello;(* My First Pascal Prg *)Begin Writeln('Hello World');End.

Program Hello;(* My First Pascal Prg *)Begin Writeln('Hello World');End.

Instructions must bebetween a begin-end. Instructions must be

between a begin-end.

Page 11: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

11

Sample Pascal Program

Program Hello;(* My First Pascal Prg *)Begin Writeln('Hello World');End.

Program Hello;(* My First Pascal Prg *)Begin Writeln('Hello World');End.

Instructs the computer toprint what is given within

single quotations

Instructs the computer toprint what is given within

single quotations

Page 12: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

12

Compiling and executing • Compiling a Program

– The computer cannot understand the instructions we have given in Pascal.

– We need to convert the instructions given in Pascal into a language the computer can understand (Machine Code)

– This conversion is called compiling

Page 13: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

13

Compiling and executing • Executing a Program

– Once you have compiled and linked a program there will be an executable machine code program saved in the computer.

– You can ask the computer to perform the instructions given by running it from the programming environment (IDE) you are in, or by directly executing the Executable file.

Page 14: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

14

Compiling and executing

• Type in Program in Editor• Compile Program• Link Program• Run Program

Page 15: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

15

Write, Writeln Statements

• Used to display a message on the screen.

• Write statement will display the message on the screen starting from the current cursor location.

• Writeln statement will move the cursor to the next line after displaying the message.

Page 16: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

16

Writeln, Write Example

Writeln('One');Writeln('One');Writeln('Two');Write('Three');Writeln('Four');

Writeln('One');Writeln('One');Writeln('Two');Write('Three');Writeln('Four');

_

_

Output Screen

Page 17: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

17

Writeln, Write Example

Writeln('One');Writeln('Two');Writeln('Two');Write('Three');Writeln('Four');

Writeln('One');Writeln('Two');Writeln('Two');Write('Three');Writeln('Four');

One_

One_

Output Screen

Page 18: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

18

Writeln, Write Example

Writeln('One');Writeln('Two');Write('Three');Write('Three');Writeln('Four');

Writeln('One');Writeln('Two');Write('Three');Write('Three');Writeln('Four');

OneTwo_

OneTwo_

Output Screen

Page 19: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

19

Writeln, Write Example

Writeln('One');Writeln('Two');Write('Three');Writeln('Four');Writeln('Four');

Writeln('One');Writeln('Two');Write('Three');Writeln('Four');Writeln('Four');

OneTwoThree_

OneTwoThree_

Output Screen

Page 20: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

20

Writeln, Write Example

Writeln('One');Writeln('Two');Write('Three');Writeln('Four');

Writeln('One');Writeln('Two');Write('Three');Writeln('Four');

OneTwoThreeFour_

OneTwoThreeFour_

Output Screen

Page 21: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

21

Statements and Expressions

• A Statement is the simplest task you can accomplish in Pascal.

height : integer; Program test;Writeln('Height is', height);Avg := (mrks1+mrks2+mrks3)/3;

height : integer; Program test;Writeln('Height is', height);Avg := (mrks1+mrks2+mrks3)/3;

You need to put a semi colon ; to separateStatements.

You need to put a semi colon ; to separateStatements.

Page 22: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

22

Sample Pascal Program

Program Hello;(* My First Pascal Prg *)Begin Writeln('Hello World'); Writeln('second line'); Writeln('Last Line')End.

Program Hello;(* My First Pascal Prg *)Begin Writeln('Hello World'); Writeln('second line'); Writeln('Last Line')End.

Pascal statements are Separated by semi colons

Pascal statements are Separated by semi colons

Page 23: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

23

Sample Pascal Program

Program Hello;(* My First Pascal Prg *)Begin Writeln('Hello World'); Writeln('second line'); Writeln('Last Line')End.

Program Hello;(* My First Pascal Prg *)Begin Writeln('Hello World'); Writeln('second line'); Writeln('Last Line')End.•Semi colons are used to separate statements.

•The semi colon for the last statement is not required.

•Semi colons are used to separate statements. •The semi colon for the last statement is not required.

Begin stmt1 ;; stmt2 ;; stmt3End.

Begin stmt1 ;; stmt2 ;; stmt3End.

Page 24: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

24

Sample Pascal Program

Program Hello;(* My First Pascal Prg *) Begin Writeln('Hello World');Writeln('second line'); Writeln('Last Line') End.

Program Hello;(* My First Pascal Prg *) Begin Writeln('Hello World');Writeln('second line'); Writeln('Last Line') End.

• It is possible write a program as shown above. • Having a program Indented makes it more easy to understand.

• It is possible write a program as shown above. • Having a program Indented makes it more easy to understand.

Page 25: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

25

A Program to Add two numbers

• Lets consider a program that is used to calculate the addition of two numbers.

• We will need to make use of variables in the Pascal program to store data.

Page 26: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

26

Program that adds two numbers

Program Addition;Var number1, number2 : integer; sum : integer;Begin number1 := 20; number2 := 56; sum := number1 + number2; Writeln('Sum is ',sum);end.

Program Addition;Var number1, number2 : integer; sum : integer;Begin number1 := 20; number2 := 56; sum := number1 + number2; Writeln('Sum is ',sum);end.

Sum is 76_

Sum is 76_

Output Screen

Number1, Number2 andSum are variables.

Number1, Number2 andSum are variables.

Page 27: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

27

Mr Kitsiri Almeda Mr Rohan

Perera

Mr Nihal Peries

No, 128 Railway Road, Nugegoda

No, 130 Railway Road, Nugegoda

No, 132 Railway Road, Nugegoda

Analogy of Variables, Memory

Page 28: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

28

Memory Locations

Memory

Addresses

Data values stored in memory locations

Animal

4504450545064507

4508

4509

4510451145124513

4514

344513

CAT

12456

AdmissionNo Marks1

VARIABLES

Page 29: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

29

Variables

• In a Program we use Variables to store data.• Variables are locations in memory which are

used to store data

Page 30: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

30

Type of Variable

• You need to specify what type of data is to be stored. e.g. integer, real

TYPE

Page 31: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

31

Variable Name

• Each Variable must be identified by a name. e.g. Age, quantity

Page 32: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

32

Value

• At a given time one value can be stored under the variable

Page 33: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

33

Data Types in Pascal

• Integers e.g. 78, -9000, 4500• Real e.g. 5.67, 900.23, 5e6• Char e.g. 'A', 'C', '9'• Boolean e.g. True, False• String e.g. 'computer', 'Nimal'

Page 34: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

34

Declaring Variables

var Count, I, R : Integer; Salary : Real; isAsleep : Boolean;

var Count, I, R : Integer; Salary : Real; isAsleep : Boolean;

Variables in Pascal need to be declared after the Var declarationVariables in Pascal need to be declared after the Var declaration

Page 35: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

35

Valid Variable Names

A Variable Name should start with an letter or _ symbol.The rest can consist of letters, digits and the _ symbol.

A Variable Name should start with an letter or _ symbol.The rest can consist of letters, digits and the _ symbol.

Page 36: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

36

Age : Integer;my_char : char;_no : integer;No2 : real;

Age : Integer;my_char : char;_no : integer;No2 : real;

A Variable Name should start with an Alphabetical letter or _ symbolYou can also use digits.But you cannot use symbols like @, #, etc

A Variable Name should start with an Alphabetical letter or _ symbolYou can also use digits.But you cannot use symbols like @, #, etc

Valid Variable Names

Page 37: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

37

Variable Names

my age : Integer;@money : Real;6my_char : Char;no* : Integer;

my age : Integer;@money : Real;6my_char : Char;no* : Integer;

The above names are incorrect.You cannot have spaces and otherspecial symbols.

The above names are incorrect.You cannot have spaces and otherspecial symbols.

Page 38: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

38

Arithmetic Operators

Addition +Substraction -Division /Multiplication *Modulus modInteger Division div

Page 39: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

39

Assignment Operator

Var age : integer; choice : char;Begin age := 17; choice := 'c';

Var age : integer; choice : char;Begin age := 17; choice := 'c';

Variables age, choice are assigned theValues 17 and 'c' respectively.Variables age, choice are assigned theValues 17 and 'c' respectively.

Page 40: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

40

Numeric CalculationsProgram Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a, b, c); b := a * 2; Writeln(a, b, c); a := c / 5; Writeln(a, b, c)End.

Program Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a, b, c); b := a * 2; Writeln(a, b, c); a := c / 5; Writeln(a, b, c)End.

Page 41: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

41

Numeric CalculationsProgram Sample;Var a, b, c : Integer;begin a := 100;a := 100; b := 50;b := 50; c := 75;c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a, b, c); b := a * 2; Writeln(a, b, c); a := c / 5; Writeln(a, b, c)End.

Program Sample;Var a, b, c : Integer;begin a := 100;a := 100; b := 50;b := 50; c := 75;c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a, b, c); b := a * 2; Writeln(a, b, c); a := c / 5; Writeln(a, b, c)End.

Variables a, b, c are assigned the values100, 50, 75 respectively.Variables a, b, c are assigned the values100, 50, 75 respectively.

100

50

75

aa

bb

cc

Page 42: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

42

Numeric CalculationsProgram Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c;a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a, b, c); b := a * 2; Writeln(a, b, c); a := c / 5; Writeln(a, b, c)End.

Program Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c;a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a, b, c); b := a * 2; Writeln(a, b, c); a := c / 5; Writeln(a, b, c)End.

The expression on the right handSide is simplified and assigned toThe left side variable

The expression on the right handSide is simplified and assigned toThe left side variable

100

50

75

aa

bb

cc

Page 43: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

43

Numeric CalculationsProgram Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c;a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a, b, c); b := a * 2; Writeln(a, b, c); a := c / 5; Writeln(a, b, c)End.

Program Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c;a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a, b, c); b := a * 2; Writeln(a, b, c); a := c / 5; Writeln(a, b, c)End.

100

50

75

aa

bb

cc

a := 50 + 75; The value 125 will be stored in Variable aa := 50 + 75; The value 125 will be stored in Variable a

Page 44: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

44

Numeric CalculationsProgram Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c);Writeln(a, b, c); c := a - 20;Writeln(a, b, c); b := a * 2; Writeln(a, b, c); a := c / 5; Writeln(a, b, c)End.

Program Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c);Writeln(a, b, c); c := a - 20;Writeln(a, b, c); b := a * 2; Writeln(a, b, c); a := c / 5; Writeln(a, b, c)End.

The Writeln statement will print the Values of a, b, cThe Writeln statement will print the Values of a, b, c

125

50

75

aa

bb

cc

1255075_

1255075_

Output Screen

Page 45: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

45

Numeric CalculationsProgram Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;c := a - 20;Writeln(a,'', c); b := a * 2; Writeln(a,'', b); a := c / 5; Writeln(a,'', c)End.

Program Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;c := a - 20;Writeln(a,'', c); b := a * 2; Writeln(a,'', b); a := c / 5; Writeln(a,'', c)End.

C := 125 - 20 The value 105 will be stored in Variable c

C := 125 - 20 The value 105 will be stored in Variable c

125

50

75

aa

bb

cc

Page 46: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

46

Numeric CalculationsProgram Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a,'', c);Writeln(a,'', c); b := a * 2; Writeln(a,'', b); a := c / 5; Writeln(a,'', c)End.

Program Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a,'', c);Writeln(a,'', c); b := a * 2; Writeln(a,'', b); a := c / 5; Writeln(a,'', c)End.

125

50

105

aa

bb

cc

1255075125 105_

1255075125 105_

Output Screen

Page 47: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

47

Numeric CalculationsProgram Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a,'', c); b := a * 2;b := a * 2; Writeln(a,'', b); a := c / 5; Writeln(a,'', c)End.

Program Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a,'', c); b := a * 2;b := a * 2; Writeln(a,'', b); a := c / 5; Writeln(a,'', c)End.

125

50

105

aa

bb

cc

B := 125*2;250 will be stored in Variable B.

B := 125*2;250 will be stored in Variable B.

Page 48: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

48

Numeric CalculationsProgram Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a,'', c); b := a * 2; Writeln(a,'', b);Writeln(a,'', b); a := c / 5; Writeln(a,'', c)End.

Program Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a,'', c); b := a * 2; Writeln(a,'', b);Writeln(a,'', b); a := c / 5; Writeln(a,'', c)End.

125

250

105

aa

bb

cc

1255075125 105125 250_

1255075125 105125 250_

Output Screen

Page 49: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

49

Numeric CalculationsProgram Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a,'', c); b := a * 2; Writeln(a,'', b); a := c / 5;a := c / 5; Writeln(a,'', c)End.

Program Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a,'', c); b := a * 2; Writeln(a,'', b); a := c / 5;a := c / 5; Writeln(a,'', c)End.

125

250

105

aa

bb

cc

a := 105/5;21 will be stored in Variable a

a := 105/5;21 will be stored in Variable a

Page 50: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

50

Numeric CalculationsProgram Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a,'', c); b := a * 2; Writeln(a,'', b); a := c / 5; Writeln(a,'', c)Writeln(a,'', c)End.

Program Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a,'', c); b := a * 2; Writeln(a,'', b); a := c / 5; Writeln(a,'', c)Writeln(a,'', c)End.

21

250

105

aa

bb

cc

1255075125 105125 25021 105_

1255075125 105125 25021 105_

Output Screen

Page 51: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

51

Integer OperatorsProgram Sample2;Var a, b, c : Integer;Begin a := 100; b := a div 3; c := a mod 3; Writeln(a, b, c)End.

Program Sample2;Var a, b, c : Integer;Begin a := 100; b := a div 3; c := a mod 3; Writeln(a, b, c)End.

100331_

100331_

Output Screen

Page 52: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

52

Integer OperatorsProgram Sample2;Var a, b, c : Integer;Begin a := 100; b := a div 3;b := a div 3; c := a mod 3; Writeln(a, b, c)End.

Program Sample2;Var a, b, c : Integer;Begin a := 100; b := a div 3;b := a div 3; c := a mod 3; Writeln(a, b, c)End.

Integer Division 100 div 3 is 33Integer Division 100 div 3 is 33

Page 53: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

53

Integer OperatorsProgram Sample2;Var a, b, c : Integer;Begin a := 100; b := a div 3; c := a mod 3;c := a mod 3; Writeln(a, b, c)End.

Program Sample2;Var a, b, c : Integer;Begin a := 100; b := a div 3; c := a mod 3;c := a mod 3; Writeln(a, b, c)End.

Integer Division 100 mod 3 is 1Integer Division 100 mod 3 is 1

Page 54: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

54

Char, Boolean typesProgram Sample3;Var status : boolean; grade : char;Begin status := true; grade := 'A'; Writeln('Grade : ',grade); Writeln('Status : ',status)End.

Program Sample3;Var status : boolean; grade : char;Begin status := true; grade := 'A'; Writeln('Grade : ',grade); Writeln('Status : ',status)End.

Grade : AStatus : true_

Grade : AStatus : true_

Output Screen

Page 55: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

55

A Program to find the avg

• Lets develop a program to calculate the average marks a student gets in an AL term test.

• The marks of three subjects will be entered into the program. Your program should calculate and print the average marks of the three subjects.

Page 56: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

56

Development and Design

• Analyze the problem• Develop a solution• Code the solution• Test and Debug

Page 57: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

57

Programming Development Cycle

Start

ProblemDefinition

AlgorithmDesign

DesktopTesting

Coding

Testing

Completed Program

Problem Solving Phase

Implementation Phase

Pascal

Page 58: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

58

Analyze the Problem

• Determine the information the computer is to produce, i.e. the output.

• Determine the information required to produce the output, i.e. the input data.

• Determine if any special cases exist, and how they are to be handled.

Page 59: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

59

Develop the solution

• Determine an algorithm which will transform the input to the output

• If the problem is complex, divide the problem into subtasks, where the completion of all the subtasks in some specified order will produce the desired output.

Page 60: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

60

Design Components

• Data Structures Description of data, its name, type,

and range of values.• Algorithms

An algorithm is a precise, unambiguous, step-by-step description of how to do a task in a finite amount of time.

Page 61: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

61

Code the Solution

• Draw a flowchart for the algorithm• Convert your flowchart to a Pascal

program.• Look up the syntax of any Pascal

statements you are unsure of as you write them.

• Compile the program, and fix any syntax errors.

Page 62: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

62

Test and Correct the Program

• Determine a set of test data, and hand calculate the expected answer.

• Run the program, and compare the computer's answer to the expected answer.

• If they do not agree, examine the program; determine where the error is; and fix the error.

Page 63: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

63

Applying the Design Process to an Example

• The marks of three subjects will be entered to the program. Your program should calculate and print the average of the three subjects.

Page 64: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

64

Problem Definition

• Understand the problem

• What are the Inputs ?

• What are the Outputs ?

• Calculations ?

• Background

3 Marks

Avg

Calculate Avg

Additional Data if any

We need to calculate the sum

Page 65: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

65

Algorithm Design

Flowchart

Start

Input Marks1

Calculate Sum, Avg

Stop

Input Marks2

Input Marks3

Print Avg

Page 66: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

66

Test Data

• Marks1 = 60, Marks2 = 70, Marks3 = 50• Sum = 180, Avg = 60

• Marks1 = 90, Marks2 = 60, Marks3 = 60• Sum = 210, Avg = 70

• Marks1 = 50, Marks2 = 0, Marks3 = 25• Sum = 75, Avg = 25

Page 67: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

67

Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;

Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;

Start

Input Marks1

Calculate Sum, Avg

Stop

Input Marks2

Input Marks3

Print Avg

Page 68: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

68

Start

Input Marks1

Calculate Sum, Avg

Stop

Input Marks2

Input Marks3

Print Avg

Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : ');Write ('Marks 1 : '); Readln(Marks1);Readln(Marks1);

Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : ');Write ('Marks 1 : '); Readln(Marks1);Readln(Marks1);

Readln() statement will allowthe user to type a value fromthe keyboard. The value willbe stored under variable Marks1

Readln() statement will allowthe user to type a value fromthe keyboard. The value willbe stored under variable Marks1

Page 69: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

69

Start

Input Marks1

Calculate Sum, Avg

Stop

Input Marks2

Input Marks3

Print Avg

Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : ');Write ('Marks 3 : '); Readln(Marks3);Readln(Marks3);

Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : ');Write ('Marks 3 : '); Readln(Marks3);Readln(Marks3);

Page 70: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

70

Start

Input Marks1

Calculate Sum, Avg

Stop

Input Marks2

Input Marks3

Print Avg

Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2Sum := Marks1+Marks2

+Marks3;+Marks3; Avg := Sum/3;Avg := Sum/3;

Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2Sum := Marks1+Marks2

+Marks3;+Marks3; Avg := Sum/3;Avg := Sum/3;

Page 71: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

71

Start

Input Marks1

Calculate Sum, Avg

Stop

Input Marks2

Input Marks3

Print Avg

Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2

+Marks3; Avg := Sum/3; Writeln('Avg :',Writeln('Avg :', Avg);Avg);

Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2

+Marks3; Avg := Sum/3; Writeln('Avg :',Writeln('Avg :', Avg);Avg);

Page 72: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

72

Start

Input Marks1

Calculate Sum, Avg

Stop

Input Marks2

Input Marks3

Print Avg

Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2

+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.End.

Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2

+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.End.

Page 73: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

73

Program that calculates avgProgram Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : ');Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.

Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : ');Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.

Marks 1 : _

Marks 1 : _

Output Screen

Page 74: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

74

Program that calculates avgProgram Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1);Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.

Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1);Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.

Marks 1 : 60_

Marks 1 : 60_

Output Screen

Page 75: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

75

Program that calculates avgProgram Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : ');Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.

Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : ');Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.

Marks 1 : 60Marks 2 : _

Marks 1 : 60Marks 2 : _

Output Screen

Page 76: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

76

Program that calculates avgProgram Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2);Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.

Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2);Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.

Marks 1 : 60Marks 2 : 70_

Marks 1 : 60Marks 2 : 70_

Output Screen

Page 77: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

77

Program that calculates avgProgram Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : ');Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.

Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : ');Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.

Marks 1 : 60Marks 2 : 70Marks 3 : _

Marks 1 : 60Marks 2 : 70Marks 3 : _

Output Screen

Page 78: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

78

Program that calculates avgProgram Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3);Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.

Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3);Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.

Marks 1 : 60Marks 2 : 70Marks 3 : 50_

Marks 1 : 60Marks 2 : 70Marks 3 : 50_

Output Screen

Page 79: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

79

Program that calculates avgProgram Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3;Sum := Marks1+Marks2+Marks3; Avg := Sum/3;Avg := Sum/3; Writeln('Avg :', Avg);End.

Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3;Sum := Marks1+Marks2+Marks3; Avg := Sum/3;Avg := Sum/3; Writeln('Avg :', Avg);End.

Marks 1 : 60Marks 2 : 70Marks 3 : 50_

Marks 1 : 60Marks 2 : 70Marks 3 : 50_

Output Screen

Sum and Avg will be calculatedSum := 60+70+50=180Avg := 180/3 = 60

Sum and Avg will be calculatedSum := 60+70+50=180Avg := 180/3 = 60

Page 80: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

80

Program that calculates avgProgram Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);Writeln('Avg :', Avg);End.

Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);Writeln('Avg :', Avg);End.

Marks 1 : 60Marks 2 : 70Marks 3 : 50Avg : 60.0

Marks 1 : 60Marks 2 : 70Marks 3 : 50Avg : 60.0

Output Screen

Page 81: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

81

Essay type Model Questions

(i) Write a program to print the following words on the console screen.

• Hello. How are you?• I'm just fine.

Page 82: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

82

Sample Answer – (i)

Program ModelAns1;Begin Writeln ('Hello. How are you ?'); Writeln ('I”m just fine.') End.

Program ModelAns1;Begin Writeln ('Hello. How are you ?'); Writeln ('I”m just fine.') End.

Page 83: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

83

Essay type Model Questions

(ii) The following program contains a few errors. Identify each error (there areseven), and show the correct version on the right.

Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : = numberi * 4;writeln('Help)end

Page 84: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

84

Sample Answer – (ii)Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : = numberinumberi * 4;writeln('Help)end

Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : = numberinumberi * 4;writeln('Help)end

ErrorError

Page 85: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

85

Sample Answer – (ii)Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : = numberi * 4;writeln('Help)end

Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : = numberi * 4;writeln('Help)end

No Semi ColonsNo Semi Colons

Page 86: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

86

Sample Answer – (ii)Progam TEST (output)Var number1, number2;; integer;begin;number1 = 24;number2 : = numberi * 4;writeln('Help)end

Progam TEST (output)Var number1, number2;; integer;begin;number1 = 24;number2 : = numberi * 4;writeln('Help)end

You need to use colon. ::You need to use colon. ::

Page 87: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

87

Sample Answer – (ii)Progam TEST (output)Var number1, number2; integer;begin;;number1 = 24;number2 : = numberi * 4;writeln('Help)end

Progam TEST (output)Var number1, number2; integer;begin;;number1 = 24;number2 : = numberi * 4;writeln('Help)end

You should not use a semi colonafter beginYou should not use a semi colonafter begin

Page 88: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

88

Sample Answer – (ii)Progam TEST (output)Var number1, number2; integer;begin;number1 == 24;number2 : = numberi * 4;writeln('Help)end

Progam TEST (output)Var number1, number2; integer;begin;number1 == 24;number2 : = numberi * 4;writeln('Help)end

The assignment operator shouldBe used here.The assignment operator shouldBe used here.

Page 89: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

89

Sample Answer – (ii)Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : =: = numberi * 4;writeln('Help)end

Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : =: = numberi * 4;writeln('Help)end

You cannot separate an operatorby a space.You cannot separate an operatorby a space.

Page 90: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

90

Sample Answer – (ii)Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : = numberinumberi * 4;writeln('Help)end

Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : = numberinumberi * 4;writeln('Help)end

There is no variable calledNumberi it should be number1There is no variable calledNumberi it should be number1

Page 91: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

91

Sample Answer – (ii)Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : = numberi * 4;writeln('Help'Help)end

Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : = numberi * 4;writeln('Help'Help)end

There should be an end singleQuotation.There should be an end singleQuotation.

Page 92: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

92

Sample Answer – (ii)Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : = numberi * 4;writeln('Help)end

Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : = numberi * 4;writeln('Help)end

Should use a full stop to end programShould use a full stop to end program

Page 93: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

93

Essay type Model Questions

• (iii) Each of the following expressions is wrong. Rewrite each using correct Pascal.

Firstletter : = A;StartCount := Initial := 0;Taxrate := 5%;Total := 5 plus 7;Efficiency := .35;

Page 94: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

94

Sample Answer – (ii)Firstletter : = A;Firstletter : = A;StartCount := Initial := 0;Taxrate := 5%;Total := 5 plus 7;Efficiency := .35;

Firstletter : = A;Firstletter : = A;StartCount := Initial := 0;Taxrate := 5%;Total := 5 plus 7;Efficiency := .35;

Firstletter := 'A';Firstletter := 'A';

Page 95: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

95

Sample Answer – (ii)Firstletter : = A;

StartCount := Initial := 0;StartCount := Initial := 0;Taxrate := 5%;Total := 5 plus 7;Efficiency := .35;

Firstletter : = A;

StartCount := Initial := 0;StartCount := Initial := 0;Taxrate := 5%;Total := 5 plus 7;Efficiency := .35;

Initial := 0;StartCount := 0;

Initial := 0;StartCount := 0;

Page 96: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

96

Sample Answer – (ii)Firstletter : = A;StartCount := Initial := 0;

Taxrate := 5%;Taxrate := 5%;Total := 5 plus 7;Efficiency := .35;

Firstletter : = A;StartCount := Initial := 0;

Taxrate := 5%;Taxrate := 5%;Total := 5 plus 7;Efficiency := .35;

Taxrate := 0.05;Taxrate := 0.05;

Page 97: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

97

Sample Answer – (ii)Firstletter : = A;StartCount := Initial := 0;Taxrate := 5%;

Total := 5 plus 7;Total := 5 plus 7;Efficiency := .35;

Firstletter : = A;StartCount := Initial := 0;Taxrate := 5%;

Total := 5 plus 7;Total := 5 plus 7;Efficiency := .35;

Total := 5 + 7;Total := 5 + 7;

Page 98: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

98

Sample Answer – (ii)Firstletter : = A;StartCount := Initial := 0;Taxrate := 5%;Total := 5 plus 7;

Efficiency := .35;Efficiency := .35;

Firstletter : = A;StartCount := Initial := 0;Taxrate := 5%;Total := 5 plus 7;

Efficiency := .35;Efficiency := .35;

Efficiency := 0.35;Efficiency := 0.35;

Page 99: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

99

Essay type Model Questions• (iv) What is displayed by the following

program?program EXERCISEl (output) ;var a, b : integer;c : real ;begina := 1; b := 5; c := 1.20;writeln ('A = ', a + 3) ;writeln ('B = ', b - 2) ;Writeln ('C =', c / 2)end.

Page 100: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

100

Sample Answer – (ii)

program EXERCISEl (output) ;var a, b : integer;c : real ;begina := 1; b := 5; c := 1.20;writeln ('A = ', a + 3) ;writeln ('B = ', b - 2) ;Writeln ('C =', c / 2)end.

program EXERCISEl (output) ;var a, b : integer;c : real ;begina := 1; b := 5; c := 1.20;writeln ('A = ', a + 3) ;writeln ('B = ', b - 2) ;Writeln ('C =', c / 2)end.

A = 4 B = 3C = 0.6

A = 4 B = 3C = 0.6

Output Screen

Page 101: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

101

Temperature Conversion

•Write a program to input a temperature in Fahrenheit. Convert and Print this temperature in Celsius.

•Celsius = 5 x (Fahrenheit-32)/9

Page 102: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

102

Problem Definition

• Understand the problem

• What are the Inputs ?

• What are the Outputs ?

• Calculations ?

• Background

Temperature in Fahrenheit

Temperature in Celsius

Calculate Temperature in Celsius

Page 103: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

103

Algorithm Design

Flowchart

Start

Input Fahrenheit

Calculate Celsius

Stop

PrintCelsius

Page 104: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

104

Test Data

• Fahrenheit = 212• Celsius = 100

• Fahrenheit = 32• Celsius = 0

• Fahrenheit = 98.7• Celsius = 37.056

Page 105: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

105

Program Temperature;var Fahrenheit, Celsius

: Real;begin

end.

Program Temperature;var Fahrenheit, Celsius

: Real;begin

end.

Start

Input Fahrenheit

Calculate Celsius

Stop

PrintCelsius

Write('Fahrenheit : '); Readln(Fahrenheit); Celsius := 5 * (Fahrenheit-32)/9; Writeln('Celsius : ', Celsius:7:2);

Page 106: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

106

Temperature ConversionProgram Temperature;var Fahrenheit, Celsius : Real;begin Write('Fahrenheit : '); Readln(Fahrenheit); Celcius := 5 * (Fahrenheit-32)/9; Writeln('Celsius : ', Celsius:7:2Celsius:7:2);end.

Program Temperature;var Fahrenheit, Celsius : Real;begin Write('Fahrenheit : '); Readln(Fahrenheit); Celcius := 5 * (Fahrenheit-32)/9; Writeln('Celsius : ', Celsius:7:2Celsius:7:2);end.

Print Celsius to a width of 7 digitsAnd 2 decimal places

Print Celsius to a width of 7 digitsAnd 2 decimal places

Fahrenheit : 212Celsius : _100.00_

Fahrenheit : 212Celsius : _100.00_

Output Screen

Page 107: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

107

User Defined Types

• Enumerated Types– e.g Type Days = (Mon, Tue, Wed, Thu, Fri,

Sat, Sun);– Increases the Readability of the

program.– You cannot input or print

enumerated types

Page 108: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

108

User Defined Types

• Sub Ranges– e.g Type MarksType = 0..100;Var Marks : MarksType; Age : 5..19; letter : 'a' .. 'z';– Specify a lower limit and a upper

limit for ordinal type data.

Page 109: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

109

User Defined Types Sample PrgProgram UserDefined;type Days = (Mon, Tue, Wed, Thu, Fri, Sat, Sun); MarksType = 0..100;Var today : Days; Marks : MarksType; Age : 5..19; begin today := Tue; Marks := 78; Age := 17;end.

Program UserDefined;type Days = (Mon, Tue, Wed, Thu, Fri, Sat, Sun); MarksType = 0..100;Var today : Days; Marks : MarksType; Age : 5..19; begin today := Tue; Marks := 78; Age := 17;end.

Page 110: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

110

Constants

• Constants are data that do not change in the program.

• e.g.pi = 3.1416

rate = 100Constants are defined under the heading

const.

Page 111: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

111

Constants Sample ProgramProgram AreaOfCircle;

constconst

pi = 3.1416;pi = 3.1416;Var radius : integer; area : real;begin Write(‘Enter Radius : ‘); Readln(Radius); area := pi*Radius*Radius; Write(‘Area is : ‘, area:7:2);end.

Program AreaOfCircle;

constconst

pi = 3.1416;pi = 3.1416;Var radius : integer; area : real;begin Write(‘Enter Radius : ‘); Readln(Radius); area := pi*Radius*Radius; Write(‘Area is : ‘, area:7:2);end.

Defines a constant called pi withthe value 3.1416

Defines a constant called pi withthe value 3.1416

Page 112: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

112

Constants Sample ProgramProgram AreaOfCircle;const pi = 3.1416;Var radius : integer; area : real;begin Write(‘Enter Radius : ‘);Write(‘Enter Radius : ‘); Readln(Radius); area := pi*Radius*Radius; Write(‘Area is : ‘, area:7:2);end.

Program AreaOfCircle;const pi = 3.1416;Var radius : integer; area : real;begin Write(‘Enter Radius : ‘);Write(‘Enter Radius : ‘); Readln(Radius); area := pi*Radius*Radius; Write(‘Area is : ‘, area:7:2);end.

Enter Radius : _

Enter Radius : _

Output Screen

radiusradius

areaarea

Page 113: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

113

Constants Sample ProgramProgram AreaOfCircle;const pi = 3.1416;Var radius : integer; area : real;begin Write(‘Enter Radius : ‘); Readln(Radius);Readln(Radius); area := pi*Radius*Radius; Write(‘Area is : ‘, area:7:2);end.

Program AreaOfCircle;const pi = 3.1416;Var radius : integer; area : real;begin Write(‘Enter Radius : ‘); Readln(Radius);Readln(Radius); area := pi*Radius*Radius; Write(‘Area is : ‘, area:7:2);end.

Enter Radius : 1010_

Enter Radius : 1010_

Output Screen

10radiusradius

areaarea

Page 114: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

114

Constants Sample ProgramProgram AreaOfCircle;const pi = 3.1416;Var radius : integer; area : real;begin Write(‘Enter Radius : ‘); Readln(Radius); area := pi*Radius*Radius;area := pi*Radius*Radius; Write(‘Area is : ‘, area:7:2);end.

Program AreaOfCircle;const pi = 3.1416;Var radius : integer; area : real;begin Write(‘Enter Radius : ‘); Readln(Radius); area := pi*Radius*Radius;area := pi*Radius*Radius; Write(‘Area is : ‘, area:7:2);end.

10

314.16

radiusradius

areaarea

Enter Radius : 1010_

Enter Radius : 1010_

Output Screen

Page 115: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

115

Constants Sample ProgramProgram AreaOfCircle;const pi = 3.1416;Var radius : integer; area : real;begin Write(‘Enter Radius : ‘); Readln(Radius); area := pi*Radius*Radius; Write(‘Area is : ‘, area:7:2);Write(‘Area is : ‘, area:7:2);end.

Program AreaOfCircle;const pi = 3.1416;Var radius : integer; area : real;begin Write(‘Enter Radius : ‘); Readln(Radius); area := pi*Radius*Radius; Write(‘Area is : ‘, area:7:2);Write(‘Area is : ‘, area:7:2);end.

10

314.16

radiusradius

areaarea

Enter Radius : 10Area is : _314.16_

Enter Radius : 10Area is : _314.16_

Output Screen

Page 116: 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

End

116