SlideShare a Scribd company logo
Professor Dr. M. Ismail Jabiullah
Professor
Department of CSE
Daffodil International University
Bangladesh
Computer Fundamentals
Examples of Algorithm, Pseudo code
and Flowchart
Example of Algorithm, Pseudo Code
and Flowchart
Topics
▪ Problem Analysis
▪ Algorithm
▪ Pseudo code
▪ Examples
▪ Flowcharts
▪ Examples of Flowcharts
Algorithm and Pseudo Code
Example - 1
Problem 1: Given a list of positive numbers, return the largest number on the list.
Inputs: A list L of positive numbers. This list must contain at least one number.
(Asking for the largest number in a list of no numbers is not a meaningful
question.)
Outputs: A number n, which will be the largest number of the list.
Algorithm:
1. Set max to 0.
2. For each number x in the list L, compare
it to max.
3. If x is larger, set max to x.
4. max is now set to the largest number in
the list.
Program Segment
def find_max (L):
max = 0
for x in L:
if x > max:
max = x
return max
Algorithm and Pseudo Code
Example - 2
Problem 2: A Recursive Version of find_max()
Inputs: A list L of positive numbers. This list must contain at least one number.
(Asking for the largest number in a list of no numbers is not a meaningful
question.)
Outputs: A number n, which will be the largest number of the list.
There can be many different algorithms for
solving the same problem. Here’s an
alternative algorithm for find_max():
1. If L is of length 1, return the first item of L.
2. Set v1 to the first item of L.
3. Set v2 to the output of
performing find_max() on the rest of L.
4. If v1 is larger than v2, return v1.
Otherwise, return v2.
Program Segment
def find_max (L):
if len(L) == 1: return L[0]
v1 = L[0]
v2 = find_max(L[1:])
if v1 > v2: return v1
else: return v2
Algorithm and Pseudo Code
Example - 3
Problem 2: Find the sum two numbers N and M.
The procedure is:
1. Enter the two numbers in the variables N and M.
2. Sum them and save the result in the variable sum.
3. Output the result
Program Segment
def find_max (L):
if len(L) == 1: return L[0] v1 = L[0]
v2 = find_max(L[1:])
if v1 > v2: return v1
else: return v2
Algorithm
Algorithm 1: Add two numbers entered by the user
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
Algorithm
Algorithm 2: Find the largest number among three numbers
Step 1: Start
Step 2: Declare variables a, b and c.
Step 3: Read variables a, b and c.
Step 4: If a > b If a > c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b > c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop
Algorithm
Algorithm 3: Find Root of the quadratic equation ax2 + bx + c = 0
Step 1: Start
Step 2: Declare variables a, b, c, D, x1, x2, rp and ip;
Step 3: Calculate discriminant D ← b2-4ac
Step 4: If D ≥ 0
r1 ← (-b+√D)/2a
r2 ← (-b-√D)/2a
Display r1 and r2 as roots.
Else
Calculate real part and imaginary part
rp ← -b/2a
ip ← √(-D)/2a
Display rp+j(ip) and rp-j(ip) as roots
Step 5: Stop
Algorithm
Algorithm 4: Find the factorial of a number
Step 1: Start
Step 2: Declare variables n, factorial and i.
Step 3: Initialize variables factorial ← 1 i ← 1
Step 4: Read value of n
Step 5: Repeat the steps until i = n
5.1: factorial ← factorial*i
5.2: i ← i+1
Step 6: Display factorial
Step 7: Stop
Algorithm
Algorithm 5: Check whether a number is prime or not
Step 1: Start
Step 2: Declare variables n, i, flag.
Step 3: Initialize variables flag ← 1 i ← 2
Step 4: Read n from the user.
Step 5: Repeat the steps until i=(n/2)
5.1 If remainder of n÷i equals 0 flag ← 0 Go to step 6
5.2 i ← i+1
Step 6: If flag = 0 Display n is not prime else Display n is prime
Step 7: Stop
Algorithm
Algorithm 6: Find the Fibonacci series till the term less than 1000
Step 1: Start
Step 2: Declare variables first_term,second_term and temp.
Step 3: Initialize variables first_term ← 0 second_term ← 1
Step 4: Display first_term and second_term
Step 5: Repeat the steps until second_term ≤ 1000
5.1: temp ← second_term
5.2: second_term ← second_term + first_term
5.3: first_term ← temp
5.4: Display second_term
Step 6: Stop
Algorithm
Algorithm 5: create an algorithm to check whether a number is positive or
negative.
Step 1: Start
Step 2: Print “Give any number”.
Step 3: Read num
Step 4: If (num==0) Print “You Entered 0”
Step 5: If (num>0) Print “You Entered a positive number”
Step 6: If (num<0) Print “You Entered a Negative number”
Step 7: Stop
Pseudo code
Pseudo code 1: create an algorithm to check whether a number is positive or
negative.
1. If student's grade is greater than or equal to 60
2. Print "passed"
3. else
4. Print "failed"
Pseudo code
Pseudo code 2: create an algorithm to check whether a number is positive or
negative.
1. Set total to zero
2. Set grade counter to one
3. While grade counter is less than or equal to ten
4. Input the next grade
5. Add the grade into the total
6. Set the class average to the total divided by ten
7. Print the class average.
Pseudo code
Pseudo code 3: create an algorithm to check whether a number is positive or
negative.
1. Initialize total to zero
2. Initialize counter to zero
3. Input the first grade
4. while the user has not as yet entered the sentinel
5. add this grade into the running total
6. add one to the grade counter
7. input the next grade (possibly the sentinel)
8. if the counter is not equal to zero
9. set the average to the total divided by the counter
10.print the average
11.else
12.print 'no grades were entered'
Pseudo code
Pseudo code 4: create an algorithm to check whether a number is positive or
negative.
1. initialize passes to zero
2. initialize failures to zero
3. initialize student to one
4. while student counter is less than or equal to ten
5. input the next exam result
6. if the student passed add one to passes
else add one to failures
add one to student counter
7. print the number of passes
8. print the number of failures
9. if eight or more students passed print "raise tuition"
Flowcharts
Medical Service
This is a hospital flowchart example that shows how clinical cases shall be processed.
This flowchart uses decision shapes intensively in representing alternative flows.
Flowcharts
Sum of Two Numbers
Read two numbers 529 and 256 from the
keyboard and find and display the sum of
the two numbers.
Flowcharts
Profit and Loss
Calculations
Read Income and Cost,
and Calculate the Profit
and Loss.
Finally Show them.
Thanks

More Related Content

PDF
Introduction to programming : flowchart, algorithm
PPT
algorithm_practice.pptzkkxiqdhiwdowhdohwod2hodh2odh2o
PPT
01 Algorithms And Flowcharts.ppt
PPTX
Algorithmsandflowcharts1
PPT
Algorithmsandflowcharts1
PPT
Algorithmsandflowcharts1
PPSX
Algorithm and flowchart
PDF
ALGORITHMS AND FLOWCHARTS
Introduction to programming : flowchart, algorithm
algorithm_practice.pptzkkxiqdhiwdowhdohwod2hodh2odh2o
01 Algorithms And Flowcharts.ppt
Algorithmsandflowcharts1
Algorithmsandflowcharts1
Algorithmsandflowcharts1
Algorithm and flowchart
ALGORITHMS AND FLOWCHARTS

Similar to L- 14. 0 Algorithm_Flowchart_Example.pdf (20)

PDF
Flowcharts. Algorithms and pseudo codepdf
PPTX
Algorithms-Flowcharts for programming fundamental
PDF
2021 CSE031.Lecture 6.Flow_Charts_Pseudocode.pptx.pdf
PPT
Algorithmsandflowcharts1
PPT
Algorithms and flowcharts1
PPT
Algorithmsandflowcharts1
PPT
Best Techniques To Design Programs - Program Designing Techniques
PPTX
Problem Solving - Introduction to Flowcharts.pptx
PPTX
Draw the flowchart of the above algorithm.pptx
PPTX
2. Algorithms Representations (C++).pptx
PPT
3 algorithm-and-flowchart
PDF
Algorithms and flowcharts
PPT
UNIT- 3-FOC.ppt
PPT
Proble, Solving & Automation
PPTX
INTROTOPROBLEMSOLVING.pptxINTROTOPROBLEMSOLVING.pptx
PDF
4. algorithm
PPTX
Algorithm for computational problematic sit
PPTX
s-INTRODUCTION TO PROBLEM SOLVING PPT.pptx
PDF
Pseudocode By ZAK
PPTX
Flowchart and algorithm
Flowcharts. Algorithms and pseudo codepdf
Algorithms-Flowcharts for programming fundamental
2021 CSE031.Lecture 6.Flow_Charts_Pseudocode.pptx.pdf
Algorithmsandflowcharts1
Algorithms and flowcharts1
Algorithmsandflowcharts1
Best Techniques To Design Programs - Program Designing Techniques
Problem Solving - Introduction to Flowcharts.pptx
Draw the flowchart of the above algorithm.pptx
2. Algorithms Representations (C++).pptx
3 algorithm-and-flowchart
Algorithms and flowcharts
UNIT- 3-FOC.ppt
Proble, Solving & Automation
INTROTOPROBLEMSOLVING.pptxINTROTOPROBLEMSOLVING.pptx
4. algorithm
Algorithm for computational problematic sit
s-INTRODUCTION TO PROBLEM SOLVING PPT.pptx
Pseudocode By ZAK
Flowchart and algorithm
Ad

