ALGORITHMS:
In programming, algorithm is the set of well-defined instruction in sequence to
solve a program. It is a self-contained step-by-step set of operations to be
performed. An algorithm should always have a clear stopping point.
Qualities of a good algorithm
1.
2.
3.
4.
Inputs and outputs should be defined precisely.
Each step in algorithm should be clear and unambiguous.
Algorithm should end in finite number of steps.
Algorithm should be most effective and efficient among many different
ways to solve a problem.
5. An algorithm shouldn't have computer code. Instead, the algorithm should
be written in such a way that, it can be used in similar programming
languages.
Algorithms can be expressed in many kinds of notation, including natural
languages, pseudocode, flowcharts, drakon-charts, programming languages or
control tables.
Pseudocode, flowcharts, drakon-charts and control tables are structured ways to
express algorithms that avoid many of the ambiguities common in natural language
statements.
Examples of Algorithms in Programming
Write an algorithm to add two numbers entered by 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.
sumnum1+num2
Step 5: Display sum
Step 6: Stop
Write an algorithm to find the largest among three different numbers entered
by user.
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
Write an algorithm to find all roots of a 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
Db2-4ac
Step 4: If D0
r1(-b+D)/2a
r2(-b-D)/2a
Display r1 and r2 as roots.
Else
Calculate real part and imaginary part
rpb/2a
ip(-D)/2a
Display rp+j(ip) and rp-j(ip) as roots
Step 5: Stop
Write an algorithm to find the factorial of a number entered by user.
Step 1: Start
Step 2: Declare variables n,factorial and i.
Step 3: Initialize variables
factorial1
i1
Step 4: Read value of n
Step 5: Repeat the steps until i=n
5.1: factorialfactorial*i
5.2: ii+1
Step 6: Display factorial
Step 7: Stop
Write an algorithm to check whether a number entered by user is prime or
not.
Step 1: Start
Step 2: Declare variables n, i, flag.
Step 3: Initialize variables
flag1
i2
Step 4: Read n from user.
Step 5: Repeat the steps until i<(n/2)
5.1 If remainder of ni equals 0
flag0
Go to step 6
5.2 ii+1
Step 6: If flag=0
Display n is not prime
else
Display n is prime
Step 7: Stop
Write an algorithm to find the Fibonacci series till term1000.
Step 1: Start
Step 2: Declare variables first_term,second_term and temp.
Step 3: Initialize variables first_term0 second_term1
Step 4: Display first_term and second_term
Step 5: Repeat the steps until second_term1000
5.1: tempsecond_term
5.2: second_termsecond_term+first term
5.3: first_termtemp
5.4: Display second_term
Step 6: Stop
Algorithm is not the computer code. Algorithm is just the instructions which give
clear idea to, how to write the computer code.
FLOWCHARTS:
Flowchart is a diagrammatic representation of an algorithm.
Symbols Used In Flowchart
Different symbols are used for different states in flowchart. The table below
describes all the symbols that are used in making flowchart
Symbol
Purpose
Flow line
Description
Used to indicate the flow of logic by connecting symbols.
Terminal(Stop/Start)
Used to represent start and end of flowchart.
Input/output
Used for input and output operation.
Processing
Used for arithmetic operations and data-manipulations.
Decision
Used to represent the operation in which there are two
alternatives, true and false.
On-page Connector
Used to join different flow line
Off-page Connector
Used to connect flowchart portion on different page.
Predefined
Process/Function
Used to represent a group of statements performing one
processing task.
Examples of flowcharts in programming
Draw a flowchart to add two numbers entered by user.
Draw flowchart to find the largest among three different numbers entered by user.
Draw a flowchart to find all the roots of a quadratic equation ax2+bx+c=0
Draw a flowchart to find the Fibonacci series till term1000.
Though, flowchart is useful in efficient coding, debugging and analysis of a
program, drawing flowchart in very complicated in case of complex programs and
often ignored.