Algorithm and Flow Chart
Types of tools to explain the process of a program.
Algorithm
Flow Chart
1
Algorithm
defined as step by step method followed to solve a problem
ie. Well defined sequence of computational steps that’s takes some set
of values as Input and produce some set of values as output
algorithm is a procedure ie. logic for solving problem sequentially.
Examples:
Algorithm : To find volume of a given sphere
1. Start
2. Read radius R
3. Compute V=4/3 π R3
4. Print Volume V
5. End
Algorithm
Algorithm corresponds to solution to a problem that is
independent of any programming languages
Problem
Algorithm
I/P data Program Solution of
O/P data
problem
Program: -Set of instructions.
- Algorithm expressed in a programming language
Computer Programming: involves writing Instructions and giving
them to computer to compute and complete a task
Algorithm:
Find largest number among two
No's
Algorithm :
Step 1 : Start Find whether a given number is even
or odd?
Step 2 : Read X, Y
Step 3 : when X > Y then print X 1: Start
otherwise Display Y 2 : Read n
Step 4 : Stop 3 : if ( n%2 == 0), write “even number"
otherwise write " odd number"
4 : Stop
Flow Chart
Graphical i.e. pictorial representation of steps involved in
solving a problem step by step by using various shapes and
symbols
Also defined as the diagrammatic representation of an
algorithm.
can be helpful for both writing programs and explaining the
program to others
Easy to understand
5
Flow chart symbols :
6
Flow chart for computing Volume of a sphere
Example: Add two numbers entered by user
Algorithm:
Step 1: Start
Step 2: Read First no a
Step 3: Read Second no b
Step 4: compute sum= a+b
Step 5: Print sum
Step 6: Stop
Example: Calculate area and circumference of a circle
Algorithm:
1. Start
2. Input radius r of the circle
3. compute area= πr2
4. compute circumference cir=2*3.14*r
5. Print area and circumference
6. Stop
Find whether a given number is even or odd?
Algorithm
1: Start
2 : Read n
3 : when ( n%2 == 0), write “even number"
otherwise write " odd number"
4 : Stop
Convert Temperature from Fahrenheit ( ) to Celsius ( )
Algorithm:
Step 1: Read temperature in Fahrenheit,
Step 2: Calculate temperature C=5/9*(F-32),
Step 3: Print C
To determine Whether a Student Passed the Exam or Not
Algorithm:
1. Start
2. Input marks of 4 courses M1, M2, M3 and M4,
3. Calculate the average grade as
Grade=(M1+M2+M3+M4)/4
3. If the average grade is less than 60,
print "FAIL", otherwise print "PASS".
4. Stop
Print number 1 to 20
Algorithm:
1: Initialize X = 0
2: Increment X by 1
3: Print X,
4: If X is less than 20 then go back to step 2.
Largest number among two numbers
Find the largest among three
different numbers entered by the
user.
Algorithm: To determine the largest number among three numbers
1. Start
2. Input a, b, c
3. if a > b goto step 4, otherwise goto step 5
4. if a > c goto step 6, otherwise goto step 8
5. if b > c goto step 7, otherwise goto step 8
6. Output "a is the largest", goto step 9
7 . Display "b is the largest", goto step 9
8 . Print " c is the largest", goto step 9
9 . Stop
Find whether a given number is even or odd?
Algorithm
1: Start
2 : Read n
3 : when ( n%2 == 0), write “even number"
otherwise write " odd number"
4 : Stop
Find Sum of N natural numbers
Algorithm
1. Start
2. Read the value of N.
3. I = 1 , SUM = 0
4. if ( I > N ) go to step 8
5. S = S + I
6. I = I + 1
7. Go to 4
8. Display value of sum S
9. Stop
Compute sum of 50 numbers taken from user
Algorithm:
1: Start
2: set counter X =0
3: initialize sum =0
4: Read number N
5: X=X+1
6: sum= sum +N
7: Is the X greater than 50
If yes, Display sum: Go to step 8.
If No, Go to step 4
8 : Stop
Factorial of a number
Algorithm: factorial of a number
1 : Start
2 : Read N
3 : set i=1 and fact = 1
4 : Is i <= N, when YES Goto step 5
otherwise Goto step 7
5 : Calculate fact = fact * i
6 : increment i=i+1 and
go to step 4
7 : Display value of fact.
8 : Stop
Questions
??
* C Program to find Volume and Surface Area of Sphere */
#include <stdio.h>
int main()
{ float radius, sa,Volume;
printf("\n Please Enter the radius of a Sphere \n");
scanf("%f", &radius);
sa = 4 * 3.14 * radius * radius;
Volume = (4.0 / 3) *3.14 * radius * radius * radius;
printf("\n The Surface area of a Sphere = %.2f", sa);
printf("\n The Volume of a Sphere = %.2f", Volume);
return 0;
}