0% found this document useful (0 votes)
19 views

Algorithm _ Flowchart_Notes(Part-1) (1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Algorithm _ Flowchart_Notes(Part-1) (1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Program Analysis with ALGORITHM AND

FLOW CHART

 Introduction
 Problem Solving
 Algorithm
 Examples of Algorithm
 Properties of an Algorithm
 Flow Chart
 Flow Chart Symbols
 Some Flowchart Examples
 Advantages of Flowcharts

Steps for Problem Solving


 When problems are straightforward and easy, we can easily find the solution.
 But a complex problem requires a methodical approach to find the right solution.
 In other words, we have to apply problem solving techniques.
 Problem solving begins with the precise identification of the problem and ends with a
complete workingsolution in terms of a program or software.

1. Problem Definition
 This is the formal definition of task
 In this stage following points are important:-
1. Definition of input and output
2. Identify variables
3. Identify different variables
4. Check whether formula is required
2. Program design
 In this stage write steps to solve the problem
i.e. either write algorithm or draw flow chart
 This step consist of planning the flow of the program
 It will guide the actual coding of the program.

3. Coding
 This is the step where process of transferring the program design into computer
language.
 This step contains:-
i) Use of variables instead of actual data
ii) Use meaningful names of variables
iii) Keep variable names distinct
iv)Avoid complex instructions
v) Give importance to simplicity

4. Debugging
 This is the step of correction of programming errors.
 It is referred as verification i.e. ensure that the program does what the programmer
want
 Some common errors are:-
i) Failure to initialize variables
ii) Failure to handle condition which can cause endless iterations of code
5. Testing
 It refers to validation of problem
 It includes running the program, testing program for different input values, checking the
output & if any error is there again correcting the errors.

6. Documentation
 It is very important step
 In the documentation part those who use software can understand it & modify it
whenever needed.
 The documentation consist of :
i) Problem definition
ii) flowchart or algorithm
iii) The form of input and output data
iv)Description of files used
v) Program code

Program Analysis Tools


 Algorithm
 Flow chart
 Pseudocode

Algorithm can be defined as


“Logical sequence of steps prepared for solving problem.”
Or
“A sequence of activities to be processed for getting desired output from a given input.”
Or
“An algorithm is a finite step by step list of well-defined instruction for solving a particular
problem”
Then we can say that:
 Getting specified output is essential after algorithm is executed.
 One will get output only if algorithm stops after finite time.
 Activities in an algorithm to be clearly defined in other word for that it to be unambiguous.

Example of Sequential Control Structure


Problem 1: Find the area of a Circle of radius r
Inputs to the algorithm:
Radius r of the Circle.
Expected output:
Area of the Circle

Algorithm:
Step1: Start
Step 2:Read/Input the Radius r of the Circle
Step 3: Area = PI*r*r // calculation of area
Step 4: Print Area
Step 5 : Stop
Problem2: Write an algorithm to read two numbers and find their sum.

Inputs to the algorithm:


First num1.
Second num2.
Expected output:
Sum of the two numbers.

Algorithm:
Step1: Start
Step2: Read/input the first num1.
Step3: Read/input the second num2.
Step4: Sum =num1+num2 // calculation of sum
Step5: Print Sum
Step6: End

Problem3: Write an algorithm to read two float numbers and find their division.
Inputs to the algorithm:
First num1.
Second num2.
Expected output:
division of the two float numbers.
Algorithm:
Step1: Start
Step2: Read/input the first num1.
Step3: Read/input the second num2.
Step4: Initialize Div (of double type)=0
Step5: Div=num1/num2 // calculation of division
Step5: Print Div
Step6: End

Problem 4: Convert temperature Fahrenheit to Celsius


Inputs to the algorithm:
Temperature in Fahrenheit
Expected output:
Temperature in Celsius
Algorithm:
Step1: Start
Step 2: Read Temperature in Fahrenheit F
Step 3: C =5/9*(F-32)
Step 4: Print Temperature in Celsius: C
Step5: End
Problem 5: Write an algorithm to find the square of a number.
• Input: Number whose square is required
Process: Multiply the number by itself to getits square
• Output: Square of the number

Algorithm to find square of a number.


Step 1: Input a number and store it to num
Step 2: square=num * num
Step 3: Print square
Step 4: End

Problem 6: Write an algorithm and program for the use of arithmetic Operators In C++.
(+,-,/,*,%)
• Input:
First num1.
Second num2.
• Output: Use of arithmetic Operators

Algorithm for the use of arithmetic Operators.


