Sie sind auf Seite 1von 13

CHAPTER 2

FLOWCHARTS AND
ALGORITHMS

Introduction

Flowchart and Algorithm are tools to


develop logic of programs. They are
written before the program is written.
Flowchart is a diagrammatic
representation of the solution, while
Algorithm is an English like pseudo
language of the solution.

Prepared by Author: Prof S M Shah, Mahajan Publishing


House

Flowchart

A Flowchart is graphical or diagrammatical


representation of sequence of any problem to
be solved by computer programming language.
It is drawn before writing any program.
Through flowchart it is easy to understand the
logic and sequence of problem. After drawing
flowchart it becomes easy to write any
program. There are various standard symbols
available to represent logic of problem which
are ..
Prepared by Author: Prof S M Shah, Mahajan Publishing
House

Symbols of flowchart

Start and Stop


Input / Output:
Process or expression representation
Decision
Direction of data flow or flow lines
Connector
loop

Prepared by Author: Prof S M Shah, Mahajan Publishing


House

Algorithm

An algorithm is a finite sequence of


well defined steps or operations for
solving a problem in systematic manner.
Instruction are written in the natural
language. It is also called step by step
solution.

Prepared by Author: Prof S M Shah, Mahajan Publishing


House

Algorithm - Examples

Write an algorithm to find out sum


of two numbers.
Step 1 : input two numbers : a,b
Step 2 : calculate sum = a+b
Step 3 : Print total = ,sum
Step 4 : stop

Prepared by Author: Prof S M Shah, Mahajan Publishing


House

Algorithm - Examples

Write a algorithm to find out are of


square.
Step 1 : input length : len
Step 2 : calculate area=len*len
Step 3 : print area = , area
Step 4 : stop

Prepared by Author: Prof S M Shah, Mahajan Publishing


House

Algorithm - Examples

Write an algorithm to find average


of three numbers.
Step 1 : input three numbers : a,b,c
Step 2 : calculate sum=a+b+c
Step 3 : avg= sum /3
Step 4 : print average = ,avg
Step 5 : stop

Prepared by Author: Prof S M Shah, Mahajan Publishing


House

Algorithm - Examples

Write an algorithm to find out minimum of three


numbers.
Step 1 : input three numbers : a,b,c
Step 2 : if a < b then go to next step otherwise (else) go to
step 8
Step 3 : if a < c then go to next step else go to step 6
Step 4 : print minimum = , a
Step 5 : stop
Step 6 : print minimum = , c
Step 7 : stop
Step 8 : if b < c then go to next step else go to step 11
Step 9 : print minimum = , b
Step 10 : stop
Step 11 : print minimum = , c
Step 12 : stop
Prepared by Author: Prof S M Shah, Mahajan Publishing
House

Algorithm - Examples

Write an algorithm to find factorial of


given number
Step 1 : input number : num
Step 2 : i=1
Step 3 : f = 1
Step 4 : repeat from step 4 to step 6 until I
<= num
Step 5 : f=f *i
Step 6 : i=i+1
Step 7 : print factorial = ,f
Step 8 : stop
Prepared by Author: Prof S M Shah, Mahajan Publishing
House

Algorithm - Examples

Write an algorithm to solve following series 1! +


2! + 3! + .. + n!
Step 1 : input number : num
Step 2 : f=1
Step 3 : sum=0
Step 4 : I =1
Step 5 : repeat from step 5 to step 8 until I <=
num
Step 6 : f=f*I
Step 7 : sum=sum + f
Step 8 : I = I +1
Step 9 : print sum = << sum
Step 10 : stop
Prepared by Author: Prof S M Shah, Mahajan Publishing
House

Algorithm - Examples

Write an algorithm to find maximum number from n


numbers.
Step 1 : input first number : num
Step 2 : max = num
Step 3 : print how many different numbers
Step 4 : input n
Step 5 : I = 2
Step 6 : repeat step 6 through step 11 until I <= n
Step 7 : print input next number
Step 8 : input num
Step 9 : if num > max then next step else go to step 11
Step 10 : max = num
Step 11 : I = I + 1
Step 12 : print maximum = , max
Step 13 : stop
Prepared by Author: Prof S M Shah, Mahajan Publishing
House

Algorithm - Examples

Write an algorithm to find whether given


number is prime or not
Step 1 : read num
Step 2 : I = 2
Step 3 : repeat through step 3 to step 7 until I <
num
Step 4 : if num % I = 0 then go to next step else go
to step 7
Step 5 : print num is not prime
Step 6 : stop
Step 7 : I = I + 1
Step 8 : print num, is prime
Step 9 : stop
Prepared by Author: Prof S M Shah, Mahajan Publishing
House

Das könnte Ihnen auch gefallen