SlideShare a Scribd company logo
Programming for Problem
Solving
CSE-113
Prepared By:
Mr. Amit Goel
(Assistant Professor)
Department of CSE
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.
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.
FLOW CHART OF LEAP YEAR
FLOW CHART OF LEAP YEAR
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
Example Flowcharts
• Here is the flowchart for going to the market to purchase a pen.
Example Flowcharts
• Here is a flowchart to calculate the average of two numbers.
C Code
• #include <stdio.h>
• int main()
• {
• unsigned int year=0;
• printf("Enter the year:");
• scanf("%u",&year);
• if ((year % (4)) == (0))
• {
• if ((year % (100)) == (0))
• {
• if ((year % (400)) == (0))
• {
• printf("%u is a Leap Year.nnn",year);
C Code
• }
• else
• {
• printf("%u is Not a Leap Year.nnn",year);
• }
• }
• else
• {
• printf("%u is a Leap Year.nnn",year);
• }
• }
• else
• {
C Code
• printf("%u is Not a Leap Year.nnn",year);
• }
• return 0;
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
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
• }
If Statement: FLOW CHART
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;
• }
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.
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
• }
Flowchart of the if-else statement in C
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;
• }
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.
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
• }
Flowchart of else-if ladder statement in C
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;
• }
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”.
Characteristics of an Algorithm
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
• 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.
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.
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.
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.
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.
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.
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
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
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;
}
writing pseudo-code from algorithm
Flow chart- Addition of 2 numbers:
THANK YOU!

More Related Content

Similar to CSE-113 UNIT-1 with the basic knowledge of the computer science.pptx (20)

PDF
Fundamental of Information Technology - UNIT 6
Shipra Swati
 
PPTX
Unit-1_PPS_H6nS4p8J1iooinoinoinoink.pptx
rannbirhealer
 
PPTX
Algorithm and flowchart.pptx
MaheShiva
 
PPTX
Lecture 02: Preliminaries of Data structure
Nurjahan Nipa
 
PPTX
s-INTRODUCTION TO PROBLEM SOLVING PPT.pptx
ShamithRai
 
PPTX
UNIT I.pptxpython unit 1 engineering full unit completed
pavithrad67
 
PPTX
PROBLEM SOLVING.pptx
sumathysenthilkumar
 
PPT
Lecture 01 - Introduction and Review.ppt
MaiGaafar
 
PDF
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Computer Programmer
 
PDF
Unit 1-problem solving with algorithm
rajkumar1631010038
 
PPTX
Algorithms and Flowcharts
Deva Singh
 
PDF
classVIII_Coding_Book018979929470479.pdf
menolem379
 
PPTX
classVIII_Coding_Teacher_Presentation.pptx
bhanutickets
 
PPTX
UNIT 1.pptx
ShaswatSurya
 
PPT
Unit 1 python (2021 r)
praveena p
 
PPTX
Algorithm and flowchart with pseudo code
hamza javed
 
PDF
GE3151 PSPP _Unit 1 notes and Question bank.pdf
Guru Nanak Technical Institutions
 
PPTX
UNIT-1.pptx python for engineering first year students
SabarigiriVason
 
PDF
Algorithm chapter 1
chidabdu
 
Fundamental of Information Technology - UNIT 6
Shipra Swati
 
Unit-1_PPS_H6nS4p8J1iooinoinoinoink.pptx
rannbirhealer
 
Algorithm and flowchart.pptx
MaheShiva
 
Lecture 02: Preliminaries of Data structure
Nurjahan Nipa
 
s-INTRODUCTION TO PROBLEM SOLVING PPT.pptx
ShamithRai
 
UNIT I.pptxpython unit 1 engineering full unit completed
pavithrad67
 
PROBLEM SOLVING.pptx
sumathysenthilkumar
 
Lecture 01 - Introduction and Review.ppt
MaiGaafar
 
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Computer Programmer
 
Unit 1-problem solving with algorithm
rajkumar1631010038
 
