0% found this document useful (0 votes)
7 views

algorithms

The document provides an overview of algorithms, emphasizing their importance in problem-solving and programming. It outlines the characteristics of algorithms, presents several examples of algorithms for various tasks, and explains the differences between algorithms and computer programs. Additionally, it includes step-by-step algorithms for tasks such as calculating sums, averages, grades, income tax, and searching elements in arrays.

Uploaded by

santhoshmart11
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

algorithms

The document provides an overview of algorithms, emphasizing their importance in problem-solving and programming. It outlines the characteristics of algorithms, presents several examples of algorithms for various tasks, and explains the differences between algorithms and computer programs. Additionally, it includes step-by-step algorithms for tasks such as calculating sums, averages, grades, income tax, and searching elements in arrays.

Uploaded by

santhoshmart11
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Touhischapicr to solve an algorithm, Write algorithm

Introduction, Meaning, Implementation, Basic steps


to a given problem as well as a program.

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

Write each instruction in a


separate line.
Each instruction should be
precise and clear.
Each instruction should be executed in a finite time.
After execution, the instructions
should give the desired result.
An algorithm must contain a finite number of
steps.
wever, it must be noted that
Howe
an
ramming logic. algorithm
differences between anprepares
Here, the the foundation to develop
ane mentioned below: algorithm and a computer program
Algorithmn
algorithm is a set of Computer Program
An
step-by-step instruc-
tions to pertorm a specific problem.
1. A
computer program is a
sequence of
instructions that comply with the rules of a
2. Algorithms are language independent or
specific programming language.
2.
computer independent. Computer programs are written in high level
3. Algorithms are problem solving methods
languages
3. A
and non-executable. computer program can be executed in the
compute.

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.

Step 6: Display "Eligible for Science", goto Step 8.


Step 7: Display "Eligible for Commerce".
Step 8: Stop.
Question 3: Write an algoritlmm to find the grade obtained by a student of a class in the term

amination as per the following rules.


Average Marks Grade

>=80% A

>= 60% & <80% B

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

Grade A, goto Step 7.


Step 4: If the average marks is 60% or more but less than 80% then displau
Grade B, goto Step 7.
Step 5: If the average marks is 40% or more but less than 60% then display
Grade C', goto Step 7.
Step 6: If the average marks is less than 40% then display ' No Grade'.
Step 7: Stop
Question 4: Write at algorithn to calculate the inconme tax to be paid by an employee in the
financial year 2017-18 as per the rates given in table.

Annual Income Rate of Income tax

Up to 1,50,000/- No tax

T1,50,001 to 2,50,000/- 10% of income exceeding 1,50,000

7 1000 + 20% of income exceeding


T 2,50,001 to 5,00,000/-
T 2,50,000

19,000 30% of income exceeding


Above 5,00,000/- T 5,00,000

Algorithm: The different steps are:


Step 1: Start
Step 2: Accept the annual income (inc) of an employee from the console.
Step 3: If the annual income is up to R1,50,000, then display 'No Tax, goto Step 8.
Step 4: If the annual income is more than T1,50,000 but less than or equal to 2,50,000
then calculate tax as:
tax 10% of (inc -1,50,000), goto step 7.
Step 5: If the annual income is more than R 2,50,000 but less than or equal to
5,00,000 then calculate tax as:
tax 1000 +20% of (inc - 2,50,000), goto step 7.

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.

Step 7: Display tax.


Step 8: Stop.

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 2: Accept the coefficients a, b, c


Step 3: Find discriminant d=b'b-4*a*c
Step 4: if (d<0)
Display("Roots are imaginary")
Step 5: if(d=0)
Find r=-b/(2"a)
Display("Roots are equal",r)
endif

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.

Algorithm: The different steps are:


Step 1: Start
Step 2: Accept a number 'n' from the console.
Step 3: Take a counter as c = 0
Step 4: Repeat for i = 1, 2, 3 .., n

if (remainder (n/i)=0) / to check whether the number is divisible with the


increment value of i
C=C+1 /to increase the counter value by 1
end for
Step 5: if (c = 2)

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

l to add the digits of the number


s=Sta
Il to get integer after dividing by 10
n=n/10;
endwhile

Step 4: Display the value of s.

Step 5: Stop.
Question 8: Write an algorithm to find maxiun1 and nmnnu711un1 ann0ng a set of 10 1mbers

entered from console


Algorithm: The different steps are:
Step 1: Accept first element n
Step 2: Initialize max=n
Step 3: Initialize min=n, i=1
Step 4: Repeat through step 7 while (i<10)
Step 5: Accept other number in m
Step 6: if (m>max)
max=m

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

Step 6: Repeat while (count<20)


Find k=i+j
Display (k)
Set i=j
Set j=k
count = count +1 II Increase counter
by one
Step 11: Stop
Question 10: Write an algorit1mi to check and print whether a string is a palindrone or not.
Algorithmn: The different steps are:
Step 1: Accept a String str
Step 2: Find length of the String say 1
Step 3: Set count = 0
Step 4: Repeat through step 7 while (count<l)
Step 5: Get a character from count position of the String str and store in C

Step 6: Concatenate the character with String p


Find p
c+pl Concatenation in reverse order
Step 7: Increase the counter value
Set count = count + 1

Step 8: Compare p with original String str


if (str=p)
Display ("Palindrome")
else
Display ("Not a palindrome")
Step 9: Stop
Ouestion 11: Write an algorith1 to arrange a set of n1ummbers stored in a Single Dinme1stO1
wa n ascending order using Bubble Sort technique.
Algorithm: The different steps are:

Suppose, M is an array with N number of elements. TEMP is the temporary variable.


The array elements are to be sorted in
ascending order.]
Step 1: Start
Step 2: Repeat through step 4 for (l=1,23,. N-1)
Step 3: Repeat through step 4 for (J=1, .
N-1)
Step 4: Compare consecutive elements
if(M[J] >MU+ 1)
Step 5: Interchange Jth and (J+1)th elements
TEMP-MJ]
MUl-M[J+1
M[J+1]=TEMP
Step 6: Stop
Question 12: Write an
algorillhmm to arrange a of numbers stored
set ina
aray 1
Single Dimension
ascendmg order us1g isertion Sort technique.
Algorithm: The different steps are:
Suppose, M is an array with N cells. PTR and TEMP are the temporary variables. The
array elements are to be sorted in ascending order.]
Step 1: Start
Step 2: Store a negative number in the initial cell as:
M[0] = -999

