ALGORITHMS
Pseudocode
• Pseudocode is very similar to everyday English.
• First produce a general algorithm (one can use pseudocode)
• Refine the algorithm successively to get step by step detailed algorithm that is very
close to a computer language.
• Pseudocode is an informal language that helps programmers develop algorithms.
Pseudocode: Example No 1
Question: Write an algorithm to find the sum of three given
numbers?
Pseudocode:
• Input a set of 3 numbers
• Calculate their Sum
• Print Sum
ALGORITHMS
• In simple terms, an algorithm is a series of instructions to solve a problem
(complete a task)
• A typical programming task can be divided into two phases:
– Problem solving phase
• produce an ordered sequence of steps that describe solution of problem
• this sequence of steps is called an algorithm
– Implementation phase
• implement the program in some programming language
Role of Algorithms
Hardware
Software
Programming
Program
Algorithm
• Algorithm: A set of steps that defines how a task is performed
• Program: A representation of an algorithm
• Programming: The process of developing a program
• Software: Programs and algorithms
• Hardware: Equipment used to input information (data) and output the results of the programs,
algorithms.
5
Algorithm (Example No 1)
Question: Write an algorithm to find the sum of three given
numbers?
Algorithm:
Step 1: Input N1,N2,N3
Step 2: Sum=N1+N2+N3
Step 3: Print Sum
Algorithm (Example No 2)
Question: Write an algorithm to find the sum and product of the
two given numbers?
Algorithm:
Step 1: Input Num1,Num2
Step 2: Sum=Num1+Num2
Step 3: Product=Num1*Num2
Step 4: Print Sum
Step 5: Print Product
Algorithm (Example No 3)
Question: Find the sum and average of three given numbers?
Algorithm:
Step 1: Input Num1,Num2,Num3
Step 2: Sum=Num1+Num2+Num3
Step 3: Average=(Num1+Num2+Num3)/3
Step 4: Print Sum
Step 5: Print Average
Pseudocode
Question: Write an algorithm to determine a student’s final grade
and indicate whether it is passing or failing. The final grade is
calculated as the average of four marks.
Pseudocode:
• Input a set of 4 marks
• Calculate their average by summing and dividing by 4
• if average is below 50
Print “FAIL”
else
Print “PASS”
Algorithm
Question: Write an algorithm to determine a student’s final grade and
indicate whether it is passing or failing. The final grade is calculated as the
average of four marks.
Algorithm:
Step 1: Input M1,M2,M3,M4
Step 2: GRADE =(M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
Algorithm
Question: Eligibility for making license
Algorithm:
Step 1: Input Current year , Birth year
Step 2: res = Current year – Birth Year
Step 3: If (res > 18)
Print “eligible “
else
Print “Not eligible “