Programming Sec 2
Programming Sec 2
Section - 2
Flowcharts of Loops
• Loops are used to repeat the execution of a step or several steps for a
number of times.
3
There are two types of loops
false
Condition
true
true
Condition
false
4
Start
c=1
5
Start
C=0
Sum=0
Example 2: Draw
a flowchart to C <50
No
6
Start
Input n
Example 3: F=1
M=1
Draw a flowchart to
print the factorial of F = F*m
(N!)
where No
m=m+1 m=n
N! = 1*2*3*….*N
Yes
Print F
End
Start
C=1
Sum=0
Example 4:
Draw a flowchart No Yes
C%2=0
of a program to
sum all the even
Sum=Sum+C
numbers
between 1 and
20 then display C=C+1
the sum Yes
C<=20
No
Print Sum End
Start
Input base,power
Product=base
Example 5: Draw a C=1
flowchart to
calculate 𝑥 𝑦 No
C<power
Yes
where x is the base ,
y is the power. Print product Product=product * base
End C=C+1
9
Start
Input n,current
10
Start
nodd=0,count=1
nEven=0
Input number
Example 7:
No Yes
Draw a flowchart to number%2=0
Stop
11
Start
number=1
C=0
Sum=0
Sum=Sum+number
No
av=Sum/10
print av
Stop 12
Find the output of
the following
flowchart
Part - 2
Start
Input x , y
Sum = x+ y
Put all the
Example 9: from the variables
following flowchart, print sum in the
What is the value of table
sum where x= 10, y Stop
= 9.
X Y Sum
10 9 19
14
Start
R=0
T=1
Example 11: from
the following R= R+ T
No
print R
Stop
15
Answer : 1- put all the variable in the table
Start 2- following flowchart step by step to go to stop
R T
R=0
T=1 0 1
R= R+T = 0+1=1 T = T + 2 = 1+2 =3
R= R+ T 3 < 10 ? Yes
R= R+T = 1 + 3=4 T = T + 2 = 3+2 =5
T=T+2
5 < 10 ? Yes
Yes T<10
R= R+T = 4+ 5=9 T = T + 2 = 5+ 2 =7
7 < 10 ? Yes
No R= R+T = 9+ 7=16 T = T + 2 = 7+ 2 =9
print R 9 < 10 ? Yes
R= R+T= 16+9=25 T = T + 2 = 9+ 2 =11
Stop
11 < 10 ? No => go to print R (25) then go to end 16
Start
R=0
T=1
Example 12: from
the following R= R+ T
No
print R
Stop
17
Answer : 1- put all the variable in the table
Start 2- following flowchart step by step to go to stop
R=0 R T
T=1 0 1
R= R+T = 0+1=1 T = T + 2 = 1+2 =3
R= R+ T 3 < 10 ? Yes
T=T+2
R=0 T=0
R= R+T = 0+1=1 T = T + 2 = 1+2 =3
Yes T<10 3 < 10 ? Yes
R=0 T=0
No
R= R+T = 0+1=1 T = T + 2 = 1+2 =3
print R
3 < 10 ? Yes
Stop This is infinite loop
18
18
Start
I =0
Sp = 0
I= I+1
Yes
Num >0 Sp = sp + num
No
19
Answer :
1- put all the variable in the table
Start 2- following flowchart step by step to go to stop
I =0
I sp num
Sp = 0 0 0 -
I = I + 1 = 0+1=1
I= I+1 1 < 3 ? Yes
6
No
I<3 Print sp 6> 0? yes
Sp = 0 +6 =6
Yes
I = 1 + 1= 2
Read num Stop 2 < 3 ? Yes
-1
Yes -1 > 0 ? No
Num >0 Sp = sp + num
I = 2 +1 = 3
No 3> 3? No then print sp (6) then stop 20
Start
count=1
sEven=0
Input number
No Yes
number%2=0
Task 14: from the sEven=sEven+number
following flowchart,
What is the value of Count=count+1
sum Even
Yes
Count<=5
No
Print sEven
Stop
21
Start
count sEven number
count=1 1 0 -
sEven=0
10
Input number 10 %2=0 ? Yes
sEven =0+10=10
No Yes
number%2=0 1+1=2
Task 14: from the sEven=sEven+number
2<=5? yes
5
following flowchart,
5%2 =0 ? No
What is the value of
Count=count+1
2+1=3
sum EvenYes 3<=5? yes
Count<=5
2
No 2%2=0? yes
Print sEven sEven =10+2=12
Stop
3+1=4
4 <= 5? yes
22
6
Start
Cont……………………..
count=1
sEven=0
Input number
No Yes
number%2=0 count sEven number
Task 14: from the sEven=sEven+number 6%2 = 0? yes
12+6=18
following flowchart,
4+1=5
What is the value of
Count=count+1
5<=5? yes
sum EvenYes 3
Count<=5
3%2=0?No
No 5+1=6
Print sEven 6<=5 ? No
Stop Print (18)
23
Thanks
24