2. FLOW CHART
•Flowchart is a diagrammatic representation
of sequence of logical steps of a program.
•Flowcharts use simple geometric shapes to
depict processes and arrows to show
relationships and process/data flow.
3. Flowchart Symbols
• Here is a chart for some of the common symbols used in
drawing flowcharts.
Symbol Symbol Name Purpose
Start/Stop
Used at the beginning and
end of the algorithm to
show start and end of the
program.
Process
Indicates processes like
mathematical operations.
Input/ Output
Used for denoting program
inputs and outputs.
Decision
Stands for decision
statements in a program,
where answer is usually Yes
or No.
Arrow
Shows relationships
between different shapes.
6. Guidelines for Developing Flowcharts
• These are some points to keep in mind while developing a
flowchart −
• Flowchart can have only one start and one stop symbol
• General flow of processes is top to bottom or left to right
• Arrows should not cross each other
12. C if else Statement
• The if-else statement in C is used to perform the operations based on
some specific condition. The operations specified in if block are
executed if and only if the given condition is true.
• There are the following variants of if statement in C language.
1. If statement
2. If-else statement
3. If else-if ladder
4. Nested if
13. If Statement
• The if statement is used to check some given condition and perform
some operations depending upon the correctness of that condition. It is
mostly used in the scenario where we need to perform the different
operations for the different conditions. The syntax of the if statement
is given below.
• if(expression)
• {
• //code to be executed
• }
15. Let's see a simple example of C language if statement.
• #include<stdio.h>
• int main()
• {
• int number=0;
• printf("Enter a number:");
• scanf("%d",&number);
• if(number%2==0)
• {
• printf("%d is even number",number);
• }
• return 0;
• }
16. If-else Statement
• The if-else statement is used to perform two operations for a single
condition.
• The if-else statement is an extension to the if statement using which,
we can perform two different operations, i.e., one is for the correctness
of that condition, and the other is for the incorrectness of the
condition.
• Here, we must notice that if and else block cannot be executed
simultaneously.
• Using if-else statement is always preferable since it always invokes an
otherwise case with every if condition.
17. The syntax of the if-else statement is given below.
• if(expression)
• {
• //code to be executed if condition is true
• }
• else
• {
• //code to be executed if condition is false
• }
19. Example to check whether a number is even or odd
using if-else statement in C language.
• include<stdio.h>
• int main(){
• int number=0;
• printf("enter a number:");
• scanf("%d",&number);
• if(number%2==0){
• printf("%d is even number",number);
• }
• else{
• printf("%d is odd number",number);
• }
• return 0;
• }
20. If else-if ladder Statement
• The if-else-if ladder statement is an extension to the if-else statement.
• It is used in the scenario where there are multiple cases to be performed for
different conditions.
• In if-else-if ladder statement, if a condition is true then the statements defined in
the if block will be executed, otherwise if some other condition is true then the
statements defined in the else-if block will be executed, at the last if none of the
condition is true then the statements defined in the else block will be executed.
• There are multiple else-if blocks possible.
• It is similar to the switch case statement where the default is executed instead of
else block if none of the cases is matched.
21. If else-if ladder Statement
• if(condition1){
• //code to be executed if condition1 is true
• }else if(condition2){
• //code to be executed if condition2 is true
• }
• else if(condition3){
• //code to be executed if condition3 is true
• }
• ...
• else{
• //code to be executed if all the conditions are false
• }
23. The example of an if-else-if statement in C language is given below.
• #include<stdio.h>
• int main(){
• int number=0;
• printf("enter a number:");
• scanf("%d",&number);
• if(number==10){
• printf("number is equals to 10");
• }
• else if(number==50){
• printf("number is equal to 50");
• }
• else if(number==100){
• printf("number is equal to 100");
• }
• else{
• printf("number is not equal to 10, 50 or 100");
• }
• return 0;
• }
24. ALGORITMS
• The word Algorithm means ” A set of rules to be followed in
calculations or other problem-solving operations ”
Or
A procedure for solving a mathematical problem in a finite number of
steps that frequently involves recursive operations”.
26. Characteristics of an Algorithm
• Clear and Unambiguous: The algorithm should be clear and unambiguous. Each
of its steps should be clear in all aspects and must lead to only one meaning.
• Well-Defined Inputs: If an algorithm says to take inputs, it should be well-
defined inputs.
• Well-Defined Outputs: The algorithm must clearly define what output will be
yielded and it should be well-defined as well.
• Finite-ness: The algorithm must be finite, i.e. it should terminate after a finite
time.
• 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.
• 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.
27. Properties of Algorithm:
• It should terminate after a finite time.
• It should produce at least one output.
• It should take zero or more input.
• It should be deterministic means giving the same output for the same
input case.
• Every step in the algorithm must be effective i.e. every step should do
some work.
28. Types of Algorithms:
• Brute Force Algorithm: It is the simplest approach for a problem. A
brute force algorithm is the first approach that comes to finding when
we see a problem.
• 2. Recursive Algorithm: A recursive algorithm is based on recursion.
In this case, a problem is broken into several sub-parts and called the
same function again and again.
• 3. Backtracking Algorithm: The backtracking algorithm basically
builds the solution by searching among all possible solutions. Using
this algorithm, we keep on building the solution following criteria.
Whenever a solution fails we trace back to the failure point and build
on the next solution and continue this process till we find the solution
or all possible solutions are looked after.
29. Types of Algorithms:
• 4. Searching Algorithm: Searching algorithms are the ones that are
used for searching elements or groups of elements from a particular
data structure. They can be of different types based on their approach
or the data structure in which the element should be found.
• 5. Sorting Algorithm: Sorting is arranging a group of data in a
particular manner according to the requirement. The algorithms which
help in performing this function are called sorting algorithms.
Generally sorting algorithms are used to sort groups of data in an
increasing or decreasing manner.
• 6. Hashing Algorithm: Hashing algorithms work similarly to the
searching algorithm. But they contain an index with a key ID. In
hashing, a key is assigned to specific data.
30. Types of Algorithms:
• 7. Divide and Conquer Algorithm: This algorithm breaks a problem into sub-
problems, solves a single sub-problem and merges the solutions together to get
the final solution. It consists of the following three steps:
• Divide
• Solve
• Combine
• 8. Greedy Algorithm: In this type of algorithm the solution is built part by part.
The solution of the next part is built based on the immediate benefit of the next
part. The one solution giving the most benefit will be chosen as the solution for
the next part.
• 9. Dynamic Programming Algorithm: This algorithm uses the concept of
using the already found solution to avoid repetitive calculation of the same part
of the problem. It divides the problem into smaller subproblems and solves
them.
31. Types of Algorithms:
• 7. Divide and Conquer Algorithm: This algorithm breaks a problem into sub-
problems, solves a single sub-problem and merges the solutions together to get
the final solution. It consists of the following three steps:
• Divide
• Solve
• Combine
• 8. Greedy Algorithm: In this type of algorithm the solution is built part by part.
The solution of the next part is built based on the immediate benefit of the next
part. The one solution giving the most benefit will be chosen as the solution for
the next part.
• 9. Dynamic Programming Algorithm: This algorithm uses the concept of
using the already found solution to avoid repetitive calculation of the same part
of the problem. It divides the problem into smaller subproblems and solves
them.
32. Advantages of Algorithms
• It is easy to understand.
• An algorithm is a step-wise representation of a solution to a
given problem.
• In 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.
33. Disadvantages of Algorithms:
• Writing an algorithm takes a long time so it is time-
consuming.
• Understanding complex logic through algorithms can be
very difficult.
• Branching and Looping statements are difficult to show in
Algorithms.
34. How to Design an Algorithm?
• In order to write an algorithm, the following things are needed as a
pre-requisite:
1.The problem that is to be solved by this algorithm i.e. clear problem
definition.
2.The constraints of the problem must be considered while solving the
problem.
3.The input to be taken to solve the problem.
4.The output to be expected when the problem is solved.
5.The solution to this problem, is within the given constraints.
• Then the algorithm is written with the help of the above parameters
such that it solves the problem.
35. Example: Consider the example to add three numbers and print the sum.
• Step 1: Fulfilling the pre-requisites
• As discussed, in order to write an algorithm, its pre-requisites must be fulfilled.
• The problem that is to be solved by this algorithm: Add 3 numbers and print their sum.
• The constraints of the problem that must be considered while solving the problem: The
numbers must contain only digits and no other characters.
• The input to be taken to solve the problem: The three numbers to be added.
• The output to be expected when the problem is solved: The sum of the three numbers
taken as the input i.e. a single integer value.
• The solution to this problem, in the given constraints: The solution consists of adding the
3 numbers. It can be done with the help of ‘+’ operator, or bit-wise, or any other method.
36. • Step 2: Designing the algorithm
Now let’s design the algorithm with the help of the above pre-
requisites:
• Algorithm to add 2 numbers and print their sum:
• START
• Declare 2 integer variables num1 and num2.
• Take the two numbers, to be added, as inputs in variables num1 and
num2 respectively.
• Declare an integer variable sum to store the resultant sum of the 3
numbers.
• Add the 2 numbers and store the result in the variable sum.
• Print the value of the variable sum
• END
• Step 3: Testing the algorithm by implementing it.
In order to test the algorithm, let’s implement it in C language.
37. Problem solving approach(top down/bottom
up approach)
Bottom-Up Model
• Bottom-Up Model is a system design approach where parts of the system
are defined in details. Once these parts are designed and developed, then
these parts or components are linked together to prepare a bigger
component. This approach is repeated until the complete system is built.
Top-Down Model
• Top-Down Model is a system design approach where design starts from the
system as a whole. Complete System is then divided into smaller sub-
applications with more details. Each part again goes through the top-down
approach till the complete system is designed with all minute details. Top
Down approach is also termed as breaking the bigger problem into smaller
problems and solving them individually in recursive manner.
38. Problem solving approach(top down/bottom
up approach)
Sr. No. Key Bottom-Up Model Top-Down Model
1
Focus In Bottom-Up Model, the focus is on identifying and
resolving smallest problems and then integrating them
together to solve the bigger problem.
In Top-down Model, the
focus is on breaking the
bigger problem into
smaller one and then
repeat the process with
each problem.
2
Language Bottom-Up Model is mainly used by object oriented
programming languages like Java, C++ etc.
Top-Down Model is
followed by structural
programming languages
like C, Fortran etc.
3
Redundancy Bottom-Up model is better suited as it ensures minimum
data redundancy and focus is on re-usability.
Top-down model has high
ratio of redundancy as
the size of project
increases.
39. Problem solving approach(top down/bottom
up approach)
Sr. No. Key Bottom-Up Model Top-Down Model
4
Interaction Bottom-Up model have high interactivity between
various modules.
Top-down model has tight
coupling issues and low
interactivity between
various modules.
5
Approach Bottom-up model is based on composition approach. Top-down model is based
on decomposition
approach.
6
Issues In Bottom-Up, some time it is difficult to identify overall
functionality of system in initial stages.
In Top-Down, it may not
be possible to break the
problem into set of
smaller problems.
40. Pseudo code
• Pseudo code: It’s simply an implementation of an algorithm in
the form of annotations and informative text written in plain
English. It has no syntax like any of the programming language
and thus can’t be compiled or interpreted by the computer.
• Pseudocode is understood by the programmers of all types. it enables the
programmer to concentrate only on the algorithm part of the code
development. It cannot be compiled into an executable program. Example,
C code : if (i < 10)
{
i++;
}
pseudocode :if i is less than 10, increment i by 1.
41. Difference between Algorithm, Pseudocode and Program
• Algorithm : Systematic logical approach which is a well-
defined, step-by-step procedure that allows a computer to
solve a problem.
• Pseudocode : It is a simpler version of a programming
code in plain English which uses short phrases to write code
for a program before it is implemented in a specific
programming language.
• Program : It is exact code written for problem following all
the rules of the programming language.
42. writing pseudo-code from algorithm
• Algorithm to add 2 numbers and print their sum:
• START
• Declare 2 integer variables num1 and num2.
• Take the two numbers, to be added, as inputs in variables num1
and num2 respectively.
• Declare an integer variable sum to store the resultant sum of the 2
numbers.
• Add the 2 numbers and store the result in the variable sum.
• Print the value of the variable sum
• END
43. writing pseudo-code from algorithm
Pseudo-code to add 2numbers:
• Begin
• WRITE “Please enter two numbers to add”
• READ num1
• READ num2
• Sum = num1+num2
• WRITE Sum
• End
44. writing pseudo-code from algorithm
C-code to add 2numbers:
#include <stdio.h>
int main()
{
int a, b, sum = 0;
printf("Enter two numbers a and b : n");
scanf("%d%d", &a, &b);
sum = a + b;
printf("Sum of a and b is: %d", sum);
return 0;
}