Programming for Problem Solving (UNIT 1)
Approaches to Problem Solving
Computer Program: Computer program is a set of Sequential instructions to perform task.
Algorithm: Algorithm is a finite sequence of explicit and unambiguous steps required to
solve the given problem. When the required input values are provided, it produces an output
and then terminates. The steps may repeat or require decisions as per the requirement. The
same problem can be solved with different [Link], for solving the same problem,
different algorithms may be accomplished. In these algorithms, the steps, time, space and
effort may vary more or less. User writes algorithm in his/her own language. So it cannot be
executed on computer.
Properties of algorithm:
1. The steps used in algorithm must be unambiguous and precisely defined.
2. The uncertainty about the instruction to be executed next should be avoided.
3. The steps used in algorithm should be finite and the algorithm should be terminated, that is,
it cannot be open-ended.
4. The execution of the algorithm should conclude after a finite number of steps.
5. The algorithm must be general enough to deal with any situation.
Example: Write an algorithm to calculate the addition of two numbers.
Algorithm:
Step1: START
Step2: INPUT A and B
Step3: SUM=A+B
Step4: PRINT SUM
Step5: STOP
1Q. Write an algorithm to find Sum and average of three numbers.
Algorithm:
Step1: START
Step2: INPUT A, B, C
Step3: SUM=A+B+C
Step4: AVG = SUM/3
Step5: PRINT SUM, AVG
By: Mr. Ankur Srivastava (Assistant Professor), AITM,Varanasi 1
Programming for Problem Solving (UNIT 1)
Step6: STOP
[Link] an algorithm to swap the contents of two variables.
Algorithm
Step1: START
Step2: INPUT A, B
Step3: C = A
Step4: A = B
Step5: B =C
Step6: PRINT A, B
Step7: STOP
3Q. Write an algorithm to find area and circumference of circle.
NOTE: - Area of circle= π R2
Circumference of circle=2πR
Algorithm:
Step1: START
Step2: INPUT R
Step3: AREA= π*R*R
Step4: Circumference = 2* π*R
Step5: PRINT AREA, Circumference
Step7: STOP
4Q. Write an algorithm to convert Fahrenheit temperature to Centigrade.
Formula: C = 5/9(F - 32)
Algorithm:
Step1: START
Step2: INPUT F
Step3: C= [5*(F - 32)]/9
Step4: PRINT C
By: Mr. Ankur Srivastava (Assistant Professor), AITM,Varanasi 2
Programming for Problem Solving (UNIT 1)
Step7: STOP
5Q. Write an algorithm to convert Centigrade temperature to Fahrenheit.
Algorithm:
Step1: START
Step2: INPUT C
Step3: F= [(9*C)/5] +32
Step4: PRINT F
Step5: STOP
Flowchart: The flow chart is a graphical representation of sequence of instructions in a
program. The flow chart uses boxes of different shapes to represent different types of
instructions. These boxes are connected by arrows lines to indicate the flow of data. A flow
chart is assumed to be the plan to be followed during the program writing. It saves the
programmer to make errors in programming code. The flow chart is helpful for testing the
program before computing it.
Symbols of Flow Chart: The following symbols are required to indicate various types of
instructions in flowcharts. These symbols have been standardizing by American National
Standard Institute (ANSI).
SHAPE DENOTES
Terminal Box
Input/Output Box
Processing Box
Decision Box
Condition
Flow Lines
Connector (Transfer) Box
1 2
By: Mr. Ankur Srivastava (Assistant Professor), AITM,Varanasi 3
Programming for Problem Solving (UNIT 1)
Iteration or Looping Box
Print Box / Output Box
1. Terminal Box: This symbol is used in a flow chart to represent the starting and stopping of
program. This is always first and last symbol used in the flow chart.
Start End
2. Processing Box: A processing symbol is used in a flow chart to represent arithmetic and data
movement instructions. Thus, all arithmetic processes such as addition, subtraction, multiplication and
division are show by a processing box symbol. The logical process of moving data (assignment
operation) from one location of the main memory to another is also represented by this symbol. When
more then one arithmetic and data movement instructions are to be shown consecutively one after the
other, they can be placed in the same processing box and assumed to be executed in the order of their
presence in the box.
A=B+C
3. Decision Box: The decision box is a diamond shaped box to indicate a point at which a
decision has to be made. Then the branching to two or more alternative points is possible.
Decision symbol can be used in following different ways-
If
I = 10
?
4. Connector: If a flow chart becomes very long, the flow lines start criss-crossing at many
places. It may cause confusion and reduce the clarity of the flow chart. There may be some
instances when a flowchart becomes impossible to be created on single page. Whenever a
flowchart becomes too complex that the number and direction of flow lines become
confusing or it spreads over more than one page, It is useful to make the connector symbol as
a substitute for flow lines. These symbols represent an Entry from or an Exit to another part
of the flow chart.
A A
Exit A Entry from A
By: Mr. Ankur Srivastava (Assistant Professor), AITM,Varanasi 4
Programming for Problem Solving (UNIT 1)
5. Flow Lines: Flow lines with arrow heads are used to indicate the flow of operations, i.e.,
the exact sequence in which the instructions are to be executed. The normal flow of flow
chart is from TOP TO BOTTOM and LEFT TO RIGHT. Arrow heads are required only
when the normal top to bottom flow is not to be followed.
6. Iteration or Looping Box: This box is use for iteration or looping statement it is use for
repetition for given instruction of time.
For(i=1; i<=5; i=i+1)
7. Print Box: Print box is use for print the result output.
Print sum
Advantages of Flow Charts
1. Conveys Better Meaning:
2. Analyses the problem effectively:
3. Effectively Joining the parts of a system:
4. Efficient Coding:
5. Systematic Debugging:
6. Systematic Testing:
Disadvantages of flow charts
1. Takes more time to draw:
2. Difficult to make changes:
3. Non Standardization:
By: Mr. Ankur Srivastava (Assistant Professor), AITM,Varanasi 5
Programming for Problem Solving (UNIT 1)
Example
[Link] a flowchart to find sum and average of 3 numbers.
Flowchart:
START
INPUT A,B,C
SUM=A+B+C
AVG= SUM/3
PRINT
SUM,AVG
STOP
By: Mr. Ankur Srivastava (Assistant Professor), AITM,Varanasi 6
Programming for Problem Solving (UNIT 1)
[Link] a flowchart to swap the contents of two variables.
START
READ A, B
C A
A B
B C
PRINT A,B
STOP
By: Mr. Ankur Srivastava (Assistant Professor), AITM,Varanasi 7