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

Algorithm, Pseudocode and Flochart

Uploaded by

azzanmir2010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Algorithm, Pseudocode and Flochart

Uploaded by

azzanmir2010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Algorithm and

pseudocode
Algorithm and pseudocode
Algorithm: Provides a high-level description of the steps to solve a particular
problem.
Pseudocode: A more detailed yet still simplified version of the algorithm,
showing how you might write it in a way that is close to actual programming
code.
 Combination of programming language and natural language.
Activity 1
Write an algorithm for the sum of any two numbers?
Activity 2
Write an algorithm for the difference of any two numbers?
Activity 3
Write an algorithm for the sum of any three numbers?
Activity 4
Write an algorithm and pseudocode for the quotient of any two
numbers?
Algorithm and pseudocode
• Start • Start
• Input the numbers x, y • Input x, y
• Compute the quotient, z=x/y • Z=x/y
• If y is not equal to 0 • If y <> 0
• Print z • Print z
• Else • Else
• Print “undefined!” • Print “undefined!”
• End • Endif
• End
Activity 5
Write an algorithm for the area of a rectangle?
Example
Algorithm Pseudocode
1. Start Start
2. Input: Read/Input the two numbers, x and y Input a
3. Process: Compute the sum of x and y. Input b
Let the result be sum = x + y sum = a + b
4. Output: Display the result, sum Output sum

5. End End
Activity 6
Write an algorithm and pseudocode for the difference of any two
numbers?
Draw a flowchart.
Activity 7
Write an algorithm and pseudocode that can find area of a circle?
Draw a flowchart.
conditional statements (like if, else if, and else)

Example: Checking if a Number is Positive, Negative, or Zero


Algorithm and Pseudocode
Algorithm Pseudocode
Start START
Input a number INPUT number
If the number is greater than 0, print IF number > 0 THEN
"Positive“ PRINT "Positive"
Else if the number is less than 0, print ELSE IF number < 0 THEN
"Negative“ PRINT "Negative"
Else, print "Zero“ ELSE
PRINT "Zero"
End ENDIF
END
Activity 8
Checking if the student is grade 8 based on age
Activity 9
Deciding if a Person Can Vote Based on Age
Algorithm and Pseudocode
Algorithm Pseudocode
Start START
Input age INPUT age
If age is greater than or equal to 18, print IF age >= 18 THEN
"You can vote“ PRINT "You can vote"
Else, print "You cannot vote“ ELSE
End PRINT "You cannot vote"
ENDIF
END
Activity 10
Finding the Largest of Three Numbers
Algorithm and Pseudocode
Algorithm Pseudocode
Start START
 Input three numbers: a, b, and c INPUT a, b, c
 If a is greater than both b and c, IF a > b AND a>c THEN
 print "a is the largest“  PRINT "a is the largest"
 Else if b is greater than both a and c, ELSE IF b > a AND b > c THEN
 print "b is the largest“  PRINT "b is the largest"
 Else if c is greater than a and b,
ELSE
 print "c is the largest“
 PRINT "c is the largest"
Else ENDIF
 Print “all are equal”
END
End
Activity 11
Check if a Number is Even, Odd or zero
Algorithm and pseudocode
Algorithm Pseudocde
Start Start
Input the number, x Input x
If x is divisible by 2 and different from If x mod 2 = 0 and x <> 0,
0,  Print “its even”
 Print “x is even”
Else if x mod 2 = 1,
Elseif x is not divisible 2
 Print “its odd”
 Print “x is odd”
Else Else
 Print “x is zero”  Print “x is zero”
endif Endif
end end
Activity 12
Find the Largest of Two Numbers
Example pseudocode algorithms
How do I calculate the ingredients needed for a given number of cakes?

OUTPUT "How many cakes do you need to make?" IF eggs < 1 THEN
INPUT numberCakes OUTPUT "You will need 1 egg"
flour = 10 * numberCakes ELSEIF eggs < 2 THEN
sugar = 10 * numberCakes OUTPUT "You will need 2 eggs"
butter = 2 * numberCakes ELSE
eggs = 0.1 * numberCakes OUTPUT "You will need 3 eggs"
OUTPUT "You will need" & flour & "grams of flour" ENDIF
OUTPUT "You will need" & butter & "grams of
butter"
OUTPUT "You will need" & sugar & "grams of
sugar"
Cont…
A user enters their score and the program tells the user whether they are in first, second or
third place, or not placed in this contest.

first = 80 ELSEIF score > second THEN


second = 70 OUTPUT "You are in second place"
third = 60 third = second
OUTPUT "What is your score?“ second = score
INPUT score ELSEIF score > third THEN
IF score > first THEN OUTPUT "You are in third place"

OUTPUT "You are in first place" third = score

third = second ELSE


second = first OUTPUT "You were not placed this time"

first = score ENDIF


Iteration
An iteration refers to the process of repeating a set of instructions or a sequence of
operations in programming and algorithms.
It is commonly used in loops, where a block of code is executed multiple times until a
certain condition is met.
Example
FOR number_of_loops = 0 UNTIL number_of_loops > 10
Sit on the floor
Clap your hands
Stand up
Jump once
Spin around twice
Add 1 to number_of_loops
Go back to FOR
Activity 1
An algorithm that outputs the numbers 1 to 10 (inclusive).

FOR count = 1 TO 10
OUTPUT count
NEXT count
Activity 2
Print Numbers from 10 to 1

FOR count FROM 10 DOWNTO 1 DO count = 10


OUTPUT count WHILE count >= 1 DO
END FOR OUTPUT count
count = count – 1
END WHILE
Activity 3
An algorithm takes a number as
input and outputs the times table
for that number up to 12 times the
number.
For example, if 3 is input, the output
is 3 6 9 12 15 18 21 24 27 30 33 36. INPUT number
count = 1
INPUT number WHILE count <= number DO
FOR count = 1 TO number OUTPUT count * number
OUTPUT count * number count = count + 1
NEXT count END WHILE
Activity 4
Create an algorithm that accepts two numbers three times and
displays their product after each input.
Activity 5
Write an algorithm that takes 4 numbers and calculates the sum of
all numbers
Sum=0
For count=1 to 4
 Input num
 Sum=sum+num
output sum
Activity 7
Create an algorithm that can find the sum of all the first n
Natural numbers. For example, if n=5, the sum would be
1+2+3+4+5=15
sum=0
Input n
For count=1 to n
sum=sum+count
Endfor
Output sum
Activity 8
Create an algorithm that calculates the sum of all natural numbers
between m and n, inclusive. For example, if m=2 and n=5, then the
sum would be 2+3+4+5=14
sum=0
Input m,n
For count=m to n
sum=sum+count
Endfor
Output sum
Activity 9
Write an algorithm to find the sum of the first N even numbers
using a For loop. The key idea is that the first N even numbers are
2, 4, 6, 8, ..., 2N
sum = 0
Input n
FOR count = 1 TO n
sum = sum + (2 * count)
ENDFOR
OUTPUT sum
Activity 10
Write an algorithm to find the sum of the first N odd numbers
using a For loop. The first N odd numbers are 1, 3, 5, 7, ..., (2N - 1)
sum = 0
Input n
FOR count = 1 TO n
sum = sum + (2 * count-1)
ENDFOR
OUTPUT sum
Activity 11
An algorithm that asks the user to enter a number, then outputs this quantity
of symbols, for example it outputs a number of stars.

OUTPUT "Enter a number"


INPUT number
FOR count = 1 TO number
OUTPUT "*"
NEXT count
Activity 12
An algorithm that takes 20 numbers as input from the user and
outputs the average (mean).
total=0
FOR count = 1 TO 20
Input number
total = total + number
NEXT count
Average= total / 20
OUTPUT “The average is” & Average
Activity 13
An algorithm that takes three numbers as input from the user and only stores
the largest.
largest = -9999
FOR count = 1 TO 3
INPUT number
IF number > largest THEN
largest = number
ENDIF
NEXT count
OUTPUT largest
Activity 14
An algorithm that takes 100 numbers as
input and adds together the largest and
smallest number input, then divides
them by 2.

largest = -9999 IF number < smallest THEN


smallest = 9999 smallest = number
FOR count = 0 TO 99 ENDIF
INPUT number NEXT count
IF number > largest THEN added = largest + smallest
largest = number answer = added / 2
ENDIF OUTPUT answer
Flowchart to check whether a non zero number is
even/odd
Flowchart for a sum of first N natural numbers
Flowchart for a factorial of a number

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy