Unit : 3
Algorithm & Flowchart
What is Algorithm | Introduction to Algorithms
Algorithm:
An algorithm is a process or step-by-step instruction for solving a problem. they form the
foundation of writing a program.
Example:
Write an algorithm for adding three numbers.
Step 1: Start
Step 2: Read num 1, numr 2, num 3
Stap 3:Result = num1+num2+num3
Step 4: Print Result
Step 5: Stop
What are the Characteristics of an Algorithm?
Characteristics of an Algorithm
As one would not follow any written instructions to cook the recipe, but only the standard one.
Similarly, not all written instructions for programming are an algorithms. For some instructions to be
an algorithm, it must have the following characteristics:
1. Clear and Unambiguous: The algorithm should be unambiguous. Each of its steps should
be clear in all aspects and must lead to only one meaning.
2. Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined inputs. It
may or may not take input.
3. Well-Defined Outputs: The algorithm must clearly define what output will be yielded and it
should be well-defined as well. It should produce at least 1 output.
4. Finite-ness: The algorithm must be finite, i.e. it should terminate after a finite time.
5. Feasible: The algorithm must be simple, generic, and practical, such that it can be
executed with the available resources. It must not contain some future technology or
anything.
6. Language Independent: The Algorithm designed must be language-independent, i.e. it
must be just plain instructions that can be implemented in any language, and yet the output
will be the same, as expected.
7. Input: An algorithm has zero or more inputs. Each that contains a fundamental operator
must accept zero or more inputs.
8. Output: An algorithm produces at least one output. Every instruction that contains a
fundamental operator must accept zero or more inputs.
9. Definiteness: All instructions in an algorithm must be unambiguous, precise, and easy to
interpret. By referring to any of the instructions in an algorithm one can clearly understand
what is to be done. Every fundamental operator in instruction must be defined without any
ambiguity.
10. Finiteness: An algorithm must terminate after a finite number of steps in all test cases.
Every instruction which contains a fundamental operator must be terminated within a finite
amount of time. Infinite loops or recursive functions without base conditions do not possess
finiteness.
11. Effectiveness: An algorithm must be developed by using very basic, simple, and feasible
operations so that one can trace it out by using just paper and pencil.
Advantages of Algorithms:
● It is easy to understand.
● An algorithm is a step-wise representation of a solution to a given problem.
● In an Algorithm the problem is broken down into smaller pieces or steps hence, it is easier
for the programmer to convert it into an actual program.
Disadvantages of Algorithms:
● Algorithms are time-consuming.
● Difficult to show Branching and Looping in Algorithms.
● Big tasks are difficult to put in Algorithms.
Examples:
1. Write the algorithm to print IT SYSTEMS.
Step 1: Start
Step 2: Print(“IT SYSTEMS”)
Step 3: Stop
2. Write the algorithm to add 10 and 20 and print the sum
Step 1: Start
Step 2: A = 10
Step 3: B = 20
Step 4: Result = A + B
Step 5: Print(Result)
Step 6: Stop
3. Write the algorithm to multiply 10, 20, 30. print the product
Step 1: Start
Step 2: A = 10
Step 3: B = 20
Step 4: C = 30
Step 5: Result = A*B*C
Step 6: Print(Result)
Step 7: Stop
4. Write an algorithm to calculate the area of a triangle.
Standard algorithm: Logical algorithm:
Step 1: Start Step 1: Start
Step 2: Declare the variables Step 2: Read base, height
Stap 3: Area of triangle Stap 3: Area = ½ (base x height)
Step 4: Print Area Step 4: Print Area
Step 5: Stop Step 5: Stop
5. Write an algorithm to find the greater of two numbers:
Standard algorithm:
Step 1: Start
Step 2: Declare the variables a,b
Step 3: Read Variables a,b
Step 4: If a>b
Yes, print a
No, print b
Step 5: Stop
6. algorithm to multiply 2 numbers and print the result:
Step 1: Start
Step 2: Get the knowledge of input. Here we need 3 variables; a and b will be the user input
and c will hold the result.
Step 3: Declare a, b, and c variables.
Step 4: Take input for a and b variables from the user.
Step 5: Know the problem and find the solution using operators, data structures and logic
We need to multiply the a and b variables so we use the * operator and assign the result to
c. That is c <- a * b
Step 6: Check how to give output, Here we need to print the output. So write print c
Step 7: End
7. Write an algorithm to find the average of 3 subjects.
Follow the algorithm approach as below:
Step 1: Start the Program
Step 2: Declare and Read 3 Subject, let’s say S1, S2, S3
Step 3: Calculate the sum of all the 3 Subject values and store the result in the Sum
variable (Sum = S1+S2+S3)
Step 4: Divide the Sum by 3 and assign it to the Average variable. (Average = Sum/3)
Step 5: Print the value of the Average of 3 Subjects
Step 6: End
8. Algorithm to determine if a given year is a leap year:
Version 1:
1. Start
2. Accept an integer input year.
3. If year is evenly divisible by 4, go to step 4. Otherwise, go to step 7.
4. If year is evenly divisible by 100, go to step 5. Otherwise, go to step 6.
5. If year is evenly divisible by 400, go to step 6. Otherwise, go to step 7.
6. Return true (year is a leap year).
7. Return false (year is not a leap year).
8. Stop
Version 2:
1. Start
2. Is the year divisible by 4?
3. Yes: Go to step 4.
4. No: Go to step 7.
5. Is the year divisible by 100?
6. Yes: Go to step 5.1.
7. No: Go to step 6.
8. Is the year divisible by 400?
9. Yes: Go to step 7.1.
10. No: Go to step 8.
11. Return "Yes, it's a leap year." and stop.
12. Return "No, it's not a leap year." and stop.
Assignment:-
9. Write an algorithm to find the area of rectangular. length is 3 and breath is 5?
10. Write an algorithm to find simple.
the principal is 10,000, the rate is 3.5% and Time is 3 years.