ALGORITHM
The fundamental knowledge necessary to
solve problems using a computer is the notion of an algorithm. An algorithm is
a precise specification of a sequence of instructions to be carried out in
order to solve a given problem. Each instruction tells us what task is to be
performed.
The fundamental
knowledge necessary to solve problems using a computer is the notion of an algorithm. An algorithm is precise
specification of a sequence of instructions to be carried out to solve a given
problem.
Def:
An
algorithm can be defined as a step-by step procedure to solve a particular
problem.
Criteria
for algorithm
An algorithm is a finite set of
instructions which, if followed, accomplish a particular task. In addition
every algorithm must satisfy the following criteria:
1. Input: there are zero
or more quantities which are externally supplied.
2. Output: at least one
quantity is produced.
3. Definiteness: each
instruction must be clear and unambiguous.
4. Finiteness: if we trace
out the instructions of an algorithm, then for cases the algorithm must
terminate after a finite number of steps.
5. Effectiveness:
every instruction must be sufficiently basic that it can in principle be
carried out by a person using only pencil and paper. It is not enough that each
operation be definite, but it must also be feasible.
Algorithmic
Notations
ü Name of the algorithm: it
specifies the problem to be solved.
ü Initialization of algorithm: it
specifies the start of the algorithm.
ü Step number: identification
tag of an instruction and it is unsigned positive integer.
ü Explanatory comment:
describes the operations, should be written in square brackets [ ] is optional.
ü Termination: it specifies
the end of the algorithm.
Few
examples of algorithm
1. To
find the sum and average of 3 numbers.
Algorithm:
To find sum and average of three numbers
Step
1: START
Step
2: PRINT “Enter any three numbers”
Step
3: INPUT a, b, c
Step
4: COMPUTE d = a + b + c
Step
5: COMPUTE e = d / 3
Step
6: PRINT d, e
Step
7: STOP
2. To
find simple interest.
Algorithm:
To calculate simple interest
Step
1: START
Step
2: PRINT “Enter principal, rate of interest and time period”
Step
3: INPUT p, r, t
Step
4: COMPUTE si = p * r * t / 100
Step
5: PRINT si
Step
6: STOP
3. To
convert Fahrenheit temperature to centigrade
Algorithm:
To convert temperature given in oF to oC
Step
1: START
Step
2: PRINT “Enter temperature in oF
Step
3: INPUT f
Step
4: COMPUTE c = ( ( f – 32 ) * 100 ) / 180
Step
5: PRINT “Temperature in oC=”, c
Step
6: STOP
4. To
swap two numbers using a third variable.
Algorithm:
To swap two numbers using a third variable
Step
1: START
Step
2: PRINT “Enter any two numbers”
Step
3: INPUT a, b
Step
4: LET c = a
Step
5: LET a = b
Step
6: LET b = c
Step
7: PRINT a, b
Step
8: STOP
5. To
swap two numbers without using a third variable.
Algorithm:
To swap two numbers without using a
third variable
Step
1: START
Step
2: PRINT “Enter any two numbers”
Step
3: INPUT a, b
Step
4: COMPUTE a = a + b
Step
5: COMPUTE b = a - b
Step
6: COMPUTE a = a - b
Step
7: PRINT a, b
Step
8: STOP
6. To
find whether the number is odd or even.
Algorithm:
To check whether the entered number is even or odd
Step
1: START
Step
2: PRINT “Enter any number”
Step
3: INPUT a
Step
4: IF (a % 2 = = 0) GOTO Step 5 ELSE GOTO Step 6
Step
5: PRINT “Given number is even” GOTO Step 7
Step
6: PRINT “Given number is odd”
Step
7: STOP
7. To
check whether the roots of quadratic equation are real or imaginary.
Algorithm:
To check whether the roots of quadratic equation are real or imaginary
Step
1: START
Step
2: PRINT “Enter the values of a, b and c”
Step
3: INPUT a, b, c
Step
4: COMPUTE d = b * b – 4 * a * c
Step
5: IF (d > 0) GOTO Step 6 ELSE GOTO Step 7
Step
6: PRINT “Roots are real” GOTO Step 8
Step
7: PRINT “Roots are imaginary”
Step
8: STOP
8. To
find the largest among given two numbers.
Algorithm:
To find the largest among given two numbers
Step
1: START
Step
2: PRINT “Enter any two numbers”
Step
3: INPUT a, b
Step
4: IF (a > b) GOTO Step 5 ELSE GOTO Step 6
Step
5: PRINT “First number is greater than second” GOTO Step 7
Step
6: PRINT “Second number is greater than first”
Step
7: STOP
9. To
find the largest among given three numbers.
Algorithm:
To find the largest among given three numbers
Step
1: START
Step
2: PRINT “Enter any three numbers”
Step
3: INPUT a, b, c
Step
4: IF (a > b) GOTO Step 5 ELSE GOTO Step 6
Step
5: IF (a > c) PRINT a ELSE Print c GOTO Step 7
Step
6: IF (b > c) PRINT b ELSE PRINT c
Step
7: STOP
10. To
find the sum of the digits of n-digit number.
Algorithm:
To find the sum of the digits of a number
Step
1: START
Step
2: PRINT “Enter any number”
Step
3: INPUT n
Step
4: LET sum = 0
Step
5: WHILE (n > 0)
{
r
= n % 10
sum = sum + r
n = n / 10
}
Step
6: PRINT sum
Step
7: STOP
11. To
reverse the digits of given n-digit number.
Algorithm:
To reverse the digits of a number
Step
1: START
Step
2: PRINT “Enter any number”
Step
3: INPUT n
Step
4: LET rev = 0
Step
5: WHILE (n > 0)
{
r
= n % 10
rev = rev * 10 + r
n = n / 10
}
Step
6: PRINT rev
Step
7: STOP
12. To print
numbers divisible by 7 from 1 to N numbers.
Algorithm:
To find the numbers divisible by 7 in range 1 to N
Step
1: START
Step
2: PRINT “Enter the range”
Step
3: INPUT n
Step
4: LET count = 1
Step
5: WHILE (count < = n)
{
IF
(count % 7 = = 0)
{
PRINT
count
count =
count + 1
}
ELSE
count =
count + 1
}
Step
6: STOP
13. To
calculate the factorial of a number.
Algorithm:
To calculate the factorial of a number
Step
1: START
Step
2: PRINT “Enter any number”
Step
3: INPUT n
Step
4: LET fact = 1, i = 1
Step
5: WHILE (i < = n)
{
fact
= fact * i
i = i + 1
}
Step
6: PRINT fact
Step
7: STOP
14. To
find whether a given number is prime or not.
Algorithm:
To check whether the given number is prime or not
Step
1: START
Step
2: PRINT “Enter any number”
Step
3: INPUT n
Step
4: LET i = 2
Step
5: WHILE (i < = n-1)
{
IF
(n % i = = 0)
PRINT “Number is
not prime!”
ELSE
i = i + 1
}
Step
6: IF (n = = i)
PRINT
“Number is prime!”
Step
7: STOP
15. To
print first N Fibonacci numbers.
Algorithm:
To generate first n-Fibonacci numbers
Step
1: START
Step
2: PRINT “Enter the range”
Step
3: INPUT n
Step
4: LET a = 0, b = 1, i = 1
Step
5: PRINT a
PRINT
b
Step
6: FOR (i = 1 to n-2)
{
c = a + b
PRINT c
a = b
b = c
i = i + 1
i = i + 1
}
Step 7: STOP
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.