Ge1105 Problem Solving and Python Programming Unit - 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

UNIT 1
ALGORITHMIC PROBLEM SOLVING
Algorithms, building blocks of algorithms (statements, state, control flow, functions), notation
(pseudo code, flow chart, programming language), algorithmic problem solving, Basic
algorithms, flowcharts, and pseudocode for sequential, decision processing, and iterative
processing strategies, Illustrative problems: find minimum in a list, insert a card in a list of sorted
cards, guess an integer number in a range, Towers of Hanoi.

PROBLEM-SOLVING
Problem-solving is the systematic approach to defining the problem and creating several solutions.
The problem-solving process starts with the problem specifications and ends with a Correct program.

PROBLEM-SOLVING TECHNIQUES
The problem-solving technique is a set of techniques that helps in providing logic for solving a
problem.
Problem-Solving Techniques:
Problem-solving can be expressed in the form of
1. Algorithms.
2. Flowcharts.
3. Pseudo codes.
4. programs

ALGORITHM
It is defined as a sequence of instructions that describe a method for solving a problem. In other
words, it is a step-by-step procedure for solving a problem.
Properties of Algorithms
• Should be written in simple English
• Each and every instruction should be precise and unambiguous.
• Instructions in an algorithm should not be repeated infinitely.
• An algorithm should conclude after a finite number of steps.
• Should have an endpoint
• Derived results should be obtained only after the algorithm terminates.

P a g e 1 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

Qualities of a good algorithm


The following are the primary factors that are often used to judge the quality of the algorithms.
• Time – To execute a program, the computer system takes some amount of time. The lesser
time required, the better the algorithm.
• Memory – To execute a program, the computer system takes some amount of memory space.
The lesser memory required, the better the algorithm.
• Accuracy – Multiple algorithms may provide suitable or correct solutions to a given problem,
some of these may provide more accurate results than others, and such algorithms may be
suitable.
Example
Write an algorithm to print „Good Morning”
Step 1: Start
Step 2: Print “Good Morning”
Step 3: Stop
BUILDING BLOCKS OF ALGORITHMS (statements, state, control flow, functions)
Algorithms can be constructed from basic building blocks namely, sequence, selection, and iteration.
1. Statements:
A statement is a single action in a computer.
In a computer, statements might include some of the following actions
➢ input data information is given to the program
➢ process data-perform operation on a given input
➢ output data-processed result

2. State:
Transitioning from one process to another process under the specified condition within a time is
called a state.
3. Control flow:
The process of executing the individual statements in a given order is called control flow.
The control can be executed in three ways
a) sequence
b) selection
c) iteration
Sequence:
All the instructions are executed one after another is called sequence execution.

P a g e 2 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

Example:
Add two numbers:
Step 1: Start
Step 2: get a,b
Step 3: calculate c=a+b
Step 4: Display c
Step 5: Stop

a) Selection:
A selection statement causes the program control to be transferred to a specific part of the program
based on the condition.
If the conditional test is true, one part of the program will be executed, otherwise, it will execute the
other part of the program.

Example
Write an algorithm to check whether he is
eligible to vote?
Step 1: Start
Step 2: Get the age
Step 3: if age >= 18 print “Eligible to vote”
Step 4: else print “Not eligible to vote”
Step 6: Stop

b) Iteration:
In some programs, a certain set of statements are executed again and again based upon the
conditional tests. i.e. executed more than one time. This type of execution is called looping or
iteration.

P a g e 3 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

Example
Write an algorithm to print all natural numbers up to n
Step 1: Start
Step 2: get n value.
Step 3: initialize i=1
Step 4: if (i<=n) go to step 5 else go to step 7
Step 5: Print i value and increment i value by 1
Step 6: go to step 4
Step 7: Stop

4. Functions:
A function is a subprogram that consists of a block of code(set of instructions) that performs a
particular task.
For complex problems, the problem is been divided into smaller and simpler tasks during algorithm
design.
Benefits of Using Functions
• Reduction in line of code
• code reuse
• Better readability
• Information hiding
• Easy to debug and test
• Improved maintainability

Example
Algorithm for the addition of two numbers using function
Main function() sub function add()
Step 1: Start Step 1: Function start
Step 2: Call the function add() Step 2: Get a, b Values
Step 3: Stop Step 3: add c=a+b
Step 4: Print c
Step 5: Return

P a g e 4 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

Main Program Sub Program

NOTATIONS
FLOW CHART
A flow chart is defined as a graphical representation of the logic for problem-solving.
The purpose of the flowchart is to make the logic of the program clear in a visual representation.

P a g e 5 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

Rules for drawing a flowchart


• The flowchart should be clear, neat, and easy to follow.
• The flowchart must have a logical start and finish.
• Only one flow line should come out from a process symbol.

4. Only one flow line should enter a decision symbol. However, two or three flow lines may leave
the decision symbol.

5. Only one flow line is used with a terminal symbol.

6. Within standard symbols, write briefly and precisely.


7. Intersection of flow lines should be avoided.

Advantages of flowchart:
i. Communication: - Flowcharts are a better way of communicating the logic of a system to all
concerned.
ii. Effective analysis: - With the help of a flowchart, the problem can be analyzed more
effectively.
iii. Proper documentation: - Program flowcharts serve as good program documentation, which
is needed for various purposes.
iv. Efficient Coding: - The flowcharts act as a guide or blueprint during the systems analysis
and program development phase.
v. Proper Debugging: - The flowchart helps in debugging process.
vi. Efficient Program Maintenance: - The maintenance of the operating program becomes easy
with the help of a flowchart. It helps the programmer to put efforts more efficiently into that
part.

P a g e 6 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

Disadvantages of flow chart:


i. Complex logic: - Sometimes, the program logic is quite complicated. In that case, the
flowchart becomes complex and clumsy.
ii. Alterations and Modifications: - If alterations are required the flowchart may require re-
drawing completely.
iii. Reproduction: - As the flowchart symbols cannot be typed, reproduction of the flowchart
becomes a problem.
iv. Cost: For large applications the time and cost of flowchart drawing become costly.

PSEUDO CODE:
i. Pseudocode consists of short, readable, and formally styled English languages used to
explaining an algorithm.
ii. It does not include details like variable declaration, and subroutines.
iii. It is easier to understand for the programmer or nonprogrammer to understand the general
working of the program because it is not based on any programming language.
iv. It gives us a sketch of the program before actual coding.
v. It is not a machine-readable
vi. Pseudocode can’t be compiled and executed.
vii. There is no standard syntax for pseudo code.
Guidelines for writing pseudo code:
• Write one statement per line
• Capitalize the initial keyword
• Indent to the hierarchy
• End multiline structure
• Keep statements language independent
Common keywords used in pseudocode
The following gives common keywords used in pseudocodes.
1. //: This keyword is used to represent a comment.
2. BEGIN, END: Begin is the first statement and end is the last statement.
3. INPUT, GET, READ: The keyword is used to inputting data.
4. COMPUTE, CALCULATE: used for calculation of the result of the given expression.
5. ADD, SUBTRACT, INITIALIZE used for addition, subtraction and initialization.
6. OUTPUT, PRINT, DISPLAY: It is used to display the output of the program.
7. IF, ELSE, ENDIF: used to make a decision.
8. WHILE, ENDWHILE: used for iterative statements.
9. FOR, ENDFOR: Another iterative incremented/decremented tested automatically.

P a g e 7 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

The syntax for if else: Example: Greatest of two numbers


IF (condition)THEN BEGIN
statement READ a,b
... IF (a>b) THEN
ELSE DISPLAY a is greater
statement ELSE
... DISPLAY b is a greater
ENDIF END IF
END

The syntax for For: Example: Print n natural numbers


FOR( start-value to end-value) DO BEGIN
statement GET n
... INITIALIZE i=1
ENDFOR FOR (i<=n) DO
PRINT i
i=i+1
ENDFOR
END

The syntax for While: Example: Print n natural numbers


WHILE (condition) DO BEGIN
statement GET n
... INITIALIZE i=1
ENDWHILE WHILE(i<=n) DO
PRINT i
i=i+1
ENDWHILE
END

Advantages:
• Pseudo is independent of any language; it can be used by most programmers.
• It is easy to translate pseudo code into a programming language.
• It can be easily modified as compared to a flowchart.
• Converting a pseudo code to a programming language is very easy compared with converting
a flowchart to a programming language.

Disadvantages:
• It does not provide a visual representation of the program’s logic.
• There are no accepted standards for writing pseudo codes.
• It cannot be compiled or executed.
• For a beginner, It is more difficult to follow the logic or write pseudo code as compared to a
flowchart.
P a g e 8 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

Example:
Addition of two numbers:
BEGIN
GET a,b
ADD c=a+b
PRINT c
END

PROGRAMMING LANGUAGE
A programming language is a set of symbols and rules for instructing a computer to perform specific
tasks. The programmers have to follow all the specified rules before writing a program using a
programming language. The user has to communicate with the computer using language that it can
understand.
Types of programming language
i. Machine language
ii. Assembly language
iii. High-level language

i. Machine language:
The computer can understand only machine language which uses 0’s and 1’s. In machine language,
the different instructions are formed by taking different combinations of 0’s and 1’s.
Advantages:
Translation free:
Machine language is the only language that the computer understands. For executing any program
written in any programming language, the conversion to machine language is necessary. The program
written in machine language can be executed directly on a computer. In this case, any conversion
process is not required.
P a g e 9 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

High speed
The machine language program is translation free. Since the conversion time is saved, the execution
of the machine language program is extremely fast.
Disadvantage:
• It is hard to find errors in a program written in machine language.
• Writing programs in machine language is a time-consuming process.

Machine dependent: According to the architecture used, the computer differs from each other. So
machine language differs from computer to computer. So a program developed for a particular type of
computer may not run on other types of computer.
ii. Assembly language: To overcome the issues in programming language and make the
programming process easier, an assembly language is developed which is logically equivalent to
machine language but is easier for people to read, write and understand.
Assembly language is a symbolic representation of machine language. Assembly languages are
symbolic programming language that uses symbolic notation to represent machine language
instructions. They are called low-level languages because they are so closely related to machines.
Assembler
Assembler is the program that translates assembly language instruction into a machine language.
• Easy to understand and use.
• It is easy to locate and correct errors.
Disadvantage
Machine dependent
The assembly language program which can be executed on the machine depends on the architecture
of that computer.
Hard to learn
It is machine-dependent, so the programmer should have the hardware knowledge to create
applications using assembly language.
Less efficient
• Execution time of an assembly language program is more than machine language program.
• Because an assembler is needed to convert from assembly language to machine language.
iii. High-level language
The high-level language contains English words and symbols. The specified rules are to be followed
while writing a program in a high-level language. The interpreter or compilers are used for converting
these programs into machine-readable form.

P a g e 10 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

Translating high-level language to machine language


The programs that translate high-level language into machine language are called interpreters or
compilers.
Compiler:
A compiler is a program that translates the source code written in a high-level language into object
code which is in the machine language program. A compiler reads the whole program written in a
high-level language and translates it to machine language. If any error is found it displays an error
message on the screen.
Interpreter:
An interpreter translates the high-level language program in line by line manner. The interpreter
translates a high-level language statement in a source program to a machine code and executes it
immediately before translating the next statement. When an error is found the execution of the program
is halted and an error message is displayed on the screen.
Advantages
• Readability High-level language is closer to natural language so they are easier to learn and
understand
• Machine-independent High-level language programs have the advantage of being portable
between machines.
• Easy debugging Easy to find and correct errors in a high-level language
Disadvantages
Less efficient The translation process increases the execution time of the program. Programs in the
high-level language require more memory and take more execution time to execute.
They are divided into the following categories:
1. Interpreted programming languages
2. Functional programming languages
3. Compiled programming languages
4. Procedural programming languages
5. Scripting programming language
6. Markup programming language
7. Concurrent programming language
8. Object-oriented programming language

P a g e 11 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

ALGORITHMIC PROBLEM SOLVING


Algorithmic problem solving is solving a problem that requires the formulation of an algorithm for
the solution.

Understanding the Problem


• It is the process of finding the input of the problem that the algorithm solves.
• It is very important to specify exactly the set of inputs the algorithm needs to handle.
• A correct algorithm does not work most of the time, but one that works correctly
for all legitimate inputs.
Ascertaining the Capabilities of the Computational Device
• If the instructions are executed one after another, it is called a sequential algorithm.
• If the instructions are executed concurrently, it is called a parallel algorithm.

P a g e 12 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

Choosing between Exact and Approximate Problem Solving


• The next principal decision is to choose between solving the problem exactly or solving it
approximately.
• Based on this, the algorithms are classified as exact algorithms and approximation algorithms.

Deciding a data structure:


• Data structure plays a vital role in designing and analyzing algorithms.
• Some of the algorithm design techniques also depend on the structuring data specifying a
problem’s instance
• Algorithm+ Data structure=programs.

Algorithm Design Techniques


• An algorithm design technique (or “strategy” or “paradigm”) is a general approach to
solving problems algorithmically that applies to a variety of problems from different areas of
computing.
• Learning these techniques is of utmost importance for the following reasons.
• First, they provide guidance for designing algorithms for new problems,
• Second, algorithms are the cornerstone of computer science
Methods of Specifying an Algorithm
• Pseudocode is a mixture of a natural language and programming language-like constructs.
Pseudocode is usually more precise than natural language, and its usage often yields more
succinct algorithm descriptions.
• In the earlier days of computing, the dominant vehicle for specifying algorithms was
a flowchart, a method of expressing an algorithm by a collection of connected geometric
shapes containing descriptions of the algorithm’s steps.
• Programming language can be fed into an electronic computer directly. Instead, it needs to
be converted into a computer program written in a particular computer language. We can look
at such a program as yet another way of specifying the algorithm, although it is preferable to
consider it as the algorithm’s implementation.

Proving an Algorithm’s Correctness


• Once an algorithm has been specified, you have to prove its correctness. That is, you have to
prove that the algorithm yields a required result for every legitimate input in a finite amount
of time.
• A common technique for proving correctness is to use mathematical induction because an
algorithm’s iterations provide a natural sequence of steps needed for such proofs.
• It might be worth mentioning that although tracing the algorithm’s performance for a few
specific inputs can be a very worthwhile activity, it cannot prove the algorithm’s correctness
conclusively. But to show that an algorithm is incorrect, you need just one instance of its
input for which the algorithm fails.
P a g e 13 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

Analyzing an Algorithm
1. Efficiency.
• Time efficiency, indicating how fast the algorithm runs,
• Space efficiency indicates how much extra memory it uses.
2. simplicity.
• An algorithm should be precisely defined and investigated with mathematical expressions.
• Simpler algorithms are easier to understand and easier to program.
• Simple algorithms usually contain fewer bugs.
Coding an Algorithm
Most algorithms are destined to be ultimately implemented as computer programs. Programming an
algorithm presents both a peril and an opportunity.
A working program provides an additional opportunity in allowing an empirical analysis of the
underlying algorithm. Such an analysis is based on timing the program on several inputs and then
analyzing the results obtained.

SIMPLE STRATEGIES FOR DEVELOPING ALGORITHMS:

1. Iterations
2. Recursions
1. Iterations:
A sequence of statements is executed until a specified condition is true is called iterations.
[1] for loop
[2] While loop

Syntax for For: Example: Print n natural numbers


FOR( start-value to end-value) DO BEGIN
GET n
Statement INITIALIZE i=1
... FOR (i<=n) DO
ENDFOR PRINT i
i=i+1
ENDFOR
END
Syntax for While: Example: Print n natural numbers
WHILE (condition) DO BEGIN
GET n
Statement INITIALIZE i=1
... WHILE(i<=n) DO
ENDWHILE PRINT i
i=i+1
ENDWHILE
END

P a g e 14 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

Recursions:
A function that calls itself is known as recursion.
Recursion is a process by which a function calls itself repeatedly until some specified condition has
been satisfied.
Algorithm for factorial of n numbers using recursion:
Main function: Sub function factorial(n):
Step1: Start Step1: if(n==1) then fact=1 return fact
Step2: Get n Step2: else fact=n*factorial(n-1) and return fact
Step3: call factorial(n)
Step4: print fact
Step5: Stop

P a g e 15 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

Pseudo code for factorial using recursion:

Main function: Sub function factorial(n):


BEGIN IF(n==1) THEN
GET n fact=1
CALL factorial(n) RETURN fact
PRINT fact ELSE
BIN RETURN fact=n*factorial(n-1)

P a g e 16 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

MORE EXAMPLES
1. Write an algorithm to find area of a rectangle
Step 1: Start BEGIN
Step 2: get l,b values READ l,b
Step 3: Calculate A=l*b CALCULATE A=l*b
Step 4: Display A DISPLAY A
Step 5: Stop END

2. Write an algorithm for Calculating area and circumference of circle


BEGIN
Step 1: Start READ r
Step 2: get r value CALCULATE A and C
Step 3: Calculate A=3.14*r*r A=3.14*r*r
Step 4: Calculate C=2.3.14*r C=2*3.14*r
Step 5: Display A,C DISPLAY A
Step 6: Stop END

3. Write an algorithm for Calculating simple interest


Step 1: Start BEGIN
Step 2: get P, n, r value READ P, n, r
Step3:Calculate CALCULATE S
SI=(p*n*r)/100 SI=(p*n*r)/100
Step 4: Display S DISPLAY SI
Step 5: Stop END

P a g e 17 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

4. Write an algorithm for Calculating engineering cutoff


Step 1: Start BEGIN
Step2: get P,C,M value READ P,C,M
Step3:calculate CALCULATE
Cutoff= (P/4+C/4+M/2) Cutoff= (P/4+C/4+M/2)
Step 4: Display Cutoff DISPLAY Cutoff
Step 5: Stop END

5. To check greatest of two numbers

Step 1: Start BEGIN


Step 2: get a,b value READ a,b
Step 3: check if(a>b) IF (a>b) THEN
print a is greater DISPLAY a is greater
Step 4: else b is greater ELSE
Step 5: Stop DISPLAY b is greater
END IF
END

6. To check leap year or not


Step 1: Start BEGIN
Step 2: get y READ y
Step 3: if(y%4==0) print IF (y%4==0) THEN
leap year DISPLAY leap year
Step 4: else print not leap ELSE
year DISPLAY not leap year
Step 5: Stop END IF
END

P a g e 18 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

7. To check positive or negative number


Step 1: Start BEGIN
Step 2: get num READ num
Step 3: check if(num>0) IF (num>0) THEN
print a is positive DISPLAY num is
Step 4: else num is positive
negative ELSE
Step 5: Stop DISPLAY num is
negative
END IF
END

8. To check odd or even number


Step 1: Start BEGIN
Step 2: get num READ num
Step 3: check if(num%2==0) IF (num%2==0) THEN
print num is even DISPLAY num is even
Step 4: else num is odd ELSE
Step 5: Stop DISPLAY num is odd
END IF
END

9. To check greatest of three numbers


Step1: Start BEGIN
Step2: Get A, B, C READ a, b, c
Step3: if(A>B) goto IF (a>b) THEN
Step4 else goto step5 IF(a>c) THEN
Step4: If(A>C) DISPLAY a is greater
print A ELSE
else print C DISPLAY c is greater
Step5: If(B>C) END IF
print B ELSE
else print C IF(b>c) THEN
Step6: Stop DISPLAY b is greater
ELSE
DISPLAY c is greater
END IF
END IF
END

