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

Shashank Yadav-AP (Computer Science Department), HITM - Agra

The document discusses algorithms and flowcharts. It defines an algorithm as a finite set of unambiguous instructions to perform a task. An algorithm must have input, output, be finite, and definite. It also discusses properties of algorithms like correctness, termination, and examples. Flowcharts are represented using standard symbols like processes, decisions, and flow lines to show the sequence of operations. Examples provided include algorithms and flowcharts to find the maximum of 3 numbers, check if a number is even or odd, and determine if a character is a vowel or consonant.

Uploaded by

Awais Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Shashank Yadav-AP (Computer Science Department), HITM - Agra

The document discusses algorithms and flowcharts. It defines an algorithm as a finite set of unambiguous instructions to perform a task. An algorithm must have input, output, be finite, and definite. It also discusses properties of algorithms like correctness, termination, and examples. Flowcharts are represented using standard symbols like processes, decisions, and flow lines to show the sequence of operations. Examples provided include algorithms and flowcharts to find the maximum of 3 numbers, check if a number is even or odd, and determine if a character is a vowel or consonant.

Uploaded by

Awais Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

9.

Problem solving by Algorithm and Flow-Charts:

9.1 Algorithm:

 Basically algorithm is a finite set of instructions that can be used to perform certain task.
 The algorithm is defined as the collection of unambiguous instructions occurring in some
specific sequence and such an algorithm should produce output for given set of input in
finite amount of time.

9.1.1 Properties of Algorithm:

1. Input: The input should be taken from specified set of objects.


2. Output: At least one quantity is produced, which has a specified relation to the inputs.
3. Finiteness: An algorithm must always terminate after a finite number of steps.
4. Definiteness: Each instruction must be clear and unambiguous.

9.1.2 Algorithm Correctness:

 The correctness of an algorithm is related to its purpose.


 The algorithm is correct with respect to the specification, if it produces the desired results
for all the inputs defined by the specification.
 Any algorithm has two types of correctness.
o Partial Correctness
o Total Correctness

9.1.2.1 Partial Correctness: The partial correctness of the algorithm defines that for every legal
input when an algorithm terminates, the result produced will be valid. An algorithm is said to be
“partial correctness” because it does not halt (terminate) in some condition.

9.1.2.2 Total Correctness: The total correctness of an algorithm defines that for every legal
input, the algorithm halts and then output produced will be valid.

9.1.3 Algorithm Termination:


 A termination proof is a type of mathematical proof that plays a critical role in
verification of algorithm because total correctness of an algorithm depends on
termination.
 For every valid input, an algorithm must terminate after a finite numbers of steps.

9.1.4 Example:

I. Write an algorithm for division of two numbers.

Division (a, b)
where a and b are two numbers.
1. c=a/b
2. write(division of a and b is c)

Shashank Yadav- AP [Computer Science Department], HITM- Agra


 This algorithm is Partial Correct because if b is zero then there will not be valid output.

Division (a, b)
where a and b are two numbers.
1. if (b!=0) then
2. {
3. c= a/b
4. Write(Division of a and b is c)
5. }
6. else
7. {
8. Write(Division is not possible)
9. }

 This algorithm is Total Correct.

II. Write pseudo code for finding out maximum number from three distinct
numbers.

Maximum (a, b, c)
where a, b, c are three numbers.

1. if(a>b) then
2. {
3. if(a>c) then
4. {
5. Write(a is maximum)
6. }
7. else
8. {
9. Write(c is maximum)
10. }
11. }
12. else if(b>c) then
13. {
14. Write(b is maximum)
15. }
16. else
17. {
18. Write(c is maximum)
19. }

Shashank Yadav- AP [Computer Science Department], HITM- Agra


9.2. Flow Chart:

 A flow chart is a graphical or symbolic representation of a process.


 Each step in the process is represented by a different symbol and contains a short
description of the process step.
 The flow chart symbols are linked together with arrows showing the process flow
direction.

9.2.1 Flow Chart Symbols:

Symbol Name Purpose

Terminal Indicates the beginning and end of a program.

Process For calculation or assigning of a value to a variable.

Input/ Output For input or output data for a process.

Decision Program decisions

Connector Its use indicates that one symbol is connected to


another.

Predefined Process A group of statements that together accomplish one


task.

Flow Lines Used to connect symbols and indicate the sequence


of operation.

Shashank Yadav- AP [Computer Science Department], HITM- Agra


9.2.2 Examples

1. Draw a flowchart for swapping two numbers.

Start

Input a, b

a=a+b
b=a-b
a=a-b

Swapped
values are a, b

Stop

Shashank Yadav- AP [Computer Science Department], HITM- Agra


9.3 Some Examples for Algorithm and flowcharts.

1. What do you mean by Algorithm? Write an algorithm and draw flowchart for
finding out maximum number from 3 numbers.

Algorithm: The algorithm is defined as the collection of unambiguous instructions


occurring in some specific sequence and such an algorithm should produce output for
given set of input in finite amount of time.

Maximum(a, b, c)
Where a, b, c are three numbers.
1. if(a> b and a>c)
2. Write(a is maximum)
3. else if(b>a and b>c)
4. Write(b is maximum)
5. else
6. Write(c is maximum)

Start

Input numbers (a, b, c)

a is maximum if(a>b and a>c)


Yes
No
Yes
if(b>a and b>c) b is maximum

No

c is maximum

Stop

Shashank Yadav- AP [Computer Science Department], HITM- Agra


