0% found this document useful (0 votes)
4 views21 pages

Paper 2 Programing Notes

The document provides a comprehensive overview of programming concepts including variables, data types, operators, selection statements, loops, and arrays. It includes numerous tasks and example code snippets demonstrating how to implement these concepts in programming, such as calculating grades, determining even/odd numbers, and handling user input. The document serves as a practical guide for writing basic programs using structured programming techniques.

Uploaded by

Mohammad Mahin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views21 pages

Paper 2 Programing Notes

The document provides a comprehensive overview of programming concepts including variables, data types, operators, selection statements, loops, and arrays. It includes numerous tasks and example code snippets demonstrating how to implement these concepts in programming, such as calculating grades, determining even/odd numbers, and handling user input. The document serves as a practical guide for writing basic programs using structured programming techniques.

Uploaded by

Mohammad Mahin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Tuesday, 12 September 2023 2:56 pm

Variable: It's like a container to store a value


Assignment:

DECLARE Result : INTEGER


DECLARE Number1 : INTEGER
DECLARE Number2 : INTEGER
Number1 <- 3
Number2 <- 5
Result <- Number1 + Number2
OUTPUT Result

Data Type:
INTEGER 4, 52, 6, 20
REAL 4.2, 4.7, 5.8
STRING "abc", "a5", "52"
CHAR 'A' , '9'
BOOLEAN TRUE/FALSE
DATE
CURRENCY

Operators:
- Arithmetic
+
-
/
*
MOD
DIV

- Relational:
> Greater Than
< Less Than
>= Greater than or equal to
<= Less than or equal to
= Equal to
<> Not equal to

- Boolean:
AND
OR
NOT

Selection Statement:

DECLARE marks : INTEGER


INPUT marks
IF marks >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF

New Section 1 Page 1


Task) Write a program that takes a number as input and output whether the number is Positive and
Negative.

DECLARE Number : INTEGER

INPUT Number

IF Number >= 0 THEN


OUTPUT "Positive"
ELSE
OUTPUT "Negative"
ENDIF

Task) Write a program that takes an integer as input and output whether the integer is even or odd.

DECLARE Number : INTEGER

INPUT Number

IF NumberMOD2 = 0 THEN
OUTPUT "Even"
ELSE
OUTPUT "Odd"
ENDIF

Task) Write a program that takes Quantity as input. If the Quantity is greater
than 70 then in prints "Ok", otherwise it prints "Low".

DECLARE Quantity : INTEGER


INPUT Quantity
IF Quantity > 70 THEN
OUTPUT "Ok"
ELSE
OUTPUT "Low"
ENDIF
New Section 1 Page 2
ENDIF

DECLARE marks : INTEGER

INPUT marks

IF marks >= 90 THEN


OUTPUT "A*"
ELSEIF marks >= 80 THEN
OUTPUT "A"
ELSEIF marks >= 70 THEN
OUTPUT "B"
ELSE
OUTPUT "U"
ENDIF

DECLARE Number : INTEGER


INPUT Number
IF NOT(Number < 1 OR Number > 10) THEN
OUTPUT "Between 1 and 10 (both inclusive)"
ELSE
OUTPUT "Not between 1 and 10"
ENDIF

IF Number >= 1 AND Number <= 10 THEN

Task) Write a program that takes Quantity as input. If the Quantity is greater
than or equal to 80 then it prints "High", if the Quantity is between 50 and
79 (both inclusive) it prints "Ok", otherwise it prints "Low". The maximum
Quantity can be 200 and should not be less than Zero.

DECLARE Quantity : INTEGER

INPUT Quantity

IF Quantity >= 80 AND Quantity <= 200 THEN


OUTPUT "High"
New Section 1 Page 3
OUTPUT "High"
ELSEIF Quantity >= 50 AND Quantity <= 79 THEN
OUTPUT "Ok"
ELSEIF Quantity >= 0 AND Quantity <= 49 THEN
OUTPUT "Low"
ELSE
OUTPUT "Invalid Input"
ENDIF

