Professional Documents
Culture Documents
C++ Lectures
C++ Lectures
PROGRAMMING FUNDAMENTALS
LECTURE # 4
AL G OR I T HM: COMPAR I NG T WO NUMB E R S
1.0 start
2.0 declare n1, n2
3.0 input n1,n2
4.0 if (n1 > n2)
4.1 print "n1 is greater"
5.0 endif
6.0 if (n2 < n1)
6.1 print "n2 is greater"
7.0 endif
8.0 if (n1 = n2)
8.1 print "they are equal“
9.0 endif
10.0 end
2
NE ST E D I F/ E L SE SE L E CT ION ST RUCT UR E
3
E XAMPL E
COMPAR I NG T WO NUMB E R S W I T H NE ST E D I F - E LSE
1.0 start
2.0 declare n1, n2
3.0 input n1,n2
4.0 if (n1 > n2)
4.1 print "n1 is greater"
5.0 else
5.1 if (n2 < n1)
5.1.1 print "n2 is greater"
5.2 else
5.2.1 print "they are equal“
5.3 endif
6.0 endif
7.0 end
4
E XAMPL E: G R ADI NG W I T H NE ST E D I F - E L SE
5
E XAMPL E: G R ADI NG AL G OR I T HM W I T H NE ST E D I F - E L SE
6
I F- E L SE SE L E CT ION ST RUCT URE
Compound statement
7
E XAMPL E: I F - E L SE SE L E CT ION ST RUCT UR E
8
CL ASS ACT I V I T Y
Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid
9
L OOPI NG FL OWCH ART
Statement (s)
10
R E PE T I T ION ST RUCT UR E
WHILE / ENDWHILE
11
W H I L E / E NDW H I LE
Start
count = 0
1.0 count = 0
12
E XAMPL E OF P R I NT I NG ODD NUMB E R S
13
AL G OR I T HM FOR P R I NT I NG 1 0 ODD NUMB E R S
1. Start
2. declare count, num
3. input num
4. if num mod 2 = 0 then
4.1 num = num + 1
5. end if
6. count = 0
7. while count < 10
7.1 print num
7.2 num = num + 2
7.3 count = count +1
8. end while
9. End
14
FL OW C H ART FOR P R I NT I NG 1 0 OD D NU MB E R S
START
1
False True
num mod 2 = 0
Print num
True
num = num + 2
num = num +1 count = count +1
END
15
CL ASS ACT I V I T Y
Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid
16
CL ASS ACT I V I T Y
17
CL ASS ACT I V I T Y: SQUAR E AND CUB E
18
CL ASS ACT I V I T Y
• Write down an algorithm of a program that will roll two dices and
add the numbers that appear on the top. If the sum is odd and
greater than 8 the player wins one chocolate, otherwise two
chocolates. If the number is even, he/she will win an ice cream.
19
CL ASS ACT I V I T Y
20
CL ASS ACT I V I T Y
21
CL ASS ACT I V I T Y
• Write down a program that will take some numbers as input from
the user one by one and will tell the user which number was
maximum and minimum of all the entered numbers.
22
END