Vous êtes sur la page 1sur 8

An algorithm is a a step-by-step method of solving a problem

Or

The sequence of steps to be performed in order to solve a given


problem by the computer is known as an algorithm

An algorithm must possess the following properties:

 finiteness: The algorithm must always terminate after a finite


number of steps.
 definiteness: Each step must be precisely defined; the actions to
be carried out must be rigorously and unambiguously specified for
each case.

E S . I N
 input: An algorithm has zero or more inputs, taken from a

KTU NOT
specified set of objects.
 output: An algorithm has one or more outputs, which have a
specified relation to the inputs.
 effectiveness: All operations to be performed must be sufficiently
basic that they can be done exactly and in finite length

Expressing Algorithms

An algorithm may be expressed in a number of ways, including:

 natural language: usually verbose and ambiguous


 flow charts: avoid most (if not all) issues of ambiguity; difficult to
modify w/o specialized tools; largely standardized
 pseudo-code: also avoids most issues of ambiguity; vaguely
resembles common elements of programming languages; no
particular agreement on syntax

Downloaded from Ktunotes.in


 programming language: tend to require expressing low-level
details that are not necessary for a high-level understanding

AN ALGORITHM TO FIND THE SUM OF TWO NUMBERS.

 STEP 1 : START
 STEP 2 : READ FIRST NUMBER
 STEP 3 : READ SECOND NUMBER
 STEP 4 : ADD THESE TWO NUMBERS
 STEP 5 : DISPLAY RESULT
 STEP 6 : STOP

S . I N
WRITE AN ALGORITHM TO FIND THE AREA OF RECTANGLE.
E

 KTU
STEP 1 : START NOT
STEP 2 : READTHE length OF RECTANGLE
 STEP 3 : ACCEPT THE breadth OF RECTANGLE
 STEP 4 : Area = length x breadth
 STEP 5 : DISPLAY Area
 STEP 6 : STOP

WRITE AN ALGORITHM TO FIND THE AREA OF CIRCLE

 STEP 1 : START
 STEP 2 : READTHE radius OF CIRCLE
 STEP 3 : Area = π × r x r
 STEP 4 : DISPLAY Area
 STEP 5 : STOP

Downloaded from Ktunotes.in


Flow Chart
 A flowchart is the graphical or pictorial representation of an algorithm
 with the help of different symbols, shapes and arrows in order to
demonstrate a process or a program

Terminal Indicates the starting or ending of the program (Start/Stop)

Process : Indicates any type of internal operation

Used for any Input / Output (I/O) operation. Indicates that the
Input/ output :
computer is to obtain data or output results

Used to ask a question that can be answered in a binary format


Decision (Yes/No, True/False)

E S . I N
KTU NOT
Flow lines: Shows direction of flow

Connector: Allows the flowchart to be drawn without intersecting


lines or without a reverse flow.

Flow chart to find sum of two numbers

Downloaded from Ktunotes.in


Flow Chart to find the area of a circle

Internal representation of data


E S . I N

 KTU NOT
Inside computers, all data is represented as 0’s and 1’s
The binary system is a perfect match to the way computers work.
 A binary system based on n bits can represent 2n different things
 Computer uses a fixed number of bits to represent a piece of data,
which could be a number, a character, or others.

Downloaded from Ktunotes.in


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.
Sumnum1+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

S . I N
OTE
Display a is the largest number.

Else
Else

K T U N
Display c is the largest number.

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
Db2-4ac
Step 4: If D=0
r1 (-b+vD)/2a
r2  (-b-vD)/2a

Downloaded from Ktunotes.in


Display r1 and r2 as roots.
Else
Calculate real part and imaginary part
Rp b/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
Factorial1
i1
Step 4: Read value of n
Step 5: Repeat the steps until i=n
5.1: factorial  factorial*i
5.2: I  i+1

S . I N
OTE
Step 6: Display factorial
Step 7: Stop

K T U N
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
Flag 0
i 2
Step 4: Read n from user.
Step 5: Repeat the steps until i <(n/2)
5.1 If remainder of n÷i equals 0
Flag 1
Go to step 6
5.2 I i+1
Step 6: If flag=1
Display n is not prime

Downloaded from Ktunotes.in


else
Display n is prime
Step 7: Stop

Write an algorithm to find the Fibonacci series till term=100


Step 1: Start
Step 2: Declare variables first_term, second_term and temp.
Step 3: Initialize variables first_term 0 second_term 1
Step 4: Repeat the steps until first_term=100
4.1: Display first_term
4.2: temp second_term+first term
4.3: first_term second_term
4.4: second_term  temp
Step 6: Stop

S . I N
T U N OTE
K

Downloaded from Ktunotes.in