Algorithms and Flowcharts
Deva Singh
 
classVIII_Coding_Book018979929470479.pdf
menolem379
 
classVIII_Coding_Teacher_Presentation.pptx
bhanutickets
 
UNIT 1.pptx
ShaswatSurya
 
Unit 1 python (2021 r)
praveena p
 
Algorithm and flowchart with pseudo code
hamza javed
 
GE3151 PSPP _Unit 1 notes and Question bank.pdf
Guru Nanak Technical Institutions
 
UNIT-1.pptx python for engineering first year students
SabarigiriVason
 
Algorithm chapter 1
chidabdu
 

Recently uploaded (20)

PDF
All chapters of Strength of materials.ppt
girmabiniyam1234
 
PPTX
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
PDF
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
PPTX
Basics of Auto Computer Aided Drafting .pptx
Krunal Thanki
 
PPTX
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
PDF
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
Online Cab Booking and Management System.pptx
diptipaneri80
 
PPTX
Introduction to Fluid and Thermal Engineering
Avesahemad Husainy
 
PPTX
Precedence and Associativity in C prog. language
Mahendra Dheer
 
PPTX
Ground improvement techniques-DEWATERING
DivakarSai4
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
cybersecurityandthe importance of the that
JayachanduHNJc
 
PDF
SG1-ALM-MS-EL-30-0008 (00) MS - Isolators and disconnecting switches.pdf
djiceramil
 
PPTX
Information Retrieval and Extraction - Module 7
premSankar19
 
PDF
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
PDF
Zero carbon Building Design Guidelines V4
BassemOsman1
 
PDF
勉強会資料_An Image is Worth More Than 16x16 Patches
NABLAS株式会社
 
All chapters of Strength of materials.ppt
girmabiniyam1234
 
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
Basics of Auto Computer Aided Drafting .pptx
Krunal Thanki
 
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Online Cab Booking and Management System.pptx
diptipaneri80
 
Introduction to Fluid and Thermal Engineering
Avesahemad Husainy
 
Precedence and Associativity in C prog. language
Mahendra Dheer
 
Ground improvement techniques-DEWATERING
DivakarSai4
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
cybersecurityandthe importance of the that
JayachanduHNJc
 
SG1-ALM-MS-EL-30-0008 (00) MS - Isolators and disconnecting switches.pdf
djiceramil
 
Information Retrieval and Extraction - Module 7
premSankar19
 
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
Zero carbon Building Design Guidelines V4
BassemOsman1
 
勉強会資料_An Image is Worth More Than 16x16 Patches
NABLAS株式会社
 
Ad

CSE-113 UNIT-1 with the basic knowledge of the computer science.pptx

  • 1. Programming for Problem Solving CSE-113 Prepared By: Mr. Amit Goel (Assistant Professor) Department of CSE
  • 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.
  • 4. FLOW CHART OF LEAP YEAR
  • 5. FLOW CHART OF LEAP YEAR
  • 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
  • 7. Example Flowcharts • Here is the flowchart for going to the market to purchase a pen.
  • 8. Example Flowcharts • Here is a flowchart to calculate the average of two numbers.
  • 9. C Code • #include <stdio.h> • int main() • { • unsigned int year=0; • printf("Enter the year:"); • scanf("%u",&year); • if ((year % (4)) == (0)) • { • if ((year % (100)) == (0)) • { • if ((year % (400)) == (0)) • { • printf("%u is a Leap Year.nnn",year);
  • 10. C Code • } • else • { • printf("%u is Not a Leap Year.nnn",year); • } • } • else • { • printf("%u is a Leap Year.nnn",year); • } • } • else • {
  • 11. C Code • printf("%u is Not a Leap Year.nnn",year); • } • return 0;
  • 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 • }
  • 18. Flowchart of the if-else statement in C
  • 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 • }
  • 22. Flowchart of else-if ladder statement in C
  • 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; }
  • 45. writing pseudo-code from algorithm Flow chart- Addition of 2 numbers: