0% found this document useful (0 votes)
16 views23 pages

Fundamentals of CS Section 3

The document outlines the fundamentals of algorithms, including their definition, structure, and basic rules for writing them. It provides examples of algorithms for various tasks, such as summing two numbers, converting Celsius to Fahrenheit, and determining if a number is positive or negative. Additionally, it covers conditional statements using IF-ELSE structures to execute operations based on specific conditions.

Uploaded by

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

Fundamentals of CS Section 3

The document outlines the fundamentals of algorithms, including their definition, structure, and basic rules for writing them. It provides examples of algorithms for various tasks, such as summing two numbers, converting Celsius to Fahrenheit, and determining if a number is positive or negative. Additionally, it covers conditional statements using IF-ELSE structures to execute operations based on specific conditions.

Uploaded by

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

FUNDAMENTALS

OF
COMPUTER SCIENCE
SECTION 3: ALGORITHMS
Algorithms
• Algorithm: a set of instructions that describe how to solve a
problem.

Input Algorithm Output


Algorithms
Algorithm Rules: Basics
• Algorithm writing rules:
• START represents the starting point of an algorithm
• READ is used to read input.
• To declare variables or perform operations on variables,
• Write the variable name followed by ‘=‘ followed by the new
value.
• PRINT is used to display output.
• END represents the end of an algorithm
Algorithm Rules: Basics
• Example: Write an algorithm to read two numbers x and y and print
their sum.
Algorithm Rules: Basics
• Example: Write an algorithm to read two numbers x and y and print
their sum.

• Answer:
1. START
2. READ x, y
3. sum = x + y
4. PRINT sum
5. END
Algorithm Rules: Basics
• Example 2: Write an algorithm to read a temperature C in Celsius
and convert it to Fahrenheit.
Algorithm Rules: Basics
• Example 2: Write an algorithm to read a temperature C in Celsius
and convert it to Fahrenheit.

• Answer:
1. START 𝑭𝒂𝒉𝒓𝒆𝒏𝒉𝒆𝒊𝒕 = 𝑪𝒆𝒍𝒔𝒊𝒖𝒔 ∗
𝟗
+ 𝟑𝟐
𝟓
2. READ c
3. f = c * (9/5) + 32
4. PRINT f
5. END
Algorithm Rules: Basics
• Try yourself: Write an algorithm to read a number and flip its sign
(change from positive to negative or from negative to positive).
Algorithm Rules: Basics
• Try yourself: Write an algorithm to read a number and flip its sign
(change from positive to negative or from negative to positive).

• Answer:
1. START
2. READ n
3. n = n * -1
4. PRINT n
5. END
Algorithm Rules: Conditional
• To perform a set of operation conditionally, we use the IF
statement.
• IF statement syntax:
1. IF condition
2. operation 1
Executed only if condition is true
3. operation 2..
4. ELSE
5. operation 4
Executed only if condition is false
6. operation 5..
7. ENDIF
Algorithm Rules: Conditional
• Example 1: Write an algorithm to read two numbers a, b and print
which number is larger than the other.
Algorithm Rules: Conditional
• Example 1: Write an algorithm to read two numbers a, b and print which
number is larger than the other.

• Answer:
1. START
2. READ a,b
3. IF a > b
4. PRINT “a is larger than b”
5. ELSE
6. PRINT “b is larger than a”
7. ENDIF
8. END
Algorithm Rules: Conditional
• Example 1: Write an algorithm to read two numbers a, b and print which
number is larger than the other.

• Answer:
1. START What is the output of the
2. READ a,b algorithm if
3. IF a > b 1)a = 7, b = 5 ?
4. PRINT “a is larger than b” 2)a = 3, b = 6 ?
5. ELSE
3)a = 2, b = 2 ?
6. PRINT “b is larger than a”
7. ENDIF
8. END
Algorithm Rules: Conditional
• Example 1: Write an algorithm to read two numbers a, b and print which
number is larger than the other.

• Answer:
1. START What is the output of the
2. READ a,b algorithm if
3. IF a > b 1)a = 7, b = 5 ? “a is larger than b”
4. PRINT “a is larger than b” 2)a = 3, b = 6 ? “b is larger than a”
5. ELSE
3)a = 2, b = 2 ? “b is larger than a”
6. PRINT “b is larger than a”
7. ENDIF
8. END
Algorithm Rules: Conditional
• Example 2: Given a student’s grade g, write an algorithm to
determine whether the student passed or failed (passing grade is
50).
Algorithm Rules: Conditional
• Example 2: Given a student’s grade g, write an algorithm to
determine whether the student passed or failed (passing grade is
50).
1. START
2. READ g
• Answer: 3. result = “”
4. IF g ≥ 50
5. result = “Pass”
6. ELSE
7. result = “Fail”
8. ENDIF
9. PRINT result
10. END
Algorithm Rules: Conditional
• Example 2: Given a student’s grade g, write an algorithm to
determine whether the student passed or failed (passing grade is
50).
1. START
2. READ g What is the output of the
• Answer: 3. result = “”
algorithm if
4. IF g ≥ 50 1)g = 75 ?
5. result = “Pass” 2)g = 40 ?
6. ELSE 3)g = 50 ?
7. result = “Fail”
8. ENDIF
9. PRINT result
10. END
Algorithm Rules: Conditional
• Example 2: Given a student’s grade g, write an algorithm to
determine whether the student passed or failed (passing grade is
50).
1. START
2. READ g What is the output of the
• Answer: 3. result = “”
algorithm if
4. IF g ≥ 50 1)g = 75 ? “Pass”
5. result = “Pass” 2)g = 40 ? “Fail”
6. ELSE 3)g = 50 ? “Pass”
7. result = “Fail”
8. ENDIF
9. PRINT result
10. END
Algorithm Rules: Conditional
• Try yourself: Write an algorithm that reads a number n (positive or
negative) and prints the absolute value of the number |n|.
Algorithm Rules: Conditional
• Try yourself: Write an algorithm that reads a number n (positive or
negative) and prints the absolute value of the number |n|.

• Answer: 1. START
2. READ n
3. IF n < 0
4. n = n * -1
5. ENDIF
6. PRINT n
7. END
Algorithm Rules: Conditional
• Try yourself: Write an algorithm that reads a number n (positive or
negative) and prints the absolute value of the number |n|.
What is the output of the
• Answer: 1. START
algorithm if
2. READ n 1)n = -5 ?
3. IF n < 0 2)n = 2 ?
4. n = n * -1
5. ENDIF
6. PRINT n
7. END
Algorithm Rules: Conditional
• Try yourself: Write an algorithm that reads a number n (positive or
negative) and prints the absolute value of the number |n|.
What is the output of the
• Answer: 1. START
algorithm if
2. READ n 1)n = -5 ? 5
3. IF n < 0 2)n = 2 ? 2
4. n = n * -1
5. ENDIF
6. PRINT n
7. END

You might also like