P a g e 19 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

10. Write an algorithm to check whether given number is +ve, -ve or zero.

Step 1: Start BEGIN


Step 2: Get n value. GET n
Step 3: if (n ==0) IF(n==0) THEN
print “Given number is DISPLAY “ n is
Zero” Else goto step4 zero”
Step 4: if (n > 0) then ELSE
Print “Given number IF(n>0) THEN
is +ve” DISPLAY “n is
Step 5: else Print positive”
“Given number is -ve” ELSE
Step 6: Stop DISPLAY “n is
positive”
END IF
END IF
END

11. Write an algorithm to print all natural numbers up to n

Step 1: Start BEGIN


Step 2: get n value. GET n
Step 3: initialize i=1 INITIALIZE i=1
Step 4: if (i<=n) go to WHILE(i<=n) DO
step 5 else go to step 8 PRINT i
Step 5: Print i value i=i+1
step 6 : increment i value ENDWHILE
by 1 END
Step 7: go to step 4
Step 8: Stop

P a g e 20 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

12. Write an algorithm to print n odd numbers

Step 1: start BEGIN


step 2: get n value GET n
step 3: set initial value i=1 INITIALIZE i=1
step 4: check if(i<=n) goto WHILE(i<=n) DO
step 5 else goto step 8 PRINT i
step 5: print i value i=i+2
step 6: increment i value by ENDWHILE
2 END
step 7: goto step 4
step 8: stop

13. Write an algorithm to print n even numbers

Step 1: start BEGIN


step 2: get n value GET n
step 3: set initial value i=2 INITIALIZE i=2
step 4: check if(i<=n) goto WHILE(i<=n) DO
step 5 else goto step8 PRINT i
step 5: print i value i=i+2
step 6: increment i value by ENDWHILE
2 END
step 7: goto step 4
step 8: stop

P a g e 21 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

14. Write an algorithm to print squares of a number

Step 1: start BEGIN


step 2: get n value GET n
step 3: set initial value i=1 INITIALIZE i=1
step 4: check i value WHILE(i<=n) DO
if(i<=n) goto step 5 else PRINT i*i
goto step8 i=i+2
step 5: print i*i value ENDWHILE
step 6: increment i value by END
1
step 7: goto step 4
step 8: stop

15. Write an algorithm to print to print cubes of a number


Step 1: start BEGIN
step 2: get n value GET n
step 3: set initial value i=1 INITIALIZE i=1
step 4: check i value WHILE(i<=n) DO
if(i<=n) goto step 5 else PRINT i*i*i
goto step8 i=i+2
step 5: print i*i *i value ENDWHILE
step 6: increment i value END
by 1
step 7: goto step 4
step 8: stop

P a g e 22 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

16. Write an algorithm to find sum of a given number

Step 1: start BEGIN


step 2: get n value GET n
step 3: set initial value INITIALIZE i=1,sum=0
i=1, sum=0 WHILE(i<=n) DO
Step 4: check i value sum=sum+i
if(i<=n) goto step 5 else i=i+1
goto step8 ENDWHILE
step 5: calculate PRINT sum
sum=sum+i END
step 6: increment i value
by 1
step 7: goto step 4
step 8: print sum value
step 9: stop

17. Write an algorithm to find factorial of a given number

Step 1: start BEGIN


step 2: get n value GET n
step 3: set initial value i=1, INITIALIZE
fact=1 i=1,fact=1
Step 4: check i value if(i<=n) WHILE(i<=n) DO
goto step 5 else goto step8 fact=fact*i
step 5: calculate fact=fact*i i=i+1
step 6: increment i value by 1 ENDWHILE
step 7: goto step 4 PRINT fact
step 8: print fact value END
step 9: stop

P a g e 23 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

ILLUSTRATIVE PROBLEMS
1. Guess an integer in a range