DECLARE Quantity : INTEGER


INPUT Quantity

IF Quantity >= 0 AND Quantity <= 200 THEN


IF Quantity >= 80 THEN
OUTPUT "High"
ELSEIF Quantity >= 50 AND Quantity <= 79 THEN
OUTPUT "Ok"
ELSE
OUTPUT "Low"
ENDIF
ELSE
OUTPUT "Invalid Input"
ENDIF

Task) Write a program that takes marks as input. The program accepts marks
between 0 and 100 (both values inclusive) as input. If marks are greater than
or equal to 50 the program outputs "Pass", otherwise it outputs "Fail". In
case of out of range input the program outputs "Invalid input".

DECLARE Marks : INTEGER


INPUT Marks
IF Marks >= 0 AND Marks <= 100 THEN
IF marks >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"

New Section 1 Page 4


OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
ELSE
OUTPUT "Invalid Input"
ENDIF

DECLARE Marks: INTEGER


INPUT Marks
IF Marks >= 50 AND Marks <= 100 THEN
OUTPUT "Pass"
ELSEIF Marks >= 0 AND Marks < 49 THEN
OUTPUT "Fail"
ELSE
OUTPUT "Invalid Input"
ENDIF

CASE Statement:

DECLARE Quantity : INTEGER

INPUT Quantity

CASE OF Quantity
80 TO 200 : OUTPUT "High"
50 TO 79 : OUTPUT "Ok"
0 TO 49 : OUTPUT "Low"
OTHERWISE: OUTPUT "Invalid Input"
ENDCASE
-----------------------------------------------------------------
DECLARE MyCharacter : CHAR

INPUT MyCharacter

CASE OF MyCharacter
'a' TO 'z' : OUTPUT "lower-case character"
'A' TO 'Z' : OUTPUT "upper-case character"
'0' TO '9' : OUTPUT "digit"
OTHERWISE : OUTPUT "Invalid input"
New Section 1 Page 5
OTHERWISE : OUTPUT "Invalid input"
ENDCASE

Task) Write a program that takes marks as input, calculates and output grade
of a student. Please use Case Statements.

DECLARE Marks : INTEGER

INPUT Marks

CASE OF Marks
90 TO 100 : OUTPUT "A*"
80 TO 89 : OUTPUT "A"
70 TO 79 : OUTPUT "B"
60 TO 69 : OUTPUT "C"
0 TO 59 : OUTPUT "Fail"
OTHERWISE : OUTPUT "Invalid Input"
ENDCASE

Task) Write a program that takes Quantity as input. If the Quantity is greater
than 500 then it prints "High", if the Quantity is between 300 and 500 (both
inclusive) it prints "Ok", otherwise it prints "Low". The maximum Quantity
can be 1000 and should not be less than Zero.

DECLARE Quantity : INTEGER

INPUT Quantity

IF Quantity > 500 AND Quantity <= 1000 THEN


OUTPUT "High"
ELSEIF Quantity >= 300 AND Quantity <= 500 THEN
OUTPUT "Ok"
ELSEIF Quantity >= 0 AND Quantity < 300 THEN
OUTPUT "Low"
ELSE
New Section 1 Page 6
ELSE
OUTPUT "Invalid Input"
ENDIF

___________________________________________

DECLARE Quantity : INTEGER

INPUT Quantity

IF Quantity >= 0 AND Quantity <= 1000 THEN


IF Quantity > 500 THEN
OUTPUT "High"
ELSEIF Quantity >= 300 THEN
OUTPUT "Ok"
ELSE
OUTPUT "Low"
ENDIF
ELSE
OUTPUT "Invalid Input"
ENDIF
___________________________________
DECLARE Quantity : INTEGER

INPUT Quantity

