Shashank Yadav-AP (Computer Science Department), HITM - Agra
Shashank Yadav-AP (Computer Science Department), HITM - Agra
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.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.4 Example:
Division (a, b)
where a and b are two numbers.
1. c=a/b
2. write(division of a and b is c)
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. }
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. }
Start
Input a, b
a=a+b
b=a-b
a=a-b
Swapped
values are a, b
Stop
1. What do you mean by Algorithm? Write an algorithm and draw flowchart for
finding out maximum number from 3 numbers.
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
No
c is maximum
Stop
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
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
Start
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.
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)
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)
x= n
sum=0
No
if (x!=0) Sum of digits is sum.
Yes
r= x%10
sum=sum+r
x=x/10
Stop
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)
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)
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)
Start
x= n
No
if (x!=0)
Yes
r= x%10
n is octal
No
if(r==8 or r==9) x= x/10
Yes
Stop
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.
if(a%2==0)
No
Yes
a is even a is odd
Stop