Algorithm: Pseudocode:
Step1: Start BEGIN
Step 2: Declare hidden, guess COMPUTE hidden=random value in range
Step 3: Compute hidden= Choose a random READ guess
value in a range
IF guess=hidden, then
Step 4: Read guess
PRINT Guess is hit
Step 5: If guess=hidden, then
ELSE
Print Guess is hit
PRINT Guess not hit
Else
PRINT hidden
Print Guess not hit
END IF-ELSE
Print hidden
END
Step 6: Stop

Flowchart:

P a g e 24 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

2. Find the minimum in a list


Algorithm: Pseudocode:
Step 1: Start BEGIN READ n
Step 2: Read n FOR i=0 to n, then
Step 3:Initialize i=0 READ a[i]
Step 4: If i<n, then goto INCREMENT i
step 4.1, 4.2 else goto END FOR
step 5 Step4.1: Read a[i] COMPUTE min=a[0]
Step 4.2: i=i+1 goto step 4 FOR i=1 to n, then
Step 5: Compute min=a[0] IF a[i]<min, then
Step 6: Initialize i=1 CALCULATE min=a[i]
Step 7: If i<n, then go to step 8 else goto step 10 INCREMENT i
Step 8: If a[i]<min, then goto ELSE
step 8.1,8.2 else goto 8.2 INCREMENT i
Step 8.1: min=a[i] END IF-ELSE
Step 8.2: i=i+1 goto 7 END FOR
Step 9: Print min PRINT min
Step 10: Stop END

P a g e 25 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

Flowchart:

P a g e 26 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

3. Insert a card in a list of sorted cards

Algorithm: Pseudocode:
Step 1: Start BEGIN
Step 2: Read n READ n
Step 3:Initialize i=0 FOR i=0 to n, then
Step 4: If i<n, then goto step 4.1, 4.2 else goto READ a[i]
step 5
INCREMENT i
Step4.1: Read a[i]
END FOR
Step 4.2: i=i+1 goto step 4
READ item
Step 5: Read item
FOR i=n-1 to 0 and item<a[i], then
Step 6: Calculate i=n-1
CALCULATE a[i+1]=a[i]
Step 7: If i>=0 and item<a[i], then go to step
DECREMENT i
7.1, 7.2 else goto step 8
END FOR
Step 7.1: a[i+1]=a[i]
COMPUTE a[i+1]=a[i]
Step 7.2: i=i-1 goto step 7
COMPUTE n=n+1
Step 8: Compute a[i+1]=item
FOR i=0 to n, then
Step 9: Compute n=n+1
PRINT a[i]
Step 10: If i<n, then goto step 10.1, 10.2 lse
goto st 11 INCREMENT i
Step10.1: Print a[i] END FOR
Step10.2: i=i+1 goto step 10 END
Step 11: Stop

P a g e 27 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

Flowchart:

P a g e 28 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

4. Tower of Hanoi
Algorithm: Pseudcode:
Step 1: Start BEGIN
Step 2: Read n READ n
Step 3: Calculate move=pow(2,n)-1 CALCULATE move=pow(2,n)-1
Step 4: Function call T(n,Beg,Aux,End) FUNCTION T(n,Beg,Aux,End) Recursiv ly
recursively until n=0 until n=0
Step 4.1: If n=0, then goto PROCEDURE
step 5 else goto step IF n=0 then,
4.2 Step No disk to move
4.2: T(n-1,Beg,End,Aux) T(1,Beg,Aux,End) , Else
Move disk from source to desti ation T(n-
T(n-1,Beg,End,Aux)
1,Aux,Beg,End)
T(1,Beg,Aux,End), move isk from source to
Step 5: Stop
destination
T(n-1,Aux,Beg,En )
END PROCEDURE
END

Flowchart:

P a g e 29 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

Procedure to solve Tower of Hanoi


The goal of the puzzle is to move all the disks from leftmost peg to rightmost peg.
1. Move only one disk at a time.
2. A larger disk may not be p1aced on top of a smaller disk. For example, consider n=3 disks

P a g e 30 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

P a g e 31 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

P a g e 32 | 33
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 1

P a g e 33 | 33
St. Joseph’s College of Engineering

You might also like