2. Write an algorithm and draw flowchart for finding out area of a triangle, if all sides
of triangle are given. If a, b, c are three sides, the formula is area=(s*(s-a)*(s-b)*(s-
c))1/2 where s is (a+b+c)/2.

Triangle_Area(a, b, c)
Where a, b, c are sides of triangle.
1. if((a+b)>c and (b+c)>a and (c+a)>b)
2. {
3. s=(a+b+c)/2
4. area=(s*(s-a)*(s-b)*(s-c))1/2
5. Write (Area of Triangle is area)
6. }
7. else
8. Write(Triangle is not possible)

Start

Input sides of Triangle


(a, b, c)

No
if ((a+b)>c and
(b+c)>a and (c+a)>b)

Yes

s=(a+b+c)/2 Triangle is
area=(s*(s-a)*(s-b)*(s-c))1/2 not possible.

Area of triangle
is area

Stop

Shashank Yadav- AP [Computer Science Department], HITM- Agra


3. Draw a flow chart for checking that given number is even or odd.

Start

Input number (n)

No
if(n%2==0) n is odd.

Yes

n is even.

Stop

4. Write an algorithm and draw the flowchart for finding out that given alphabet is
vowel or consonant.

Vowel_Consonant(ch)
Where ch is an alphabet.

1. if(ch ∈ (a, e, i, o, u, A, E, I, O, U)) then


2. write (ch is vowel).
3. else
4. write(ch is consonant).

Shashank Yadav- AP [Computer Science Department], HITM- Agra


Start

Input an alphabet (ch)

No
if (ch ∈
(a,e,i,o,u,A,E,I,O,U)) ch is consonant

Yes

ch is vowel.

Stop

5. Write an algorithm and draw flowchart for finding out given number is prime or
not.

Prime(n)
Where n is a number.
1. i= 2
2. while(i<=n/2) do
3. {
4. if(n%i==0)
5. {
6. Write(n is not prime)
7. break
8. }
9. i=i+1
10. }
11. if(i>n/2)
12. write( n is prime)

Shashank Yadav- AP [Computer Science Department], HITM- Agra


Start

Input a number (n)

i= 2

No
if (i<=n/2)

Yes
No
if(n%i==0) i= i+1
n is Prime

Yes

n is not Prime

Stop

6. Write an algorithm and draw flowchart for calculating the sum of digits of given
number.

Sum_of_Digits (n)
Where n is a number
1. x= n
2. sum=0
3. while(x!=0) do
4. {
5. r=x%10
6. sum=sum+r
7. x=x/10
8. }
9. Write(sum of digits is sum)

Shashank Yadav- AP [Computer Science Department], HITM- Agra


Start

Input a number (n)

x= n
sum=0

No
if (x!=0) Sum of digits is sum.

Yes
r= x%10
sum=sum+r
x=x/10

Stop

7. Write an algorithm and draw flowchart for reversing a given number.

Reverse (n)
Where n is a number
1. x= n
2. sum=0
3. while(x!=0) do
4. {
5. r=x%10
6. sum=sum*10+r
7. x=x/10
8. }
9. Write(reverse of n is sum)

Shashank Yadav- AP [Computer Science Department], HITM- Agra


Start

Input a number (n)

x= n
sum=0

No
if (x!=0) Reverse of n is sum.

Yes
r= x%10
sum=sum*10+r
x=x/10

Stop

8. Write an algorithm and draw flowchart for checking given number is palindrome or
not.

Palindrome(n)
Where n is a number
1. x=n
2. sum=0
3. while(x!=0) do
4. {
5. r=x%10
6. sum=sum*10+r
7. x=x/10
8. }
9. if(n==sum)
10. Write(n is palindrome number)
11. else
12. Write(n is not palindrome number)

Shashank Yadav- AP [Computer Science Department], HITM- Agra


Start

Input a number (n)

x= n
sum=0

No No
if (x!=0) if(n==sum)

Yes Yes
r= x%10
sum=sum*10+r
x=x/10 n is not
Palindrome

n is Palindrome

Stop

9. Write an algorithm and draw flowchart for checking given number is octal or not.
Check_Octal(n)
Where n is a number.
1. x=n
2. while(x!=0) do
3. {
4. r=x%10
5. if(r==8 or r==9) then
6. {
7. write( n is not octal number)
8. break
9. }
10. x=x/10
11. }
12. If(x==0)

Shashank Yadav- AP [Computer Science Department], HITM- Agra


13. n is octal number

Start

Input a number (n)

x= n

No
if (x!=0)

Yes

r= x%10
n is octal

No
if(r==8 or r==9) x= x/10

Yes

n is not octal number

Stop

Shashank Yadav- AP [Computer Science Department], HITM- Agra


10. What is the difference b/w algorithm and flowchart? Explain with example.

Sn Algorithm Flowchart
1 Basically algorithm is a finite set of A flow chart is a graphical or symbolic
instructions that can be used to representation of a process.
perform certain task.
2 Each step in the process is written in Each step in the process is represented
sentences or in pseudo code. by a different symbol and contains a
short description of the process step.
3 Eg: Algorithm for checking given Eg: Flowchart for checking given
integer number is even or odd. number is even or odd.

Even_Odd (a) Start


Where a is an integer number.
1. if (a%2==0) then
2. Write(a is even) Input an integer
3. else number (a)
4. Write(a is odd)

if(a%2==0)

No
Yes

a is even a is odd

Stop

Shashank Yadav- AP [Computer Science Department], HITM- Agra

You might also like