Lect 2 Unit 1
Lect 2 Unit 1
Pseudocode:
Input a set of 4 marks
Calculate their average by summing and dividing by 4
if average is below 50
Print “FAIL”
else
Print “PASS”
Pseudocode & Algorithm
Detailed Algorithm
Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
The Flowchart
A Flowchart
shows logic of an algorithm
emphasizes individual steps and their interconnections
e.g. control flow from one action to the next
Basic Flowchart Symbols
Name Symbol Use in Flowchart
START
Step 1: Input M1,M2,M3,M4
Input Step 2: GRADE (M1+M2+M3+M4)/4
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL”
GRADE(M1+M2+M3+M4)/4
else
Print “PASS”
N IS Y endif
GRADE<50
PRINT PRINT
“PASS” “FAIL”
STOP
Example 2
Write an algorithm and draw a flowchart to convert the length in feet to centimeter.
Pseudocode:
Input the length in feet (Lft)
Calculate the length in cm (Lcm) by multiplying LFT with 30
Print length in cm (LCM)
Example 2
Algorithm Flowchart
Step 1: Input Lft
START
Step 2: Lcm Lft x 30
Input
Step 3: Print Lcm Lft
Lcm Lft x 30
Print
Lcm
STOP
Example 3
Algorithm
START
Step 1: Input W,L
Input
Step 2: AL x W W, L
Step 3: Print A
ALxW
Print
A
STOP
Flowcharts
Flowcharts is a graph used to depict or show a step by step solution using symbols
which represent a task.
The symbols used consist of geometrical shapes that are connected by flow lines.
Disk storage I/O symbol - indicates input from or output to disk storage.
Statement 1
Statement 2
Statement 3
:
Flowchart – selection control structure
No Yes
Condition
else- then-
statement(s) statement(s)
Flowchart – repetition control structure
yes Loop
Condition
Statement(s)
no
Flowchart – example 1
Begin
Calculate
Age = current year – birth date
Display
age
End
Flowchart – example 2
Begin
Read age
End
Flowchart – example 5
To find the sum of first 10 natural numbers
Begin
sum = 0
current_number = 1
NO
current_number <= 10?
print sum
YES
End
sum = sum + current_number
current_number = current_number + 1
Example 4
Write an algorithm and draw a flowchart that will calculate the
roots of a quadratic equation
ax 2 bx c 0
Hint: d = sqrt ( b 2 4ac), and the roots are: x1 = (–b + d)/2a
and x2 = (–b – d)/2a
Exercises: Algorithm & Flowchart
2.) Create an algorithm and a flowchart that will compute the area of
a circle.
Exercises: Algorithm & Flowchart
3.) Create an algorithm and a flowchart that will compute the sum of
two numbers. If the sum is below or equal to twenty, two numbers
will be entered again. If the sum is above 20, it will display the sum.
Activity: Algorithm & Flowchart
Flowchart
Number System
A number system is defined as the representation of numbers by using digits or other symbols in a
consistent manner.
The value of any digit in a number can be determined by a digit, its position in the number, and the
base of the number system.
The numbers are represented in a unique manner and allow us to operate arithmetic operations
like addition, subtraction, and division.
In a positional base b numeral system (with b a natural number greater than 1 known as
the radix), b basic symbols (or digits) corresponding to the first b natural numbers including zero
are used. To generate the rest of the numerals, the position of the symbol in the figure is used. The
symbol in the last position has its own value, and as it moves to the left its value is multiplied
by b.
For example, in the decimal system (base 10), the numeral 4327 means
(4×103) + (3×102) + (2×101) + (7×100), noting that 100 = 1.
In general, if b is the base, one writes a number in the numeral system of base b by expressing it in
the form anbn + an − 1bn − 1 + an − 2bn − 2 + ... + a0b0 and writing the enumerated digits anan − 1an −
2 ... a0 in descending order. The digits are natural numbers between 0 and b − 1, inclusive.
Types of Number System
Binary Number System (Base 2)
A computer can understand only the “on” and “off” state of a switch.
The combination of 1 and 0 form binary numbers. These numbers represent various data.
Binary numbers
All computers work with 0’s and 1’s so it is like learning alphabets before learning English
Binary to Decimal conversion
Take each digit in the binary number and multiply it with its place
value.
The sum of all the above products will give the equivalent decimal
number.
For the fraction (after the decimal point) the place value starts
with negative power of 2. This negative power value increases from
left to right.
The place value of the first left digit in fraction is (2–1)
The place value of the next right digit in fraction is (2–2)
The place value of the next right digit in fraction is (2–3)
……. so on
Ex :- Convert the binary number (1010.0101)2 into decimal number.
Sol :- Integer part (1010)2 = (1 x 23) + ( 0 x 22) + (1 x 21) + (0 x 20 )
= (1 x 8) + ( 0 x 4) + (1 x 2) + (0 x 1 )
= (10)10
Fractional part (0.0101)2 = (0 x 2–1) + ( 1 x 2–2) + (0 x 2–3) + (1 x 2–4)
1 1 1 1
= (0 x ) + ( 1 x ) + (0 x ) + (1 x )
2 4 8 16
= 0 x 0.5 + 1 x 0.25 + 0 x 0.125 +1 x 0.0625
= 0 + 0.25 + 0 + 0.0625
= (0.3125)10
Decimal to Binary conversion
(56)10=(111000)2
Fractional Decimal number to binary
The octal number system uses eight digits: 0,1,2,3,4,5,6 and 7 with the base of 8.
Digits like 8 and 9 are not included in the octal number system.
Just as the binary, the octal number system is used in minicomputers but with digits from 0 to 7. For
example: 358, 238, 1418 are some examples of numbers in the octal number system.
Decimal Number System and Octal Number System
This system uses digits 0 to 7 (i.e. 8 digits) to represent a number and the numbers are as
a base of 8.
Step 1: Identify the base of the required number. Since we have to convert the given number into
the octal system, the base of the required number is 8.
Step 2: Divide the given number by the base of the required number and note down the quotient
and the remainder in the quotient-remainder form. Repeat this process (dividing the quotient again
by the base) until we get the quotient less than the base.
Step 3: The given number in the octal number system is obtained just by reading all the
remainders and the last quotient from bottom to top.
Convert 432010 into the octal system.
432010 = 103408
Octal to Decimal
Hexadecimal Number System (Base 16)
16 digits used to represent a given number. Thus it is also known as the base 16 number system.
As the base is greater than 10, the number system is supplemented by letters.
To take A, B, C, D, E, and F as part of the number system is conventional and has no logical or
deductive reason.
Example 2: Convert 5BC16 into the decimal system.
5BC16 is in the hexadecimal system. We know that B=11 and C= 12 in the hexadecimal system. So
we get the equivalent number in the decimal system using the following process:
5BC16 = 146810
Convert hexadecimal number 1F.01B into decimal number.
Since value of Symbols: B and F are 11 and 15 respectively. Therefore equivalent decimal number is,
= (1F.01B) 16
= (1x161+15x160 +0x16-1+1x16-2+11x16-3) 10
27 010 111 23
(10111.1) = (010 111 . 100) = (2 7 . 4)8
135 001 011 101 93