CASE OF Quantity
501 TO 1000 : OUTPUT "High"
300 TO 500 : OUTPUT "Ok"
0 TO 299 : OUTPUT "Low"
OTHERWISE: OUTPUT "Invalid Input"
ENDCASE

Task) Write a program to take a number as input and output whether the
number is Odd or Even?

DECLARE Number : INTEGER

New Section 1 Page 7


INPUT Number

IF NumberMOD2 = 0 THEN
OUTPUT "Even"
ELSE
OUTPUT "Odd"
ENDIF

Task) Write a program to take a number as input and output whether the
number is Positive or Negative?

DECLARE Number : INTEGER


INPUT Number
IF Number >= 0 THEN
OUTPUT "Positive"
ELSE
OUTPUT "Negative"
ENDIF

Loops:

- Count-controlled (FOR)
- Pre-condition (WHILE)
- Post-condition (REPEAT/UNTIL)

OUTPUT "1"
OUTPUT "2"
OUTPUT "3"
OUTPUT "4"
OUTPUT "5"

DECLARE i : INTEGER
FOR i <- 1 TO 5
OUTPUT i
NEXT i

New Section 1 Page 8


NEXT i

DECLARE i : INTEGER
i <- 1
WHILE i <= 5 DO
OUTPUT i
i <- i + 1
ENDWHILE

DECLARE i : INTEGER
i <- 1
REPEAT
OUTPUT i
i <- i + 1
UNTIL i > 5

Task) Write a program to output all numbers between 1 and 20 (both values
inclusive) with an interval of 3.

DECLARE Number : INTEGER


Number <- 1
FOR Number <- 1 TO 20 STEP=3
OUTPUT Number
NEXT Number

DECLARE Number : INTEGER


Number <- 1
WHILE Number <= 20
OUTPUT Number
Number <- Number + 3
ENDWHILE
New Section 1 Page 9
ENDWHILE

DECLARE Number : INTEGER


Number <- 1
REPEAT
OUTPUT Number
Number <- Number + 3
UNTIL Number > 20

Task) Write a program to OUTPUT all numbers between 1 and 10 that are
divisible by 3.

DECLARE Number : INTEGER


FOR Number <- 1 TO 10
IF NumberMOD3 = 0 THEN
OUTPUT Number
ENDIF
NEXT Number

Number <- 1
WHILE Number <= 10 DO
IF NumberMOD3 = 0 THEN
OUTPUT Number
ENDIF
Number <- Number + 1
ENDWHILE

Number <- 1
REPEAT
IF NumberMOD3 = 0 THEN
OUTPUT Number
ENDIF
Number <- Number + 1
UNTIL Number > 10

Task) Write a program that output the total count of numbers which are
New Section 1 Page 10
Task) Write a program that output the total count of numbers which are
divisible by 5 and 7 between 1 and 100 (both values inclusive).

DECLARE Number, Count : INTEGER


Count <- 0
FOR Number <- 1 TO 100
IF NumberMOD5 = 0 AND NumberMOD7 = 0 THEN
Count <- Count + 1
ENDIF
NEXT Number
OUTPUT Count

DECLARE Number, Count : INTEGER


Count <- 0
Number <- 1
WHILE Number <= 100 DO
IF NumberMOD5 = 0 AND NumberMOD7 = 0 THEN
Count <- Count + 1
ENDIF
Number <- Number + 1
ENDWHILE
OUTPUT Count

DECLARE Number, Count : INTEGER


Count <- 0
Number <- 1
REPEAT
IF NumberMOD5 = 0 AND NumberMOD7 = 0 THEN
Count <- Count + 1
ENDIF
Number <- Number + 1
UNTIL Number > 100
OUTPUT Count

Task) Write a program that takes three numbers as input and output the
largest among them.

DECLARE Number1, Number2, Number3 : INTEGER


New Section 1 Page 11
DECLARE Number1, Number2, Number3 : INTEGER

INPUT Number1
INPUT Number2
INPUT Number3

IF Number1 > Number2 AND Number1 > Number3 THEN


OUTPUT "The largest number is: " , Number1
ELSEIF Number2 > Number1 AND Number2 > Number3 THEN
OUTPUT "The largest number is: ", Number2
ELSE
OUTPUT "The largest number is: ", Number3
ENDIF
_____________________________________________________

DECLARE Number, Count, Largest : INTEGER

INPUT Number

Largest <- Number

FOR Count <- 1 TO 2


INPUT Number
IF Number > Largest THEN
Largest <- Number
ENDIF
NEXT Count

OUTPUT Largest

_____________________________________________

DECLARE Number, Count, Largest : INTEGER

INPUT Number

Largest <- Number

Count <- 1
New Section 1 Page 12
Count <- 1

WHILE Count <= 2 DO


INPUT Number
IF Number > Largest THEN
Largest <- Number
ENDIF
Count <- Count + 1
ENDWHILE

OUTPUT Largest

_______________________________________________

DECLARE Number, Count, Largest : INTEGER

INPUT Number

Largest <- Number

Count <- 1

REPEAT
INPUT Number
IF Number > Largest THEN
Largest <- Number
ENDIF
Count <- Count + 1
UNTIL Count > 2

OUTPUT Largest

Task) Write a program that outputs all even numbers between 1 and 30
(Both values inclusive). You are required to write the program with all three
loops.
New Section 1 Page 13
loops.

DECLARE Number : INTEGER

FOR Number <- 1 TO 30


IF NumberMOD2 = 0 THEN
OUTPUT Number
ENDIF
NEXT Number

Number <- 1
WHILE Number <= 30
IF NumberMOD2 = 0 THEN
OUTPUT Number
ENDIF
Number <- Number + 1
ENDWHILE

Number <- 1
REPEAT
IF NumberMOD2 = 0 THEN
OUTPUT Number
ENDIF
Number <- Number + 1
UNTIL Number > 30

Task) Write a program that outputs a table of a given number. Take the input
for the number from the user.

DECLARE Number, Count, TillWhatNumber : INTEGER

INPUT Number
INPUT TillWhatNumber

FOR Count <- 1 TO TillWhatNumber


OUTPUT Count , "x", Number, "=", Count*Number
New Section 1 Page 14
OUTPUT Count , "x", Number, "=", Count*Number
NEXT Count

___________________________________________

DECLARE Number, Count : INTEGER

INPUT Number
Count <- 1
WHILE Count <= 10 DO
OUTPUT Count , "x", Number, "=", Count*Number
Count <- Count + 1
ENDWHILE

___________________________________________________
DECLARE Number, Count : INTEGER

INPUT Number
Count <- 1
REPEAT
OUTPUT Count , "x", Number, "=", Count*Number
Count <- Count + 1
UNTIL Count > 10

Arrays (1D):

DECLARE Marks : ARRAY[1:5] OF INTEGER


DECLARE Index : INTEGER
Marks[3] <- 50
New Section 1 Page 15
Marks[3] <- 50
Marks[2] <- 25

FOR Index <- 1 TO LENGTH(Marks)


Marks[Index] <- 0
NEXT Index

Task) Write a program that adds all elements in an array and output the
total. The name of the array is List1. There are 7 elements in the array.

DECLARE List1 : ARRAY[1:7] OF INTEGER


DECLARE Index : INTEGER
DECLARE Total : INTEGER

List1 <- [25, 34, 23, 65, 34, 56, 10]


Total <- 0

FOR Index <- 1 TO LENGTH(List1)


Total <- Total + List1[Index]
NEXT Index

OUTPUT Total

Task) Write a program that calculates and output average of all elements in
an array. The name of the array is List1. There are 7 elements in the array.

DECLARE Average : REAL


