Algorithm_Notes
Algorithm_Notes
Introduction to Programming
Algorithm:
Ex. 1. Write the algorithm to find the sum and product of two given
numbers.
Algorithm: To find the sum and product of two given numbers: Step 1:
Read A , B
Step 2: Let Sum= A+B Step 3:
Let Product=A*B Step 4: Print
Sum, Product Step 5: Stop.
1. [Initialize the
variables] large = 0
count = 0
2. [Increment the count]
count = count+1
3. [Enter
number] read x
4. [Perform the operations]
if (t>x) then
goto step 6
end if
5. large = x
6. if (count 10) then
goto step 2
end if
7. [Print the result]
Print large
8. [Finished]
Stop
Algorithm:
1. [Initialize the
variables] X=1
S=0
2. [Perform the operations]
Y = X*X
S=S + Y
X=X+1
3. [Check the condition]
If (X 100) Then
Goto step 2
End if
4. [Print the output]
Print S
5. [Finished]
Stop
Flow-chart?
Flowchart symbols:
Start/Stop
Ellipse
Expression
Rectangle
I/O Statements
Parallelogram
Decision
Rhombus
Ex. Draw the flowchart to find the sum and product of two given
numbers.
Start
Read A,B
Sum=A+B
Product=A*B
Stop
Ex: Write the algorithm and draw the flowchart to find the sum and
product of two given numbers.
Solution:
Algorithm: To find the sum and product of two given numbers:
Step 1: Read A , B
Step 2: Let Sum= A+B
Step 3: Let Product=A*B
Step 4: Print Sum, Product
Step 5: Stop.
Start
Read A,B
Sum=A+B
Product=A*B
Stop
Solution:
Algorithm: To compute the arithmetic mean of n numbers.
1. [Enter N]
Read N
2. [Enter numbers]
For I=1 to N
Read A[I]
End For
3. [Initialize the variables]
Sum=0
4. [Perform the operations]
For I=1 to N
Sum=Sum+A[I]
End For
AM=Sum/N
5. [Print the Output]
Print Sum, AM
6. [Finished]
Stop
Disadvantages of Algorithm:
It is time consuming and cumbersome as an algorithm is developed first
which is converted into a flowchart and then into a computer program.
Solution:
An algorithm is a step of instructions to be followed to solve a
problem in computers.
According to D.E. Knuth, a pioneer in the computer science
discipline, an algorithm has five important features.
Start
Pick up a postcard
Stop
Start
Read N
Put S = 0
Initialization and I = 0
Read A
Looping
Increase I by 1
Modification (I=I+1)
is
Testing I=N
No
Write S
Stop
Start
Read A, B, C
No Yes
Is
A>B
Yes No No Yes
Is Is
B>C A>C
Stop
Solution:
Start
Read n
Sum=0
For i=1 to n
Read x[i]
Sum=Sum + x[i]
End for
Print Sum
Stop
Q.2. Draw a flowchart to find the average, maximum and minimum values of n given
numbers.
Solution:
Start
Read n
For i=1 to n
Read x[i]
End for
For i=1 to n
No Is
Yes
Max<x[i]
?
Max=x[i]
Is
Min>x[i] Yes
? Min=x[i]
No Sum=Sum+x[i]
End for
Average=Sum/n
Stop
Q.3. Draw a flowchart to read and find the maximum of n given numbers.
Solution:
Start
Read n
For i=1 to n
Read x[i]
End for
Max=x[1]
For i=1 to n
If
No max<x[i] ? Yes
Max=x[i]
End for
Print Max
Stop
Solution:
Start
Read n
For i=1 to n
Read x[i]
End for
Max=x[1]
For i=1 to n
If
max<x[i] ?
No Yes
Max=x[i]
End for
Print Max
Stop
Solution:
Start
Read n
For i=1 to n
Read x[i]
End for
Max=x[1]
For i=1 to n
No If max<x[i]
? Yes
Max=x[i]
End for
Print Max
Stop
Q.8: Draw a flowchart to determine and print the average of 30 given numbers.
Solution:
Flowchart: To determine and print the average of 30 given numbers.
Start
Read Num
Sum = Sum+Num
Count = Count+1
Is
Count 30
?
Yes
No
Average =Sum/(Count-1)
Print Average
Stop
Q.9: Draw a flowchart to read N numbers and then to compute and print the
arithmetic mean of the numbers.
Solution: The flowchart to compute the arithmetic mean of n numbers is shown
below.
Start
Read n
For I=1 to N
Read A[I]
End For
Sum = 0
For I=1 to N
Sum=Sum+A[I]
End For
AM=Sum/N
Print AM
Stop
Q.13: Draw a flowchart to read a value of N and to compute and print the value of N!
Solution:
Flowchart: To read a value of N and to compute and print the value of N!
Start
Read N
Is
N <0 ?
Yes No
Fact = 1
Stop
For L = 2 to N
Fact=Fact*L
End For
Print Fact
Stop