Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
1K views

Algorithm - Flowchart - Examples

The document provides examples of algorithms and flowcharts for various types of problems. It includes sample algorithms and flowcharts for swapping variable values, calculating the area of shapes, finding maximum values, checking positive/negative/zero values, calculating grades, and adding the first 10 odd numbers using a loop. The algorithms are presented step-by-step and the flowcharts demonstrate the logic flow visually.

Uploaded by

Vraj Shah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
1K views

Algorithm - Flowchart - Examples

The document provides examples of algorithms and flowcharts for various types of problems. It includes sample algorithms and flowcharts for swapping variable values, calculating the area of shapes, finding maximum values, checking positive/negative/zero values, calculating grades, and adding the first 10 odd numbers using a loop. The algorithms are presented step-by-step and the flowcharts demonstrate the logic flow visually.

Uploaded by

Vraj Shah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Algorithm and Flowchart Examples

Sample Flowchart & Algorithm


(Note: Assuming all inputs are valid)
Start
Algorithm: Interchanging / swapping values of two variables.
Step 1.) BEGIN Input N1, N2
Step 2.) INPUT N1, N2
Step 3.) temp  N1
Step 4.) N1  N2 temp←N1
Step 5.) N2  temp
Step 6.) Print N1, N2
Step 7.) END N1←N2

N2←temp

Print N1, N2

Stop

EXAMPLE OF FORMULA BASED ALGORITHMS


Algorithm: Find Area of circle Start
Step 1.) BEGIN
Step 2.) INPUT radius Input Radius
Step 3.) Area  22 / 7 * radius * radius
{Area of circle =  R2 } Area ← 22/7 * Radius * Radius
Step 4.) Print “Area of the circle is”, Area
Step 5.) END
Print “Area of Circle is”, Area

Stop
Algorithm: Find Simple Interest
Step 1.) BEGIN
Step 2.) INPUT P, R, N
Step 3.) I  P * R * N / 100 Start
{ Use formula for simple interest }
Step 4.) Print “Simple Interest is “, I Input P, R, N
Step 5.) END
Area ← P * R * N/100

Print “Simple Interest is”, I

Stop
Prof Monika Shah
Algorithm and Flowchart Examples

Start
Algorithm: Find Area of Rectangle
Step 1.) BEGIN
Input length, width
Step 2.) INPUT Length, Width
Step 3.) Area  Length * Width
Step 4.) Print “Area of the Rectangle is “, Area Area ← length*width
Step 5.) END
Print “Area of
rectangle is”, Area

Stop

EXAMPLE OF CONDITION / DECISION BASED ALGORITHMS


Algorithm: Find maximum of two numbers Start
Step 1.) BEGIN
Step 2.) INPUT N1, N2 Input N1, N2
Step 3.) IF (N1 > N2) Then {Compare two numbers}
Begin
max  N1 N1 > N 2 ? No
End
ELSE
Begin Yes
max  N2 max←N1 max←N2
End
Step 4.) Print “maximum number is”, max
Step 5.) END

Print “maximum is ”,
max

Stop
Algorithm: Find maximum of three numbers
Step 1.) BEGIN
Step 2.) INPUT N1, N2, N3
Step 3.) IF (N1 > N2) Then
Begin
IF (N1 > N3) Then
Begin
Print “maximum is ”, N1
End
ELSE
Begin
Print “maximum is ”, N3

Prof Monika Shah


Algorithm and Flowchart Examples

End
End
ELSE
Begin
IF (N2 > N3) Then
Begin
Print “maximum is ”, N2
End
ELSE
Begin
Print “maximum is ”, N3
End
End
Step 4.) END
Start

Input N1, N2, N3

Yes N1 > N 2 ? No

Yes N1 > N 3 ? No
No N2 > N 3 ? Yes

Print N1 Print N3 Print N2

Stop Stop
Stop

Algorithm: Algorithm to check +ve, -ve , or zero


Step 1.) BEGIN
Step 2.) INPUT N
Step 3.) IF (N = 0) ThenBegin
Print N, “is zero”
End
ELSE
Begin
IF (N > 0)
Begin
Print N, “is positive”
End
ELSE
Begin
Print N, “is negative”
End
End
Step 4.) END