Step 3: Sort the elements


Repeat through step 5 for(l=1,2,3..N)
Step 4: Initialize temporary variables
PTR= -1
TEMP= M[
Step 5: Compare the elements backward
Repeat while(M[PTR]>TEMP)
MIPTR+1]=M[PTR]
PTR=PTR-1
end of while loop
Step 6: Insert the element
M[PTR+1=TEMP
Step 7: Stop
Question 13: IVrite an algoritlhm to search an element m a set of nunibers stored in a Sinole
Dnmensio1 Armay n ascending order using Linear Search techmqne
Algorithm: The different steps are:
Suppose, M is a single dimensional array with N number of elements. An element k
is to be searched in the array]
Step 1: Start
Step 2: Initialize the flag
FLAG = FALSE

Step 3: Search the element


PTR=1
Repeat while(PTR<=N)
if(M[PTR] = K)

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

Question 14: Write an


algorithm to search an elenent in set
Dimension Array, wlnich is in ascending order,
a
of numbers stored in a Smgle
using Binary Search technique.
Algorithm: The different steps are:
Suppose, M is a single dimensional array with N number of
in ascending order. An element K is
to be searched in
elements, which is stored
the array.]
Step 1: Start
Step 2: Initialize the flag, lower and
FLAG FALSE
upper boundary.
LOWER= 1
UPPER = N
Step 3: Search the element
Step 4: Repeat while
(LOWER<=UPPER)
MID =(LOWER +UPPER)/2
if(M[MID) = K)
then
FLAG TRUE
if(M[MIDJ> K)
UPPER = MID 1
if(M[MID]< K)
LOWER = MID+1
end of while loop
Step 5: Check for success
if (FLAG =TRUE)
then
Display("Search is Successful")
else
Display("Search is Unsuccessful")
Step 6: Stop.
Question 15: Write an algorithn to merge the clements of treo diferent Single Dimensional
Arrays into a Single Array
Algorithm: The different steps are:
Suppose, A and B are the sorted arrays with M and N number of elements respectively.
It is to merge them into another array C having size P=M+N, such that the elements
remain sorted.]
Step 1: Start
Step 2: Initialize the parameters
=1
J=1
TEMP 1
Step 3: Compare the elements of the arrays
Repeat while(l<= M and J«=N)

if (AlI < BI0)


then
C[TEMP]=A||
TEMP=TEMP+1

=+1
else
CITEMP] = BJ]

TEMP=TEMP+1

J=J+1
endif

Step 4: Store remaining elements in C

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:

the number of elements present in it. An element ELE


[ARR is an array with SIZE as

is to be inserted at position POS.]


Step 1: Initialize the counter
COUNT = SIZE
>=POS)
Step 2: Repeat through step 4 while(COUNT
Step 3: ARRICOUNT + 1] ARR[COUNT]
=

Step 4: Decrease the counter value


COUNT = COUNT -1

Step 5: Insert the element


ARR[POs] = ELE

Step 6: Stop
given position in an array
Question 17: Write an algorithm to delete an element from a

Algorithm: The different steps are:


number of elements present in the array. An element
[ARR is an array with SIZE as the
is to be deleted from position POS.]
Step 1: Initialize the counter

COUNT = POS

Step 2: Repeat through step 4 while(COUNT< SIZE)


Step 3: ARR[COUNT] =
ARR[COUNT + 1]
Step 4: Increase the counter value
COUNT =COUNT + 1
Step 5: Decrease the size of array
SIZE = SIZE - 1

Step 6: Stop

You might also like