Modified Leture Notes 7 (CSC-103) Algorithm
Modified Leture Notes 7 (CSC-103) Algorithm
Basic Algorithm
Given By- Lecturer Fardina Fathmiul Alam
CSE Dept. IUBAT
Variable
A variable is a data name that may be
used to store a data value.
Variable hold a space in the computer
memory. Variable a declaring means
its hold a position in the computer
memory:
Valid name:
Average, hight,Counter_1, x1,ph_value
Invalid Names:
123,(area),25th,%
Start
Declare 3 variables x,y,z of int type
Input the value on x
Input the value on y
Calculate z=x+y and store the
result on z
Print z
stop
12
Start
Declare 4 variables a,b,c,d of int type
Put the values on a,b,c
Calculate d=(a+b+c)/3
Store the result on d
Print d
stop
Start
Declare two variables d and m.
Put the value on d.
Calculate m=d/30.
Store the result on m
Print m
Stop
EXERSICE
Ask for float number and divide
second by first
Convert meter to kilometer
Start
Declare three variables c, x, y
Put the value on c.
Calculate x=c+1 (next int value)
Print x.
Calculate y=c-1 (prev int value)
Print y.
Stop
Relational Operators
Relational Operators
Operator
Description
>
Greater than
<
Less than
Equal to
Not equal to
FLOW CHART
Flow Chart
A Flowchart
Graphical Representation of Algorithm
shows logic of an algorithm
shows individual steps and their
interconnections
Flowchart Symbols
Basic
Example 3
Write an algorithm and draw a
flowchart that will read the two sides
of a rectangle and calculate its area.
Pseudocode
Input the width (W) and Length (L) of a
rectangle
Calculate the area (A) by multiplying L
with W
Print A
Example 3
Algorithm
Step 1:
Step 2:
Step 3:
START
Input W,L
AL x W
Print A
Input
W, L
ALxW
Print
A
STOP
IFTHENELSE
STRUCTURE
The structure is as follows
If condition then
true alternative
else
false alternative
endif
IFTHENELSE
STRUCTURE
The algorithm for the flowchart is as
follows:
If A>B then
print A
Y
N
is
A>B
else
print B
Print
Print
A
B
endif
Example 5
Write an algorithm that reads two values,
determines the largest value and prints the
largest value.
ALGORITHM
Step 1:
Input VALUE1, VALUE2
Step 2:
if (VALUE1 > VALUE2) then
MAX VALUE1
else
MAX VALUE2
endif
Step 3:
Print The largest value is, MAX
Example 5
START
Input
VALUE1,VALUE2
is
VALUE1>VALUE2
MAX VALUE1
MAX VALUE2
Print
The largest value is,
MAX
STOP
Step 1:
Input M1,M2,M3,M4 of float
type
Step 2: GRADE (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print FAIL
else
Print PASS
endif
Example
START
Input
M1,M2,M3,M4
GRADE(M1+M2+M3+M4)/4
IS
GRADE<5
0
PRINT
PASS
PRINT
FAIL
STOP
The End