Prof Monika Shah


Algorithm and Flowchart Examples

Start

Input N

Yes N=0? No

Print “Zero” No
Yes N>0?

Print “Positive”
Print “Negative”

Stop

Algorithm: Compute Percentage and Grade for a student (Input : 3 subjects marks - out of 100)
Step 1.) BEGIN
Step 2.) INPUT Mark1, Mark2, Mark3
Step 3.) IF (Mark1<35 OR Mark2<35 OR Mark3<35) Then
Begin
Print “Grade is “, “Fail” {Assumption 35 is passing marks}
GOTO Step 10
End
Step 4.) Sum  Mark1 + Mark2 + Mark3
Step 5.) Per  sum / 3
Step 6.) IF Per >=70 Then
Begin
Print “Grade is”, “Distinction”
GOTO Step 10
End
Step 7.) IF Per >=60 Then
Begin
Print “Grade is”, “First”
GOT Step 10
End
Step 8.) IF Per >=50 Then
Begin
Print “Grade is”, “Second”
GOT Step 10
End
Step 9.) Print “Grade is”, “Pass”
Step 10.) End

Prof Monika Shah


Algorithm and Flowchart Examples
Start

Input m1, m2, m3

m1< 35 or
m2<35 or Yes
m3<35
No
sum←m1+m2+m3 Print “Fail ”

per←sum/3

Yes
No per ≥ 70 ?

No Yes
per ≥60 ?
Print “Distinction ”

Yes
No Per ≥ 50 ? Print “First ”

Print “Pass ” Print “Second ”

Stop

EXAMPLE OF LOOP BASED ALGORITHMS


Algorithm: Find Addition of first 10 odd numbers
Step 1.) BEGIN
Step 2.) I1
Step 3.) cnt  0 {Initialize counter }
Step 4.) Sum  0 {Initialize sum as 0 }
Step 5.) REPEAT
Step 6.) IF (I mod 2  0) Then {Check for odd number }
Begin
Sum  Sum + I
cnt  cnt + 1 {Increment counter}
End
Step 7.) I  I + 1 {Next Number }
Step 8.) UNTIL ( cnt =10) {Repeat until counter equals to 10}
Step 9.) Print Sum
Step 10.) END

Prof Monika Shah


Algorithm and Flowchart Examples
Start

I←0
sum←0
cnt←0

I mod 2 ≠ 0 ? No

Yes
sum←sum+I
cnt←cnt+1

I←I+1

No cnt = 10 ?

Yes
Print sum

Stop

Algorithm: Find sum of successive numbers range from N1 to N2


Step 1.) BEGIN Start
Step 2.) INPUT N1, N2
Step 3.) Sum  0, temp  N1 Input N1, N2
Step 4.) WHILE (temp < N2) DO
Step 5.) Sum  sum + temp {Add the Number }
sum←0
Step 6.) temp  temp + 1 {Next Number }
temp←N1
Step 7.) END-WHILE
Step 8.) Print “Sum of numbers for range ”,
N1, “ to”, N2, “is”, Sum No
Step 9.) END temp < N2 ?

Yes
sum←sum + temp
temp←temp + 1

Print sum

Stop

Prof Monika Shah


Algorithm and Flowchart Examples
Start
Algorithm: Find Factorial of N.
Step 1.) BEGIN Input N
Step 2.) INPUT N
Step 3.) I  1 , Fact  1 {Initialize Fact, and I variables }
I←1
Step 4.) WHILE (I <= N ) DO
Fact←1
Step 5.) Fact  Fact * I
Step 6.) II+1 {Next Number }
Step 7.) END-WHILE No
I≤N?
Step 8.) Print “Factorial is ” , Fact
Step 9.) END
Yes
Fact←Fact * I
I←I + 1

Print Fact

Stop

Algorithm: Find Power (X, N) Start


