0% found this document useful (0 votes)
63 views36 pages

UNIT 1 - Program Logic Development

Here is a flowchart to find the largest number between A and B: START → Input A, B → Compare A and B → IF A>B → Largest number is A → PRINT A → STOP ELSE → Largest number is B → PRINT B → STOP
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
63 views36 pages

UNIT 1 - Program Logic Development

Here is a flowchart to find the largest number between A and B: START → Input A, B → Compare A and B → IF A>B → Largest number is A → PRINT A → STOP ELSE → Largest number is B → PRINT B → STOP
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 36

UNIT-I

Program Logic
Development
Fundamentals Of Algorithms

What is algorithm?
Definition 1:
An algorithm is a procedure or step-by-step instruction for solving a
problem. They form the foundation of writing a program.

Definition 2:
An algorithm is clearly specified set of instructions describing the
solution to a specific problem.
For writing any programs, the following has to be
known:
● Input
● Tasks to be performed
● Output expected
An Algorithm
● Takes input and transform it into an adequate output
● Must be independent from any programming language
● Is written in a level of detail that allows to reproduce it
in any programming language
● Has to be designed so it can be reused and understood
by others.
How to Write an Algorithm?

● There are no well-defined standards for writing algorithms.


● Rather, it is problem and resource dependent.
● Algorithms are never written to support a particular programming
code.
● Algorithms tell the programmers how to code the program.
● We write algorithms in a step-by-step manner, but it is not always
the case.

● Algorithm writing is a process and is executed after the problem


domain is well-defined. (That is, we should know the problem
domain, for which we are designing a solution.)
Write an algorithm to find sum of two numbers

2+4=6
8+9=17
88+90=
1.2+2.6

c=a+b
Problem − Design an algorithm to add two numbers and display the result.
Step 1 − START
Step 2 − declare three integers a, b & c
Step 3 − define values of a & b
Step 4 − add values of a & b
Step 5 − store output of step 4 to c
Step 6 − print c
Step 7 − STOP
Problem − Design an algorithm to add two numbers and display the result.

Step 1 − START ADD


Step 2 − get values of a & b
Step 3 − c ← a + b
Step 4 − display c
Step 5 − STOP
Problem − Design an algorithm to subtract two numbers and display the result.

Step 1 − START
Step 2 − get values of p & q
Step 3 − x = p - q
Step 4 − display x
Step 5 − STOP
Problem − Design an algorithm to multiply three numbers and display the result.

Step 1 − START
Step 2 − accept/get/input/read values of a , b and c
Step 3 − x = a * b * c
Step 4 − display/print/output x
Step 5 − STOP
Write an algorithm to find area of circle
Step 1 − START
Area = 3.14*r*r
Step 2 − accept r
Step 3 − area=3.14*r*r
Step 4 − display area
Step 5 − STOP
Write an algorithm to find area of square
Step 1 − START
Step 2 − Accept value of side Area = s*s
Step 3 − area = side * side
Step 4 − display area
Step 5 − STOP
Write an algorithm to find area of rectangle
Step 1 − Start
Step 2 − Input value of l and b Area = l*b
Step 3 − area = l * b
Step 4 − display area
Step 5 − STOP
Pseudo-code :
● Pseudo code is not an actual programming language.
● Pseudo code is informal writing style for program algorithm
independent from programming languages to show the
basic concept behind the code.
● Pseudocode is an artificial and informal language that helps
programmers develop algorithms.
● Pseudocode is a "text-based" detail (algorithmic) design tool.
The actions are performed in the same sequence (top to bottom) in which they
are written:

Start

Action 1

Action N

Stop
Selection Structure:
The selection structure is also known as decision making structure. It is
categorized in three types

1. IF-THEN
Pseudo-code:
2. IF-THEN-ELSE
3. CASE IF (condition is true) THEN

List of actions

ELSE

List of different actions

ENDIF
if(rollno>=1 and rollno<=20)
Batch1
Else if( rollno>=21 and rollno<=40)
Batch2
Else if(rollno>=41 and rollno<=60)
Batch3
Pseudo-code:

CASE expression OF

1 condition: sequence 1

2 condition: sequence 2

N condition: sequence N

OTHERS:

Default sequence

ENDCASE
Case 2

1 : “jan”

2 : “feb”

6 : “JUNE”

12: “dec”

OTHERS:

“Wrong value”

ENDCASE
Repetition (looping) Construct:
Looping construct in pseudo code is used when some particular task is to be
repeated for a number of times according to specified condition. Various
types of looping structure :

1. DO-WHILE LOOP
2. REPEAT-UNTIL LOOP
Pseudo-code:

DO WHILE (condition is true)


1st statement
.
.
Nth statement
ENDDO
Pseudo-code:

REPEAT
1st sequence
.
.
Nth sequence
UNTIL (condition is False)
Flowchart
What is a Flowchart?

● Flowchart is a graphical representation of an algorithm.


● Programmers often use it as a program-planning tool to solve a problem.
● It makes use of symbols which are connected among them to indicate the
flow of information and processing.
Symbols of flowchart
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.
On-page Connects two or more parts of a
Connector flowchart, which are on the same page.

Off-page Connects two parts of a flowchart


Connector which are spread over different pages.
Guidelines for preparing Flowchart
● In drawing a proper flowchart, all necessary requirements should be listed out in
logical order.
● The flowchart should be ne t, clear and easy to follow. There should not be any room
for ambiguity in understanding the flowchart.
● The flowchart is to be read left to right or top to bottom.
● A process symbol can have only one flow line coming out of it.
● For a decision symbol, only one flow line can enter it, but multiple lines can leave it to
denote possible answers.
● The terminal symbols can only have one flow line in conjunction with them.
Problem 1: Flowchart for an algorithm which g ts two numbers and prints sum
of their value
Problem 2: Add two numbers entered by the user.
Problem 3: Flowchart to calculate the average of two numbers.
Problem 5: Find the area of circle of radius r.
Problem 6: Convert temperature Fahrenheit to Celsius.
Problem 7: Draw the flowchart to find the largest number between A and B

A=12
B=55
A<B
12<55

You might also like