Recently uploaded (20)

PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
master seminar digital applications in india
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
master seminar digital applications in india
Weekly quiz Compilation Jan -July 25.pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Pharma ospi slides which help in ospi learning
Orientation - ARALprogram of Deped to the Parents.pptx
Chinmaya Tiranga quiz Grand Finale.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Microbial disease of the cardiovascular and lymphatic systems
Module 4: Burden of Disease Tutorial Slides S2 2025
Complications of Minimal Access Surgery at WLH
Microbial diseases, their pathogenesis and prophylaxis
VCE English Exam - Section C Student Revision Booklet
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
human mycosis Human fungal infections are called human mycosis..pptx
O5-L3 Freight Transport Ops (International) V1.pdf
Ad

L- 14. 0 Algorithm_Flowchart_Example.pdf

  • 1. Professor Dr. M. Ismail Jabiullah Professor Department of CSE Daffodil International University Bangladesh Computer Fundamentals Examples of Algorithm, Pseudo code and Flowchart
  • 2. Example of Algorithm, Pseudo Code and Flowchart Topics ▪ Problem Analysis ▪ Algorithm ▪ Pseudo code ▪ Examples ▪ Flowcharts ▪ Examples of Flowcharts
  • 3. Algorithm and Pseudo Code Example - 1 Problem 1: Given a list of positive numbers, return the largest number on the list. Inputs: A list L of positive numbers. This list must contain at least one number. (Asking for the largest number in a list of no numbers is not a meaningful question.) Outputs: A number n, which will be the largest number of the list. Algorithm: 1. Set max to 0. 2. For each number x in the list L, compare it to max. 3. If x is larger, set max to x. 4. max is now set to the largest number in the list. Program Segment def find_max (L): max = 0 for x in L: if x > max: max = x return max
  • 4. Algorithm and Pseudo Code Example - 2 Problem 2: A Recursive Version of find_max() Inputs: A list L of positive numbers. This list must contain at least one number. (Asking for the largest number in a list of no numbers is not a meaningful question.) Outputs: A number n, which will be the largest number of the list. There can be many different algorithms for solving the same problem. Here’s an alternative algorithm for find_max(): 1. If L is of length 1, return the first item of L. 2. Set v1 to the first item of L. 3. Set v2 to the output of performing find_max() on the rest of L. 4. If v1 is larger than v2, return v1. Otherwise, return v2. Program Segment def find_max (L): if len(L) == 1: return L[0] v1 = L[0] v2 = find_max(L[1:]) if v1 > v2: return v1 else: return v2
  • 5. Algorithm and Pseudo Code Example - 3 Problem 2: Find the sum two numbers N and M. The procedure is: 1. Enter the two numbers in the variables N and M. 2. Sum them and save the result in the variable sum. 3. Output the result Program Segment def find_max (L): if len(L) == 1: return L[0] v1 = L[0] v2 = find_max(L[1:]) if v1 > v2: return v1 else: return v2
  • 6. Algorithm Algorithm 1: Add two numbers entered by the user Step 1: Start Step 2: Declare variables num1, num2 and sum. Step 3: Read values num1 and num2. Step 4: Add num1 and num2 and assign the result to sum. sum←num1+num2 Step 5: Display sum Step 6: Stop
  • 7. Algorithm Algorithm 2: Find the largest number among three numbers Step 1: Start Step 2: Declare variables a, b and c. Step 3: Read variables a, b and c. Step 4: If a > b If a > c Display a is the largest number. Else Display c is the largest number. Else If b > c Display b is the largest number. Else Display c is the greatest number. Step 5: Stop
  • 8. Algorithm Algorithm 3: Find Root of the quadratic equation ax2 + bx + c = 0 Step 1: Start Step 2: Declare variables a, b, c, D, x1, x2, rp and ip; Step 3: Calculate discriminant D ← b2-4ac Step 4: If D ≥ 0 r1 ← (-b+√D)/2a r2 ← (-b-√D)/2a Display r1 and r2 as roots. Else Calculate real part and imaginary part rp ← -b/2a ip ← √(-D)/2a Display rp+j(ip) and rp-j(ip) as roots Step 5: Stop
  • 9. Algorithm Algorithm 4: Find the factorial of a number Step 1: Start Step 2: Declare variables n, factorial and i. Step 3: Initialize variables factorial ← 1 i ← 1 Step 4: Read value of n Step 5: Repeat the steps until i = n 5.1: factorial ← factorial*i 5.2: i ← i+1 Step 6: Display factorial Step 7: Stop
  • 10. Algorithm Algorithm 5: Check whether a number is prime or not Step 1: Start Step 2: Declare variables n, i, flag. Step 3: Initialize variables flag ← 1 i ← 2 Step 4: Read n from the user. Step 5: Repeat the steps until i=(n/2) 5.1 If remainder of n÷i equals 0 flag ← 0 Go to step 6 5.2 i ← i+1 Step 6: If flag = 0 Display n is not prime else Display n is prime Step 7: Stop
  • 11. Algorithm Algorithm 6: Find the Fibonacci series till the term less than 1000 Step 1: Start Step 2: Declare variables first_term,second_term and temp. Step 3: Initialize variables first_term ← 0 second_term ← 1 Step 4: Display first_term and second_term Step 5: Repeat the steps until second_term ≤ 1000 5.1: temp ← second_term 5.2: second_term ← second_term + first_term 5.3: first_term ← temp 5.4: Display second_term Step 6: Stop
  • 12. Algorithm Algorithm 5: create an algorithm to check whether a number is positive or negative. Step 1: Start Step 2: Print “Give any number”. Step 3: Read num Step 4: If (num==0) Print “You Entered 0” Step 5: If (num>0) Print “You Entered a positive number” Step 6: If (num<0) Print “You Entered a Negative number” Step 7: Stop
  • 13. Pseudo code Pseudo code 1: create an algorithm to check whether a number is positive or negative. 1. If student's grade is greater than or equal to 60 2. Print "passed" 3. else 4. Print "failed"
  • 14. Pseudo code Pseudo code 2: create an algorithm to check whether a number is positive or negative. 1. Set total to zero 2. Set grade counter to one 3. While grade counter is less than or equal to ten 4. Input the next grade 5. Add the grade into the total 6. Set the class average to the total divided by ten 7. Print the class average.
  • 15. Pseudo code Pseudo code 3: create an algorithm to check whether a number is positive or negative. 1. Initialize total to zero 2. Initialize counter to zero 3. Input the first grade 4. while the user has not as yet entered the sentinel 5. add this grade into the running total 6. add one to the grade counter 7. input the next grade (possibly the sentinel) 8. if the counter is not equal to zero 9. set the average to the total divided by the counter 10.print the average 11.else 12.print 'no grades were entered'
  • 16. Pseudo code Pseudo code 4: create an algorithm to check whether a number is positive or negative. 1. initialize passes to zero 2. initialize failures to zero 3. initialize student to one 4. while student counter is less than or equal to ten 5. input the next exam result 6. if the student passed add one to passes else add one to failures add one to student counter 7. print the number of passes 8. print the number of failures 9. if eight or more students passed print "raise tuition"
  • 17. Flowcharts Medical Service This is a hospital flowchart example that shows how clinical cases shall be processed. This flowchart uses decision shapes intensively in representing alternative flows.
  • 18. Flowcharts Sum of Two Numbers Read two numbers 529 and 256 from the keyboard and find and display the sum of the two numbers.
  • 19. Flowcharts Profit and Loss Calculations Read Income and Cost, and Calculate the Profit and Loss. Finally Show them.