Step 1.) BEGIN
Step 2.) INPUT X, N Input X, N
Step 3.) I  1 , Power  1 {Initialize Fact, and I variables }
Step 4.) WHILE (I <= N ) DO I←1
Step 5.) Power  Power * X Power←1
Step 6.) II+1 {Next Number }
Step 7.) END-WHILE
No
Step 8.) Print “Power is ”, Power
I≤N?
Step 9.) END

Yes
Power←Power * X
I←I + 1

Print Power

Stop
Algorithm: Find Total marks of 5 subjects for 10 students
Step 1.) BEGIN
Step 2.) REPEAT for I = 1,2,3, ...,10 upto Step 8
Step 3.) Sum  0 { Marks total of Student I is initialize 0}
Step 4.) REPEAT for J = 1,2,. . , 5 upto Step 7
Step 5.) INPUT Mark
Step 6.) Sum  Sum + Mark {Add marks to the sum}
Step 7.) Print “Total Marks of Student “, I, “ is ”, Sum
Step 8.) END

Prof Monika Shah


Algorithm and Flowchart Examples

Start

For I = 1 to 10

sum←0

For J = 1 to 5

Read mark

sum←sum+mark

Next J

Print “Total marks”, sum

Next I

Stop

EXAMPLE OF ARRAY BASED ALGORITHMS

Algorithm: Sort Elements of an Array in Ascending order

Input :A {An Array of N elements}


N {size of an Array }

Step 1.) BEGIN

Step 2.) REPEAT for I = 1 to N-1 upto Step 3

Step 3.) REPEAT for J = 1 to N - I upto Step 4

Step 4.) IF ( A[J] > A[J+1]) Then {If no is greater than next number}
Begin {Exchange data}
temp  A[J]
A[J]  A[J+1]
A[J+1]  temp
End

Step 5.) END

Prof Monika Shah


Algorithm and Flowchart Examples

Start

For I = 1 to N- 1

For J = 1 to N - 1

No
A[J]>A[J+1]?

Yes
temp←A[J]
A[J]←A[J+1]
A[J+1]←temp

Next J

Next I

Stop
Start
Algorithm: Convert Given Decimal Number to Binary format
Input : N {Given Decimal Number} I←1
Step 1.) BEGIN
Step 2.) I1
Step 3.) WHILE ( N >0) DO N > 0? No
Step 4.) A[I]  N mod 2
Step 5.) NN/2 Yes
Step 6.) I I+1 {Next I} A[I[]←N mod 2
Step 7.) END-WHILE N←N/2
Step 8.) I I–1 {Last Number where data is stored} I←I+1
Step 9.) WHILE (I>=0) DO {Print Number in reverse order}
Step 10.) Print A[I]
Step 11.) II–1 {Previous I} I←I - 1
Step 12.) END-WHILE
Step 13.) END No
I≥0?
Note : For Decimal to octal : Replace 2 with 8
For Decimal to Hexadecimal : Replace 2 with 16. Yes
Print A[I]

I←I - 1

Stop
Prof Monika Shah
Algorithm and Flowchart Examples

Algorithm: Matrix multiplication

Input : A {Two Dimensional Array to store matrix. of order m x n }


B {Two Dimensional Array to store matrix. of order p x q }
C {Two Dimensional Array to store A X B data of order m x q }

Step 1.) BEGIN


Step 2.) IF n  q Then
Begin
Print “Matrix Multiplication can not be performed”
GOTO Step 10
End
Step 3.) REPEAT for I = 1,2,…, m upto Step 4
Step 4.) REPEAT for J = 1,2,……q upto Step 6
Step 5.) C[I, J] 0
Step 6.) REPEAT for K = 1,2, . . . , n upto Step 7
Step 7.) C[I,J]  C[I,J] + A[I,K] * B[K,J]
Step 8.) END

Start

Yes
n≠p?
Print “Multiplication canot be
No performed”
For I = 1 to m

For J = 1 to q

C[I, J]← 0

For K = 1 to n

C[I, J]←C[I, J] + A[I, K]*B[K, J]

Next K

Next J

Next I

Stop

Prof Monika Shah


Algorithm and Flowchart Examples

