0% found this document useful (0 votes)
15 views

Programming Sec 2

Uploaded by

BADR ESLAM
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Programming Sec 2

Uploaded by

BADR ESLAM
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Programming

Section - 2
Flowcharts of Loops
• Loops are used to repeat the execution of a step or several steps for a
number of times.

• A loop refers to a set of instructions that is repeated as long as a


criterion holds.

3
There are two types of loops

Repeat Loop While Loop

false
Condition
true

true
Condition

false
4
Start

c=1

Example 1: c <= 100


No
Draw a flowchart to Stop
Yes
print numbers from
Print c
1 to 100
c = c+ 1

5
Start

C=0
Sum=0

Example 2: Draw
a flowchart to C <50
No

find the sum of Yes


first 50 natural Sum=Sum+C Print Sum
numbers
C=C+1
End

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

Example 6: Draw a max= current

flowchart to determine Counter=1


the largest number of (n)
numbers. The user
Yes No
should enter the count Counter<n
of numbers in variable Counter=counter+1
(n) and enter the Input next Print max
numbers in variable
(current) at first time. No
Next>max End
the other numbers
entered in (next) yes
variable. Max=next

10
Start

nodd=0,count=1
nEven=0

Input number
Example 7:
No Yes
Draw a flowchart to number%2=0

determine the count nodd=nodd+1 nEven=nEven+1

of odd & Even


numbers when the Count=count+1

user enter 50 Yes


Count<=50
random values. No
Print nodd,nEven

Stop
11
Start

number=1
C=0
Sum=0

Sum=Sum+number

Example 8: Draw a number=number+2


flowchart to display C=C+1
the average of first
ten odd numbers. Yes C<10

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

flowchart, What is T=T+2


the value of R
Yes T<10

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

flowchart, What is T=T+2


the value of R
Yes T<10

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

Example 13: from I<3


No
Print sp
the following
flowchart, What is Yes
Stop
the value of sp Read num

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

You might also like