Module - 02 - Algorithm and Flow Charts
Module - 02 - Algorithm and Flow Charts
Algorithm:
Step1: Read //input the Radius r of the Circle
Step2: Area PI*r*r // calculation of area
Step3: Print Area
Problem2: Write an algorithm to read two numbers and find their sum.
Algorithm:
Step1: Start
Step2: Read num1 //input the first num1.
Step3: Read num2 //input the second num2.
Step4: Sum num1+num2 // calculation of sum
Step5: Print Sum
Step6: End
Algorithm:
Step 1: 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-1: write algorithm to find the greater number between two numbers
Inputs to the algorithm: Number 1(A) and Number 2(B)
Expected output: Greater number between A and B
Algorithm:
Step1: Start
Step2: Read //input A and B
Step3: If A greater than B then C=A
Step4: if B greater than A then C=B
Step5: Print C
Step6: End
Problem-2: An algorithm to find the largest value of any three numbers.
Inputs to the algorithm: Number 1(A), Number 2(B) and Number 3(C)
Expected output: Greatest number between A, B and C
Algorithm:
Step1: Start
Step2: Read //input A, B and C
Step3: If (A>=B) and (A>=C) then Max=A
Step4: If (B>=A) and (B>=C) then Max=B
Step5: If (C>=A) and (C>=B) then Max=C
Step6: Print Max
Step7: End
Examples:
Problem1: An algorithm to calculate even numbers between 0 and 99
Algorithm:
Step1: Start
Step2: I ← 0
Step3: Write I in standard output
Step4: I ← I+2
Step5: If (I <=98) then go to step 3
Step6: End
Problem2: Design an algorithm which generates even numbers between 1000
and 2000 and then prints them in the standard output. It should also print total
sum:
Algorithm:
1. Start
2. I ← 1000 and S ← 0
3. Write I
4. S←S+I
5. I←I+2
6. If (I <= 2000) then go to line 3
Else go to line 7
7. Write S
8. End
Properties of algorithm:
Donald Ervin Knuth has given a list of five properties for an algorithm. These
properties are:
1) Finiteness: An algorithm must always terminate after a finite number of
steps. It means after every step one reach closer to solution of the problem and
after a finite number of steps algorithm reaches to an end point.
2) Definiteness: Each step of an algorithm must be precisely defined. It is done
by well thought actions to be performed at each step of the algorithm. Also the
actions are defined unambiguously for each activity in the algorithm.
3) Input: Any operation you perform need some beginning value/quantities
associated with different activities in the operation. So the value/quantities are
given to the algorithm before it begins.
4) Output: One always expects output/result (expected value/quantities) in
terms of output from an algorithm. The result may be obtained at different stages
of the algorithm. If some result is from the intermediate stage of the operation
then it is known as intermediate result and result obtained at the end of algorithm
Examples of Flowchart:
Problem1: Find the area of a circle of radius r.
Problem 3: Algorithm for find the greater number between two numbers.
Problem 4: Flowchart for the problem of printing even numbers between 9 and
100.
Yes No
is
A>B
Print A Print B
print B
endif Print A Print B
2. NESTED IFS:
One of the alternatives within an IF–THEN–ELSE statement
may involve further IF–THEN–ELSE statement
Example: Write an algorithm that reads three numbers and prints the value
of the largest number.
Algorithm:
Flowchart:
Flowchart:
Exercise 03. Draw an algorithm & flowchart to find Even number in between 1
to 50
Algorithm: Flowchart:
Step 1: Start
Step 2: I = 1
Step 3: IF (I >50) THEN
GO TO Step-7
END IF
Step 4: IF ( (I % 2) =0) THEN
Display I
END IF
Step 5: I = I + 1
Step 6: GO TO Step-3
Step 7: Stop
Exercise 04. Draw an algorithm & flowchart to find sum of series 1+2+3+…..+N
Algorithm: Flowchart:
Step 1: Start
Step 2: Input Value of N
Step 3: I = 1, SUM=0
Step 4: IF (I >N) THEN
GO TO Step-8
ENDIF
Step 5: SUM = SUM + I
Step 6: I = I + 1
Step 7: Go to step-4
Step 8: Display value of SUM
Step 9: Stop
----------------------------------------0--------------------------------------------