EXAMPLE OF COUNT BASED ALGORITHMS

Algorithm: Count +ve, -ve, zeroes from given range

Step 1.) BEGIN

Step 2.) INPUT N1, N2 { Input Range bound N1, and N2}

Step 3.) CountP0, CountN0, CountZ0{Initialize +ve, -ve, zero count }

Step 4.) I  N1

Step 5.) WHILE (I  N2 ) DO

Step 6.) IF I = 0 Then


CountZ  CountZ + 1
Else
Begin
IF I >0 Then
CountP  CountP + 1
Else
CountN  CountN + 1
End

Step 7.) I I+1 {Next I}

Step 8.) END-WHILE

Step 9.) Print “Count of Positive Numbers is”, CountP

Step 10.) Print “Count of Negative Numbers is”, CountN

Step 11.) Print “Count of Zero Numbers is”, CountZ

Step 12.) END

Prof Monika Shah


Algorithm and Flowchart Examples

Start

Input N1, N2

countP←0
countN←0
countZ←0

I←N1

No
I ≤ N2 ?

Yes
Yes
I=0?

No countZ←countZ +1

I>0? Yes

No countP←countP +1
count N←count N +1

I←I+1

Print “Positive Nos”, countP

Print “Negative Nos”, countN

Print “Zero Nos”, countZ

Stop

Algorithm: Count odd, even numbers from given range


Step 1.) BEGIN
Step 2.) INPUT N1, N2 { Input Range bound N1, and N2}
Step 3.) CountO0, CountE0{Initialize ood,even zero counter }
Step 4.) I  N1
Step 5.) WHILE (I  N2 ) DO
Step 6.) IF I mod 2 = 0 Then

Prof Monika Shah


Algorithm and Flowchart Examples

CountE  CountE + 1
Else
CountO  CountO + 1
Step 7.) I I+1 {Next I}
Step 8.) END-WHILE
Step 9.) Print “Count of Odd Numbers is”, CountO
Step 10.) Print “Count of Even Numbers is”, CountE
Step 11.) END
Start

Input N1, N2

countO←0
countE←

I←N1

I ≤ N2 ? No

Yes

I mod 2 = 0 ? No

countO←countO +1
Yes
countE←countE +1

Print “Even nos”, countE

Print “odd nos”, countO

Stop
Seriens Based Algorithm
Algorithm: Series 21 +24+27+…. N terms

Step 1.) BEGIN


Step 2.) I ← 21
Step 3.) SUM ← 0
Step 4.) INPUT N
Step 5.) CNT ← 1
Step 6.) WHILE (CNT ≤ N) DO
Step 7.) SUM ← SUM +1
Step 8.) CNT ← CNT + 1

Prof Monika Shah


Algorithm and Flowchart Examples

Step 9.) I← I+3 Start


Step 10.) END WHILE
Step 11.) PRINT SUM I←21
Step 12.) END
sum←0

Input N

cnt←1

cnt ≤ N ? No

Yes
sum←sum + I

cnt←cnt + 1

I←I + 3

Stop

Algorithm : Series 1, 11, 111, … N terms


Start
Step 1.) BEGIN
Input N
Step 2.) INPUT N

I←1
Step 3.) I←1
cnt←1
Step 4.) CNT ← 1
cnt ≤ N No
Step 5.) WHILE (CNT ≤ N) DO
Yes
Step 6.) PRINT I,
Print I
Step 7.) I ← I *10 + 1
I←I*10 + 1
Step 8.) CNT ← CNT + 1
cnt←cnt + 1
Step 9.) END –WHILE
Stop
Step 10.) END

Prof Monika Shah


Algorithm and Flowchart Examples
Start
Algorithm : Series 12 + 22 + 32 + …. N terms
Input N
Steps :
1. BEGIN sum←0
2. INPUT N
3. SUM ← 0 I←1
4. I ←1
5. WHILE (I≤N) DO
6. SUM ← SUM + (I*10) +2 No
7. I ← I +1 I≤ N
8. END-WHILE
9. STOP Yes
sum←sum + (I*10) + 2

