Computer Fundamentals
Slide 2
Algorithm
• A step-by-step procedure used to solve a
problem is called Algorithm.
• An algorithm is a set of well-defined
instructions in sequence to solve a problem.
• An algorithm is a procedure or formula for
solving a problem, based on a sequence of
specified actions.
Characteristics of an Algorithm
• Unambiguous(Definiteness)−Algorithm
should be clear and unambiguous. Each of its
steps , and their inputs/outputs should be
clear and must lead to only one meaning.
• Input − An algorithm should have zero or
more well-defined inputs.
• Output − An algorithm should have one or
more well-defined outputs, and should match
the desired output.
• Effectiveness:For an algorithm to be effective,
it means that all those steps that are required
to get to output must be feasible with the
available resources.It should not contain any
unnecessary and redundant steps which could
make an algorithm ineffective.
• Finiteness: An algorithm must always
terminate after a finite number of steps. An
algorithm should not be infinite and always
terminate after definite number of steps.
• Independent:An algorithm should have
step-by-step directions, which should be
independent of any programming code.It
should be such that it could be run on any of
the programming languages.
Algorithm to add two numbers
Step 1: Start
Step 2: Read values in 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 to calculate average of two numbers.
Step 1 : Start
Step 2 : Input number1, number2
Step 3 : set sum = 0 ,average = 0
Step 4 : Calculate sum = number1 + number2
Step 5 : calculate Average = sum / 2
Step 6 : Print average of two numbers
Step 7 : Stop
Algorithm to 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
Finding Smallest Number
step 1 : Start
Step 2 : Read the values of no1 and no2
Step 3 : Check Whether no1<no2 then goto
step 4 otherwise goto step 5
step 4: Display no1 is smaller and goto step 6
step 5: Display no2 is smaller
step 6 : Stop
Prime Number
Step 1: Start
Step 2: Read number from user in no and initialize
j=1,flag=1
Step 3: If no<=1 or no=0
Display "no is not a prime number" Goto
step 6
Step 4: Repeat the steps until j<[(n/2)+1]
4.1 If remainder of no/j equals to 0, Set flag=0 Goto
step 6
4.2 j=j+1
Step 5: If flag==0, Display no is not prime number Else
Display n is a prime number"
Step 6: Stop
Quadratic Equation
Step 1:Start
Step 2:Read the coefficients of the quadratic
equation in a ,b and c
Step 3:Calculate d=(b*b)-(4*a*c)
Step 4:check if d >0 then goto step 5 otherwise
goto step 6
Step 5:Print Roots are real and distinct
5.1:calculate r1=(-b+sqrt(d))/(2*a)
5.2:calculate r2=(-b-sqrt(d))/(2*a)
5.3:Print r1 and r2 and goto step 9
Step 6: check if d=0 then goto step 7 otherwise
goto step 8
Step 7:Print Roots are real and same
7.1:calculate r1=-b/(2*a)
7.2:Print r1 and goto step 9
Step 8:Print roots are imaginary
Step 9:Stop
Odd/Even Number
Step 1:Start
Step 2:Read the number in variable no
Step 3:check if no/2=0 then print “no is even
number” and goto step 5 otherwise goto
step4
Step 4:print “no is odd number”
Step 5:Stop
Algorithm to check the given year is a leap year
or not
• A year is a leap year if the following conditions
are satisfied:
1. The year is multiple of 400.
2. The year is multiple of 4 and not multiple of
100.
Factorial of a number entered by the user.
Largest among three numbers
Flowchart
• Flowchart is a diagrammatic representation of
sequence of logical steps of a program.
• Pictorial representation of an algorithm
• A flowchart is simply a graphical
representation of steps. It shows steps in
sequential order and is widely used in
presenting the flow of algorithms, workflow or
processes.
Flowchart Symbols
some of the common symbols used in drawing
flowcharts.
1.Terminator symbol or start/stop symbol
• An oval represent start or end.
• Used at the beginning and end of the
algorithm to show start and end of the
program.
2.Process
• A rectangle represent a process
• Flowchart process shape is used to represent a
process, action step or an operation.
• Indicates processes like mathematical
operations
3.Input/Output
• A parallelogram represent input or output
• It represents information entering or leaving
the system.
4.Decision
• A diamond/Rhombus indicate a decision
• This object is always used in a process flow to ask a
question. And, the answer to the question
determines the arrows coming out of the Diamond.
• One from the bottom point corresponding to Yes or
True and one from either the right/left point
corresponding to No or False. The arrows should
always be labeled to avoid confusion in the process
flow.
5.Arrow
• Lines represent the flow of the sequence and
direction of a process.
6.Connetor
• Connects two or more parts of a flowchart,
which are on the same page.
• On-page connectors are referenced using
numbers