Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

DISCRETE MATHEMATICS

ALGORITHM

An algorithm is a set of step-by-step instructions that must be executed in order to solve a


problem or perform a task.

Pseudocode- a detailed yet readable description of what a computer program or algorithm


must do, expressed in a formally-styled natural language rather than in a programming
language.

SAMPLE 1

Add two integers and display the sum.


Enter two integers

Read num1

Read num2

Sum=num1+num2

Display sum

SAMPLE 2

Compute and display the average of 3 quiz scores


Enter three quiz scores

Read score1, score2, score3

Average=(score1+score2+score3)/3

Display average
SAMPLE 3

Sample inputs/integers 43862

Max=4

4<3 max=4

4<8 max=8

8<6 max=8

8<2 max=8

Max=8-largest element

Sample 4

DISPLAY THE NUMBERS 1 2 3 4 5 IN REVERSE ORDER

Assign the values to variables a1 to an

a1=1

a2=22

a3=3

a4=4

an=5

For i=5 to 1

Display ai
Properties of algorithm

1. Input
2. Output
3. Correctness
4. Finiteness
5. Effectiveness

Selection Statement

• If condition then statement

Ex: If grade=100 then display “Excellent”

• If condition then

Statement1

Else

Staement2

Ex: If grade=100 then

Display “Excellent”

ELSE

Display “ For improvement”

Repetition(Loop)

For variable= initial value to end value

Statement/s

Ex: Display “LPU” five times

For m=1 to 5

Display “LPU”
SEARCHING ALGORITHM
4,7,3,2,1,0,9
SEATWORK:
Write the pseudocode of the following:
1. Display numbers one to ten together with your name
1 John
2 John
3 John
.
.
10 John
2. Display the summation of numbers1, 2,3,4,and 5 using a loop.

You might also like