I←I +1

Print sum

Stop

Algorithm : 1, 1, 2, 3, 5, .. N terms (Finbonacci)


Start
Steps :
1. BEGIN I←1
2. I ←1 J←1
3. J ←1 cnt←1
4. CNT ← 1
5. INPUT N Input N
6. WHILE (CNT ≤ N) DO
7. K ←I+J
8. PRINT I cnt ≤ n ?
9. I ←J
10. J ←K
11. CNT ← CNT + 1
K←I+J
12. END-WHILE
13. END
Print I

I←J

J←K

cnt ← cnt + 1

Stop
Prof Monika Shah
Algorithm and Flowchart Examples
Start

Algorithm : Generate Series R + R2 + R3 + .. N terms


Input N, R
Steps :
1. BEGIN I←1
2. INPUT N , R sum←0
3. I ←1
4. SUM ← 0 No
5. WHILE (I ≤ N) DO I ≤ N?
6. POWER ← 1, J ← 1
7. WHILE (J ≤ I ) DO Yes
8. POWER ← POWER * R Power←1
9. J ← J +1 J←1
10. END-WHILE
11. SUM ← SUM + POWER
No
12. I ←I+1 J≤ I?
13. END-WHILE
14. PRINT SUM Yes
15. END Power←Power*R

J←J+1

sum←sum + Power

I←I+1

Stop

Algorithm : NCR = N !
(( N  R )! R ! Start

Steps : Input N, R
1. BEGIN
2. READ N, R NFact←Fact (N)
3. N FACT ← FACT (N)
4. NrFACT ← FACT ((N – R))
NRFact←Fact(N-R)
5. RFACT← FACT(R)
6. ANS ← N FACT/(NRFACT * R FACT)
7. PRINT ANS RFact←Fact(R)
8. END.
Ans←NFact/(NRFact*RFact)

Print Ans

Stop

Prof Monika Shah


Algorithm and Flowchart Examples
Fact
Algorithm : Fact Module
Input : N F←1
Steps :
1. BEGIN FACT MODULE I←1
2. F ← 1
3. I ← 1
4. WHILE (I≤N) DO No
5. F ← F*I I≤N?
6. I ←I+1
7. END-WHILE Yes
8. RETURN F. F←F*I

I←I+1

Algorithm : Check for Pelindrom & Number. Return F


Start
Steps :
1. BEGIN Input N
2. INPUT N
3. TEMP ← N
4. REV ← 0 temp←N
5. WHILE (TEMP > 0) DO
6. REM ← TEMP MOD 10 rev←0
7. REV ← REV * 1 0 +REM
8. TEMP ← TEMP/10
9. END-WHILE temp ≥ 0 No
10. IF (N = REV) THEN
11. BEGIN Yes
12. PRINT “Palindrome” rem←temp mod 10
13. END
14. ELSE
15. BEGIN rev←rev *10 + rem
16. PRINT “Not Palindrome”
17. END temp←temp/10
18. END.

N = rev ? No

Yes
Print Print
“Palindrome” “Palindrome”

Stop

Prof Monika Shah


Algorithm and Flowchart Examples

Start
Algorithm : Find Factroal for given range of Number
Input N1, N2
Steps :
1. BEGIN
2. INPUT N1, N2 I←N1
3. I ← N1
4. WHILE (I ≤ N2) DO No
5. F ← FACT (I) I ≤ N2 ?
6. PRINT F
7. I ←I+1 Yes
8. END – WHILE F ←Fact (J)
9. END.
Print F

I←I+1

Stop
Fact
Algorithm : Fact Module
Input : N F←1
Steps :
1. BEGIN FACT MODULE I←1
2. F ← 1
3. I ← 1
4. WHILE (I≤N) DO No
5. F ← F*I I≤N?
6. I ←I+1
7. END-WHILE Yes
8. RETURN F. F←F*I

I←I+1

Return F

Prof Monika Shah


Algorithm and Flowchart Examples

Algorithm : Find Reverse of given Integer number Start

Steps : Input N
1. BEGIN
2. INPUT N rev←0
3. REV ← 0
4. WHILE (N > 0) DO
5. REM ← N MOD 10 No
6. REV ← REV * 10 + N N > 0?
7. N ←N/10
Yes
8. END-WHILE
9. PRINT “Reverse is”, REV rem←N mod 10
10. END. rev←(rev*10) + rem
N←N/10

Print “Reverse is”, rev

Stop

Algorithm : Sum of digits of a number Start

Steps : Input N
1. BEGIN
2. INPUT N sum←0
3. SUM ← 0
4. WHILE (N > 0) DO
5. REM ← N MOD 10 No
6. SUM ← SUM + REM N > 0?
7. N ← N /10
8. END WHILE Yes
9. PRINT SUM rem←N mod 10
10. END. sum←sum + rem
N←N/10

Print sum

Stop

Prof Monika Shah


Algorithm and Flowchart Examples

Start
Algorithm : Conversion from binay to decimal.
Read “Binary String” S
Steps :
1. BEGIN
2. READ BINARY STERING S len←length(s)
3. LEN ← LENGTH (S)
4. D ← 0, I ←0
D←0
5. WHILE (LEN > 0) DO
I←0
6. BIT ← S [LEN]
7. D ← D + (POWER(2, I)*BIT)
8. I ← I +1 No
9. LEN ← LEN – 1 len > 0?
10. END-WHILE
11. PRINT “Equivalne Decimal no is”, D Yes
12. END. bit←s[len]
Note :
Conversion from Octal to Decimal t←Power(2, 1)
Replace 2 with 8
Repalce Binary with Octal.
t←t*bit

D←D+t

I←I+1

len←len - 1

Print D

Stop Power
Module : Power
Input : B : Integer
N : Integer P←1
Output : P : Integer cnt←1
Steps :
1. BEGIN POWER
2. P ← 1, CNT ← 1 cnt ≤ N ? No
3. WHILE (CNT ≤ N ) DO
4. P ← P*R Yes
5. CNT ← CNT +1
6. END-WHILE P←P*B
7. RETURN P.
cnt←cnt+1

Return P
Prof Monika Shah
Algorithm and Flowchart Examples

Algorithm : Foe Given salary of 100 employees count numbe of employees having salary in
following categories
1. Less than 2000
2. Between 2000 to 50000
3. Between 5000 to 10000
4. Above 10000

Algorithm :
Input : salary = Integer Arrar For 100 Employee
Steps :
1. BEGIN
2. I ←1, COUNTA ←0, COUNTB ←0, COUNTC ←0, COUNTD ←0
3. WHILE I ≤100 DO
4. IF SALARY[I] < 2000
5. BEGIN
6. COUNTA ← COUNTA + 1
7. END
8. ELSE
9. BEGIN
10. IF SALARY[I] < 5000
11. BEGIN
12. COUNTB ← COUNTB +1
13. END
14. ELSE
15. BEGIN
16. IF SALARY [I] < 10000
17. BEGIN
18. COUNTC ← COUNTC + 1
19. END
20. ELSE
21. BEGIN
22. COUNTD ← COUNTD + 1
23. END
24. END
25. I ← I +1
26. END-WHILE
27. PRINT “Less than 2000” COUNTA
28. PRINT “Between 2000 and 5000”, COUNTB
29. PRINT “Betweeen 5000 and 10000”, COUNTC
30. PRINT “Above 10000”, COUNTD
31. END.

Prof Monika Shah


Algorithm and Flowchart Examples

Start

I←1
CountA←0
CountB ← 0
CountC ← 0

I ≤ 100 ? No

Yes
salary[I] < 2000 ? No

Yes
CountA←CountA +1
salary[I] < 5000 ? No

Yes
CountB←CountB +1
salary[I] < 10000 ? No

Yes
CountC←CountC +1
CountD←CountD +1

I←I+1

Print “less than 2000”, CountA

Print “Between 2000 and 5000”, CountB

Print “Between 5000 and 10000”, CountC

Print “Above 10000”, CountD

Stop

***
Prof Monika Shah

You might also like