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

Looping Procees

The document discusses different types of looping processes in algorithms and flowcharts, including for, while, and do-while loops. It provides examples of algorithms and flowcharts for common problems like summation, determining prime numbers, finding the greatest common divisor (GCD) of two numbers, and calculating the Fibonacci series. Key points covered include the differences between entry-controlled and exit-controlled loops, and how to represent different loop types in algorithms and flowcharts.

Uploaded by

Erwin Marcelo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Looping Procees

The document discusses different types of looping processes in algorithms and flowcharts, including for, while, and do-while loops. It provides examples of algorithms and flowcharts for common problems like summation, determining prime numbers, finding the greatest common divisor (GCD) of two numbers, and calculating the Fibonacci series. Key points covered include the differences between entry-controlled and exit-controlled loops, and how to represent different loop types in algorithms and flowcharts.

Uploaded by

Erwin Marcelo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

LOOPING IN

ALGORITHM
AND
FLOWCHART
Fundamentals of
Programming
By:
Erwin D. Marcelo
LOOPING Two types of loop process 2

PROCESS 1. entry controlled


CONDITIONAL ITERATIVE 2. exit controlled
PROCESS WHICH IS USED TO
CHECK FOR
CERTAIN CONDITIONS AND
THEN REPEATEDLY
EXECUTE A BLOCK OF CODE
AS LONG AS THOSE
CONDITIONS ARE MET.

Entry controlled Exit controlled


ENTRY CONTROLLED LOOP
 For FOR LOOP
 While WHILE LOOP
We use the for loop when the increment
and initialization are very simple. We
use the while loop in the case of a
complex initialization.
The for loop is a control flow statement that's
used to iterate through a sequence of values.
The while loop is a control flow statement that
allows you to continuously execute commands
as long as a condition evaluates to true.
The for loop is used when we already
know the number of iterations
Start
FOR LOOP
ALGORITHM – summation of N
sum=0
1. Initialize sum=0
2. Accept N
3. For i = 1 to N Read N
sum = sum + i
sum N i
Next i ( i= i+1)
4. Print sum 0 5 1
i= 1
1 2
i = i +1
WHILE LOOP 3 3
Y 6 4
ALGORITHM – summation of N i <=N sum=sum + i,
10 5
1. Initialize sum=0 N 15 6
2. Accept N Print
3. Let i=1 sum
4. While (i <= N)
sum = sum + i
i= i+1 End
5. Print sum
Start
DO-WHILE LOOP
sum N i
sum=0
0 5 1
ALGORITHM – summation of N 1 2
Read N 3 3
1. Initialize sum=0
2. Accept N 6 4
3. Let i=1 10 5
4. Do i=1
15 6
sum = sum + i
i= i+1
sum=sum + i,
While(i<=N)
i = i +1
5. Print sum

Y
i<=N

N
Print End
sum
PRIME NUMBERS
Create an algorithm and a flowchart that determine if the
entered number is a PRIME or NOT
A prime number is a number that can only be divided by
itself and 1 without remainders.

ALGORITHM - FOR ALGORITHM - WHILE ALGORITHM – DO-WHILE

1. Accept N 1. Accept N 1. Accept N


2. Let count = 0 2. Let count = 0 2. Let count = 0
3. FOR d = 1 TO N 3. Let d = 1 3. Let d = 1
x = N mod d 4. WHILE ( d<=N) 4. DO
If (x = 0) then x = N mod d x = N mod d
count = If (x = 0) then If (x = 0) then
count + 1 count = count =
NEXT d count + 1 count + 1
4. If ( count = 2) then d=d+1 d=d+1
Dislpay “PRIME” 5. If ( count = 2) then WHILE(d <= N)
Else Dislpay “PRIME” 5. If ( count = 2) then
Display “NOT Else Dislpay “PRIME”
PRIME” Display “NOT Else
GCD
Create an algorithm and a flowchart that will output
for g.c.d. of two number.
The greatest common divisor (GCD) of A B D X GCD
two or more numbers is the greatest 4 8 1 4 1
common factor number that divides them, 2 2
exactly.
3 4
ALGORITHM
4
1. Accept A, B 5
2. D = 1
3. If A < B then
X=A
Else
X=B
4. While(D<= X)
If (A mod D = 0 AND B mod D =
0)
GCD = D
D=D + 1
Read
START A,B
8

ALGORITHM
X=B
1. Accept A, B
2. D = 1
3. If A < B then Y
A<B X=A
X=A
Else
N
X=B
4. While(D<= X) X=B
If (A mod D = 0 AND B mod D =
0)
GCD = D
D=D + 1 Amod d=0 Y
5. Print GCD GCD = D AND D<=X
B mod D=0
N

Print
D=D+1
GCD

END
FIBONACCI SERIES START

Create an algorithm and a flowchart that will output Read


the Fibonacci series up to a given number. N
Example: Fibonacci of 8
0,1,1,2,3,5,8,13,… a=0, b=1,
ctr=0
N a b ctr Print a c
8 0 1 0 0 1 Print a
1 1 1 1 2
1 2 2 1 3
c= a +b, a=b,
2 3 3 2 5 b=c, ctr=ctr+1
3 5 4 3 8
5 8 5 5 13 Y
8 13 6 8 21 ctr<N

13 21 7 13 34 N
21 34 8 END
START
ALGORITHM

1. Accept N Read
2. a=0, b=1, ctr=0 N
3. DO
Print a a=0, b=1,
c=a+b ctr=0
a=b
b=c
ctr=ctr + 1 Print a
WHILE (ctr<N)

c= a +b, a=b,
b=c, ctr=ctr+1

Y
ctr<N

N
END
A B x1 x2 i ctr j Display i
PRIMES NUMBERS BETWEEN 2 GIVEN 5 2 2 5 2 0 1 2
NUMBERS 3 1 2 3
Create an algorithm and a flowchart that will output all the 4 2 3 5
prime numbers between 2 numbers. 5 0 1
Algorithm 6 1 2
2 3
1. Accept A, B
2. If A < B then 0 4
x1=A, x2 = B 1 1
Else 2 2
x1 = B, x2 = A
3. i = x1 3 3
4. WHILE (i <= x2) 0 4
ctr = 0 1 5
FOR j = 1 TO i
If ( i mod j =0) 2 1
ctr = ctr + 1 2
NEXT j 3
If ctr = 2 then
Display i 4
i=i+1 5
6
START

Algorithm Read c= a +b, a=b,


N b=c, ctr=ctr+1
1. Accept A, B
2. If A < B then
Y
x1=A, x2 = B A<B x1 = A
Else
x1 = B, x2 = A
3. i = x1 ctr = ctr+1
x1 = A
4. WHILE (i <= x2)
ctr = 0
FOR j = 1 TO i i<=x2
If ( i mod j i=x1
=0)
ctr
= ctr + 1
NEXT j i<=x2 ctr = 0 j=1 j <= i
If ctr = 2 then N
Display i
i=i+1
Print a
THANK YOU

You might also like