Step 1: Start
Step 2 : Read two integer numbers a and b
Step 3 : Print “ Addition of a and b = ” a+b
Step 4 : Print “ subtraction of a and b = ” a-b
Step 5 : Print “ Product of a and b = ” a*b
Step 6 : Print “ Quotient of a and b = ” a/b
Step 7 : Print “ Remainder of a and b = ” a%b
Step 8 : Stop

Control Structures:-
There are three ways in which steps of algorithm carried out:
 Sequential Control Structure
 Conditional Control Structure
 Iterative/Looping Control Structure

1. Sequential Control Structure :- (All above algorithm examples included in Sequential


Control structures
 Modules executed sequentially one after another.

 The sequence may be present explicitly by means

of numberedsteps or by order in which modules are

written.

 Modules executed sequentially one after another.

2.Conditional Control Structure/ Selection Logic structure


 This structure test the condition

 A condition is any variable or expression that returns Boolean value(i.e. True or False)

 When the condition is associated with a statement that is true then respective
statements will be executed.

 It is divided into three types:-


a. Single Alternative
b. Double Alternative
c. Multiple Alternative

(a) Single Alternative


Syntax:-
If condition, then
[module A]
[End of If structure]

 If condition is true then module A, which consist of number of statements, is executed.

 Otherwise module A is skipped and next statement of algorithm is executed.

Example on Conditional Control Structure


Problem 1:-
Write an algorithm to find first number is greater or not from two given
numbers
Inputs to the algorithm:
First num1.
Second num2.
Expected output:
Greater number from two numbers

Algorithm:
Step1: Start
Step 2:Read integer numbers a, b
Step 3 : initialize max=0
Step 4 : If (a>b) Then
max = a
[End of If ]
Step 5 : Print “ Maximum value = ” max
Step 6 : Stop

Problem 2:-
Write an algorithm to check number is even or not?
Inputs to the algorithm:
Integer number
Expected output:
Number is even or not

Algorithm:
Step1: Start
Step 2:PRINT "Enter the Number"
Step 3: Read integer number num
Step 4: If (num % 2 == 0 )Then
PRINT "Number is Even“
[ End of If ]
Step 5 : Stop

(b) Double Alternative


Syntax:-
If condition, then
[module A]
Else:
[module B]
[End of If structure]

 If condition is true then module A, which consist of number of statements, is executed.


 Otherwise module B is executed.

Problem 1:-
Write an algorithm to find smaller of two numbers
Inputs to the algorithm:
First num1.
Second num2.
Expected output:
Smaller number from two numbers

Algorithm:
Step1: Start
Step 2:Read integer numbers a, b
Step 3 : initialize min=0
Step 4 : If (a<b) Then
min= a
Else
min=b
[End of If ]
Step 5 : Print “ Minimum value = ” min
Step 6 : Stop
Problem 2:-
Write an algorithm to check number is even or odd?
Inputs to the algorithm:
Integer number
Expected output:
Number is even or odd

Algorithm:
Step1: Start
Step 2:PRINT "Enter the Number"
Step 3: Read integer number num
Step 4: If (num % 2 == 0 )Then
PRINT "Number is Even“
Else
PRINT "Number is Odd“
[ End of If ]
Step 5 : Stop

Problem 3:-
Write an algorithm to check student Pass or Fail?
Inputs to the algorithm:
Percentage Marks
Expected output:
Student declare as Pass or Fail

Algorithm:
Step1: Start
Step 2:PRINT "Enter the Percentage Marks"
Step 3: Read Marks
Step 4: If Marks < 40 Then
Print “Student is Fail”
Else
Print “Student is Pass”
Step 5 : Stop

Problem 4:-
Write an algorithm to check number is divisible by 5 or not
Inputs to the algorithm:
Integer number
Expected output:
Number is divisible by 5 or not

Algorithm:
Step1: Start
Step 2:PRINT "Enter the Number"
Step 3: Read integer number num
Step 4: If (num % 5 == 0 )Then
PRINT "Number is divisible by 5“
Else
PRINT "Number is not divisible by 5“
[ End of If ]
Step 8 : Stop

(c) Multiple Alternative:-


Syntax

If condition(1), then:
[module A1]
else if condition(2), then:
[module A2]
:
:
else if condition(n), then:
[module An]
else :
[module B]
[End of If structure]

Example on Multipale alternative Control Structure


Problem :- 1
Write an algorithm to find Maximum value from three numbers (Nested if…else)
Inputs to the algorithm:
First num1.
Second num2.
Third num3
Expected output:
larger number from three numbers

Algorithm:
Step1: Start
Step 2:Read integer numbers a, b ,c
Step 3 : initialize max=0
Step 4 :If (a>b) Then
If (a>c) Then
max = a
Else
max=c
Else
If (b>c) Then

max = b
Else
max =c
Step 5 : Print “ Maximum value = ” max
Step 6 : Stop
Problem :- 2
Write an algorithm to find Maximum value from three numbers by using and operator
Inputs to the algorithm:
First num1.
Second num2.
Third num3
Expected output:
larger number from three numbers

Algorithm:
Step1: Start
Step 2:Read integer numbers a, b ,c
Step 3 : initialize max=0
Step 4 :If (a>b) and (a>c) Then
max = a
Else If ( b>a) and (b>c) Then
max = b

Else
max =c
Step 5 : Print “ Maximum value = ” max
Step 6 : Stop
Problem :- 3
Write an algorithm to find out grade of student
Inputs to the algorithm:
Percentage of student
Expected output:
Grade of student

Algorithm:
Step1: Start
Step 2:Read percentage marks of student
Step 3 :If (percentage >= 90)
print “ Student is having A+ grade” and Go to step 4
Else if (percentage >= 80)
print “ Student is having A grade” and Go to step 4
Else if (percentage >= 70)
print “ Student is having B+ grade” and Go to step 4
Else if (percentage >= 60)
print “ Student is having B grade” and Go to step 4
Else if (percentage >= 50)
print “Student is having C grade” and Go to step 4
Else if (percentage >= 40)
print “Student is having Pass Class” and Go to step 4
Else print “ Student is Fail”
[End of If]
Step 4: Stop
Problem :- 4
Write an algorithm to do arithmetic operations on two numbers depending upon the
choice of operator
Inputs to the algorithm:
Two integer numbers num1 & num2
Operator to perform arithmetic operation
Expected output:
Result of entered arithmetic operator

Algorithm:
Step1: Start
Step 2:Read two integer numbers, a & b
Step 3 : Read operator and store in variable ‘ch’
Step 4: If (ch==‘+’) Then
Print “ Addition of a and b = ” a+b and Goto step 5
Else If (ch==‘-’ ) Then
Print “ Subtraction of a and b = ” a-b and Goto step 5
Else If (ch==‘* ’) Then
Print “ Multilication of a and b = ” a * b and Goto step 5
Else If (ch==‘/ ’) Then
Print “ Division of a and b = ” a / b and Goto step 5
Else if ch==‘%’ Then
Print “ Remainder of a and b = ” a % b and Goto step 5
Else
Print “ Invalid input”
[End of If]
Step 5: stop

Problem :- 5
Write an algorithm for the use of conditional, logical, relational Operators to find
minimum and Maximum value from three numbers
Inputs to the algorithm:
First num1.
Second num2.
Third num3
Expected output:
Smaller & larger number from three numbers

Algorithm:
Step1: Start
Step 2:Read integer numbers a, b ,c
Step 3 : initialize min=0 max=0
Step 4 :If (a>b) and (a>c) Then
max = a
Else if ( b>a) and (b>c) Then
max = b
Else
max =c
[End of If]
Step 5 : If (a<b) and (a<c) Then
min = a
Else if ( b<a) and (b<c) Then
min = b
Else
min=c
[End of If]

Step 6 : Print “ Maximum value = ” max


Step 7 : Print “ Minimum value = ” min
Step 8 : Stop

3. Iterative / Repetitive Control Structure


 It is divided into two types:-
a. Repeat While
b. Repeat For

(a) Repeat while


Syntax:-
Initialize counter
Repeat While Condition
[module]
Update Counter
[End of Loop]

 Here body of loop i.e. module is executed repeatedly, until the condition is
satisfied.
 There must be a statement before the structure
that initialize the condition controlling loop counter.
 And one statement which change value of condition counter.

Example on Iterative/ Looping Control Structure


Problem :- 1
Write an algorithm to find sum of n natural numbers using do……while control
structure.
Inputs to the algorithm:
Enter number till you want to find out addition
Expected output:
Sum of n natural numbers

Algorithm:
Step1: Start
Step 2: Read N
Step 3: Initialize I= 1, sum =0
Step 4: Repeat steps 5 and 6 while (I<=N)
Step 5: sum = sum +I
Step 6: Set I= I+1
[End of do …. While]
Step 7: Print sum
Step8 : stop

Problem :- 2
Write an algorithm to print table of number using do……while control structure.
Inputs to the algorithm:
Enter number
Expected output:
Table of number

Step1: Start
Step 2: Initialize I= 1, T= 1
Step 3: Read N
Step 4: Repeat steps 5, 6 and 7 while (I <= 10)
Step 5: T = N * I
Step 6: Print T
Step7 : Set I= I+1
[End of do …. While]
Step 8 : stop

You might also like