algorithms
algorithms
introduction
is important aspect of problem solving. You
Planning an
Design
would have experienced that proper planning is required
for successful completion of a task. It includes analysing,
reasoning, designing and implemention. We also need to
plan for problem solving even with the help of a computer.
To do so, first of al1, the given problem is analysed. Based Algorithms
on analysis, a sequence of actions are
generated through
proper reasoning. The step wise sequence of actions to
perform desired task is known as algorithm. Implement
Thus, an
algorithm is defined as a sequence of steps written in simple English. It helps
in solving a problem whether it is simple or
complex.
Algorithms have a definite beginning and end with a finite number of steps. It is
usually presented in the form of some pseudo-code which may be a combination of some
mathematical notations and a few selected keywords from a
In this chapter, you will learn about
programming language.
problem solving in the field of While
writing a program, it requires some logic to solve the problem and if you computers.
plan before hand,
then execution of the task becomes easier. There are
some basic characteristics folloWing
which, you can write an algorithm for a given
problem easily. They are as follows:
Begin algorithm with the keyword 'Start'.
an
Solved Problems
Ouestion 1: Write an
algorithmm to find the sum and average of three niombers.
Algorithm: The different steps are:
Step 1: Start
Step 2: Accept three numbers nl, n2 and n3.
Step 3: Find Sum = nl+n2+n3.
Step 4: Find Average= Sum/3
Step 5: Display sum and Average.
Step 6: Stop
Question 2: Snjay Mallhotrn is a student of class X. He wants to calculate the aterage nars
obtained in1 Science (i.e. Plhysics, Clhemistry and Biologu) and wants to knoe whether he is
elig:ble
for Science or Coninmerce stream in Class XI. To get adission in Science Streanm one 1eeds 70
S or abotve in Science otherwise, he uill get Commerce Stream. Write an algoritim to pertiom
he above task.
Algorithm: The different steps are:
Step 1: Start
Step 2: Accept the marks obtained in Physics, Chemistry and Biology from the
console.
Step 3: Find Sum = Physics +Chemistry + Biology
Step 4: Find Average= Sum/3
Step 5: If average marks is 70% or more then goto Step 6 otherwise, Step 7.
>=80% A
C
>=40% & <60%
No Grade
<40%
Algorithm: The diferent steps are:
Step 1: Start
Step 2: Accept the average marks obtained in the term examination from the consola
.
Step 3: If the average marks is 80% or more but less than or equal to 9 o then display
Up to 1,50,000/- No tax
Step 6: If the annual income is more than R5,00,000 then calculate tax as
tax 19,000 +30% of (inc - 5,00,000), goto step 7.
Question 5. The standard form of a quadratic cquation is givet by: ax2 + bx + c = 0, elhere
a, b and care the coefficient ofx2, x and the constant respectively Calculate d = b2- 4ac, where
d is known as discriminant
if d>=0 ihe roots are possible and
if d<0 the roots are inaginary
The roots of the equation are deterined by the fornnla
-b yb2-4nc
2
Write an algorithm to determine the roots of the equation taking a, b and c as inputs.
Algorithm: The different steps are:
Step 1: Start
Step 6: if(d>0)
Find rl=-b+ Vd
2*a
Find r2=-bd
2'a
Display("Roots are real and distinct",r1,r2)
endif
Step 7: Stop
Question 6: Write an algorithm to enter a number and check whether the number is prime
or not.
then
Display ("Prime Number")
else
Display ("Not a Prime Number")
Step 6: Stop
Question 7: Write an algorithm to enter a number and find the sum of tite digits of tlhe nunmber.
For example, Sample Input: 478
Sanple Output: 4+ 7 +8 = 19
Algorithnm: The different steps are:
Step 1: Start
Step 2: Accept a number n from the console.
Step 3: while (n>0)
Il to digit at a time
a=remainder(n/10) get one
Step 5: Stop.
Question 8: Write an algorithm to find maxiun1 and nmnnu711un1 ann0ng a set of 10 1mbers
Step 7: if(m<min)
min=m
Step 8: Display ("Maximum and minimum", max, min)
Step 9: Stop
Question 9: Write an algorithm to generate first 20 elements of Fibonacci senies.
Algorithm: The different steps are:
Step 1: Initialize i=0
Step 2: Initialize j=1
Step 3: Display (i)
Step 4: Display )
Step 5: Set count = , 2
then
FLAG = TRUE
else
PTR=PTR +1
End of while loop
Step 4: Check for success
if(FLAG = TRUE)
then
Display("Search is Successful")
else
Display("Search is Unsuccessful)
Step 5: Stop
=+1
else
CITEMP] = BJ]
TEMP=TEMP+1
J=J+1
endif
if (I>M)
then
repeat for K =0,1,2,.N-J
CITEMP+K] = B[I+K]
else
Repeat for K=0,1,2,....,M-I
CITEMP+K] = A[I+K]
endif
Step 5: Stop
to insert a1 element at a give1 posttion1
111 a Sigle dinmensional
Question 16: Write an algoritlm
array.
Algorithm: The different steps are:
Step 6: Stop
given position in an array
Question 17: Write an algorithm to delete an element from a
COUNT = POS
Step 6: Stop