DECLARE Total : INTEGER
DECLARE Index : INTEGER
Total <- 0
Average <- 0
FOR Index <- 1 TO LENGTH(List1)
Total <- Total + List1[Index]
NEXT Index
Average <- Total/LENGTH(List1)
OUTPUT Total
OUTPUT Average
New Section 1 Page 16
OUTPUT Average

Task) Write a program to output all elements in List1 that are divisible by 5.

DECLARE Index : INTEGER

FOR Index <- 1 TO LENGTH(List1)


IF List1[Index]MOD5 = 0 THEN
OUTPUT List1[Index]
ENDIF
NEXT Index

Task) Write a program to output all elements in List1 that are divisible by 5
and 8.

DECLARE Index : INTEGER


FOR Index <- 1 TO LENGTH(List1)
IF List1[Index]MOD5 = 0 AND List1[Index]MOD8 = 0 THEN
OUTPUT List1[Index]
ENDIF
NEXT Index

Task) Write a program to output the count of even numbers in an array. The
name of the array is MyList.

DECLARE Index, Count : INTEGER

Count <- 0

FOR Index <- 1 TO LENGTH(MyList)


IF MyList[Index]MOD2 = 0 THEN
Count <- Count + 1
ENDIF
NEXT Index
OUTPUT Count

New Section 1 Page 17


Worksheet 1:
Task 1)

DECLARE Length : INTEGER


DECLARE Width : INTEGER
DECLARE SurfaceArea : INTEGER
INPUT Length
INPUT Width
SurfaceArea <- Length * Width
OUTPUT SurfaceArea

Task 2)
DECLARE TemperatureC : REAL
DECLARE TemperatureF : REAL
INPUT TemperatureF
TemperatureC <- (TemperatureF - 32) * 5/9
OUTPUT TemperatureC

Task 3)
DECLARE Temperature : INTEGER
INPUT Temperature
IF Temperature < 1 THEN
OUTPUT "Freezing"
ELSE
OUTPUT "Not Freezing"
ENDIF

Task 4)
DECLARE Month : INTEGER
INPUT Month
IF Month >= 1 AND Month <= 12 THEN
IF Month = 1 THEN
OUTPUT "Jan"
ELSEIF Month = 2 THEN
OUTPUT "Feb"
ELSEIF Month = 3 THEN
New Section 1 Page 18
ELSEIF Month = 3 THEN
OUTPUT "Mar"
...

ELSE
OUTPUT "Dec"
ENDIF
ELSE
OUTPUT "Invalid Input"
ENDIF

Worksheet 2:
Task 1)

DECLARE Num : INTEGER


DECLARE Index : INTEGER

INPUT Num

IF Num > 0 THEN


FOR Index <- 1 TO Num
OUTPUT Index
NEXT Index
ELSE
OUTPUT "Invalid Input"
ENDIF

Task 2)

DECLARE Num, Count : INTEGER


INPUT Num
FOR Count <- Num TO 1 STEP=-1
OUTPUT Count
NEXT Count

DECLARE Num, Count : INTEGER


INPUT Num
Count <- Num
WHILE Count >= 1
New Section 1 Page 19
WHILE Count >= 1
OUTPUT Count
Count <- Count - 1
ENDWHILE

Task 3)
DECLARE Number, Interval, Count : INTEGER
INPUT Number
INPUT Interval

FOR Count <- 1 TO Number STEP = Interval


OUTPUT Count
NEXT Count

DECLARE Number, Interval, Count : INTEGER


INPUT Number
INPUT Interval
Count <- 1

WHILE Count <= Number


OUTPUT Count
Count <- Count + Interval
ENDWHILE

DECLARE Number, Interval, Count : INTEGER


INPUT Number
INPUT Interval
Count <- 1
REPEAT
OUTPUT Count
Count <- Count + Interval
UNTIL Count > Number

New Section 1 Page 20


u
Tuesday, 3 October 2023 3:32 pm

New Section 1 Page 21

You might also like