0% found this document useful (0 votes)
18 views28 pages

PSPP Unit 1

The document discusses the fundamentals of computing, focusing on computational thinking and problem-solving techniques, including the identification of computational problems and the development of algorithms. It outlines the steps involved in solving computational problems, types of computational problems, and the properties and qualities of algorithms. Additionally, it covers notations such as flowcharts and pseudo code, as well as the basics of programming languages.
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)
18 views28 pages

PSPP Unit 1

The document discusses the fundamentals of computing, focusing on computational thinking and problem-solving techniques, including the identification of computational problems and the development of algorithms. It outlines the steps involved in solving computational problems, types of computational problems, and the properties and qualities of algorithms. Additionally, it covers notations such as flowcharts and pseudo code, as well as the basics of programming languages.
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/ 28

UNIT I COMPUTATIONAL THINKING AND PROBLEM SOLVING

Fundamentals of Computing– Identification of Computational Problems -Algorithms,


building blocks of algorithms (statements, state, control flow, functions), notation (pseudo
code, flow chart, programming language), algorithmic problem solving, simple strategies for
developing algorithms (iteration, recursion). 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.

1.1 FUNDAMENTALS OF COMPUTING

Definition
Computing is any type of calculation that includes both arithmetical and non-
arithmetical steps and follows a well defined model. It is the process of using computer
technology to complete a given goal-oriented task.

Computational thinking
 Computational thinking is the process of approaching a problem in a systematic manner.
 Creating and expressing a solution such that it can be carried out by a computer.
 It is not necessary that one need to be a computer scientist to think like a computer
scientist.
 Computational thinking is sometimes referred to as algorithmic thinking, but computational
thinking is the more comprehensive term.

How is Computational thinking used?


 Computational thinking can be applied by anyone who is attempting to solve problem and
have a computer to play a role in the solution
 Example
 To a computer scientist, it means the study of algorithms and their application to different
problems.
 To a mathematician, it might mean carrying out long division factoring

Computational thinking allows us

 To take a complex problem


 Understand what the problem is
 Develop possible solutions
 Present these solutions in a way that a computer, a human, or both, can understand

1.2 IDENTIFICATION OF COMPUTATIONAL PROBLEMS

 Computational problems are one of the main objects of study in theoretical computer
science.
 Computational thinking gives us the basis to organize our thought process, and a language
to communicate such organization to others including a computer.
 Computational problem solving does not simply involve the act of computer programming.
 The organizing part has relation to the study of algorithms, the language part to the design
of programming languages and systems and the last communication part to the activity of
coding in a given programming language.
 Computational thinking provides the foundation for learning how to translate any real-
world problem into a computer program or system through the use of algorithms,
languages and coding.
 Computational problem is a problem that can be solved step-by step with a computer.
 These problems usually have well-defined input, constraints and conditions that the output
must satisfy. Ex: Given a +ve integer n, find a nontrivial prime factor of n.

Steps in computational problems


Computational problem in general requires computational thinking ability. The 3 steps to
be followed in identifying and solving computational problems are
1. Problem specification
Start by analyzing problem, stating it precisely and establishing the criteria for the solution. It
involves:

 Abstraction: Removing the aspects of a problem that are not needed for its solution.
Example: When we are driving car, we are only concerned about driving the car like
start/stop the car, accelerate/break, etc. We are not concerned about how the actual
start/stop mechanism or accelerate or break mechanism works internally
 Decomposition: Breaking a big problem down into smaller and more manageable
sub problems
 Pattern Recognition: Analyzing any kind of repeated elements or sequences in the
problem. Example: photo recognition in photos

2. Algorithmic Expression
 Finding an algorithm which is a precise sequence of steps that solves the problem
using appropriate data representations. This step is called algorithmic thinking.

3. Solution implementation and evaluation

 Final stage is to create the actual solution and systematically evaluate it to determine
its correctness and efficiency. This step also involves seeing if the solution can be
generalized via automation or extension to other kinds of problems
Types of computational problems are
1. Decision problems
2. Search problems
3. Counting problems
4. Optimization problems

1. Decision problems: A decision problem is a yes-or-no question on an infinite set of


inputs. It is traditional to define the decision problem as the set of possible inputs together
with the set of inputs for which the answer is yes.
2. Search Problems: The algorithm provides search solutions through a sequence of actions
that transform the initial state to the goal state. A search problem consists of a search space,
start state, and goal state. Search algorithms help the AI agents to reach the goal state
through the assessment of situations and alternatives.
3 Counting problems: The task of finding the number of elements of some set with a
particular property. Such counting problems are usually encountered in combinations.
It is the task of counting the number of solutions to a problem.
4 Optimization problems
 In mathematics & computer science, an optimization problem is the problem of finding
the best solution from all possible solutions.
 For example, in the Bin Packing Problem (BPP) the aim is to find the right number
of boxes of a given size to store a set of objects of given sizes; optimization involves,
for example, finding the smallest number of boxes.

Computational Problem Solving Techniques

Problem solving technique is a set of techniques that helps in providing logic for solving a problem.
Problem solving can be expressed in the form of
1. Algorithms.
2. Flowcharts.
3. Pseudo codes.
4. Programs

1.3 ALGORITHM
Algorithm 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. The steps must be
ordered, unambiguous and finite in number

1.3.1 Properties of Algorithms


 Algorithm should be written in simple English
 Each and every instruction should be accurate and explicit
 Instructions in an algorithm should not be repeated infinitely.
 Algorithm should conclude after a finite number of steps.
 Should have an end point
 Derived results should be obtained only after the algorithm terminates.

Qualities of a good algorithm


 Time – To execute a program if the lesser time is required then it is the better one.
 Memory – To execute a program if the lesser memory is required then it is the better.
 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, “Welcome to python world”
Step 1: Start
Step 2: Print “Welcome to python world”
Step 3: Stop
1.4. BUILDING BLOCKS OF ALGORITHMS
1.4.1 Building blocks
i) Statement : A Statement is a single action in a computer. In a computer, statements might
include some of the following actions

 input data-information given to the program


 process data-perform operation on a given input
 output data-processed result

ii) State: Transition from one process to another process under specified condition within a
time is called state.
iii) 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.

a) Sequence: All the instructions are executed one after another is called sequence of
execution.
Example:
Add two numbers:
Step 1: Start
Step 2: get a,b
Step 3: calculate c=a+b
Step 4: Print c
Step 5: Stop

b) Selection: A selection statement causes the program control to be transferred to a


specific part of the program based upon 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 the person is eligible to vote?


Step 1: Start
Step 2: Get age
Step 3: if age >= 18 print “Eligible to vote”
Step 4: else print “Not eligible to vote”
Step 6: Stop

Flow Chart
C. Iteration: In some programs, certain set of statements are executed again and again based
upon conditional test. i.e. executed more than one time. This type of execution is called looping
or iteration.
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: while (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

iv) Functions
 Function is a sub program which consists of set of instructions or code 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


 Reuse the code
 Better readability
 Information hiding
 Improved maintainability
 Easy to debug and test

Example: Algorithm for addition of two numbers using function

Main function()
Step 1: Start
Step 2: Call the function add()
Step 3: Stop

sub function add()

Step 1: Function start


Step 2: Get a, b Values
Step 3: add c=a+b
Step 4: Print c
Step 5: Return
1.5 NOTATION

1.5.1 Flow Chart: Flow chart is defined as graphical representation of the logic for problem
solving. The purpose of flowchart is making the logic of the program clear in a visual
representation fol. Following Table shows the symbols and its description used in flowchart

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

 Only one flow line should enter a decision symbol. However, two or three flow lines may
leave the decision symbol.
 Only one flow line is used with a terminal symbol.

 Within standard symbols, write briefly and precisely.


 Intersection of flow lines should be avoided.

Advantages of flowchart:
1. Easy Communication: - Flowcharts are better way of communicating the logic of
a system to all involved.
2. Effective analysis: - With the help of flowchart, problem can be analyzed in
more effective way.
3. Efficient Coding: - The flowcharts act as a guide or blueprint during the
systems analysis and program development phase.
4. Efficient Program Maintenance: - The maintenance of operating program becomes
easy with the help of flowchart. It helps the programmer to put efforts more efficiently
on that part.
5. Documentation: - Program flowcharts serve as a good program documentation, which
is needed for various purposes.
6. Debugging: - The flowchart helps in debugging process.

Disadvantages of flow chart:


1. Complex logic: - Sometimes, the program logic is quite complicated. In that
case, flowchart becomes complex and uncoordinated.
2. Alterations and Modifications: - If alterations are required the flowchart may require
re-drawing completely.
3. Reproduction: - As the flowchart symbols cannot be typed, reproduction of flowchart
becomes a problem.
4. Cost: For large application the time and cost of flowchart drawing becomes costly.

1.5.2 PSEUDO CODE


 Pseudo code consists of short, readable and English languages used by an algorithm.
 It does not include details like variable declaration, subroutines.
 It is easier for the programmer or non programmer to understand the general working
of the program, because it is not based on any programming language.
 It gives us the sketch of the program before actual coding.
 It is not a machine readable
 Pseudo code can’t be compiled and executed.
 There is no standard syntax for pseudo code.

Guidelines for writing pseudo code


 Write one statement per line
 Capitalize initial keyword
 Indent to hierarchy
 End multiline structure
 Keep statements language independent
Common keywords used in pseudo code

The following gives common keywords used in pseudo codes.


1. //: This keyword 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 decision.
8. WHILE, ENDWHILE: used for iterative statements.
9. FOR, ENDFOR: Used for iterations ( incremented/decremented automatically)
Syntax for if else:
IF (condition)THEN
statement
... ELSE
statement
...
ENDIF
Example: Greatest of two numbers
BEGIN
READ a,b
IF (a>b) THEN
DISPLAY a is greater
ELSE
DISPLAY b is greater
END IF
END

Syntax for For:


FOR( start-value to end-value) DO
statement
...
ENDFOR
Example: Print n natural numbers
BEGIN
GET n
FOR i= 1 to n DO
PRINT i
i=i+1
ENDFOR
END

Syntax for While:


WHILE (condition) DO
statement
...
ENDWHILE
Example: Print n natural numbers
BEGIN GET n
INITIALIZE i=1
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 flowchart.
 Converting a pseudo code to programming language is very easy as compared with
converting a flowchart to programming language.

Disadvantages
 It does not provide visual representation of the program’s logic.
 There are no accepted standards for writing pseudo codes.
 It cannot be compiled nor executed.
 For a beginner, it is more difficult to follow the logic or write pseudo code as compared
to flowchart.

COMPARE: ALGORITHM, FLOWCHART AND PSEUDO CODE

1.5.3 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 program using
programming language.
 The user has to communicate with the computer using language which it can
understand.

Types of programming language


1. Machine language
2. Assembly language
3. High level language
1. Machine language

 The computer can understand only machine language which uses 0’s and 1’s.
 In machine language, different instructions are formed by taking different combinations
of 0’s and 1’s.

Advantages
 Translation free: Machine language is the only language which 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 computer. In this
case conversion process is not required.
 High speed: The machine language program is translation free. Since the conversion
time is saved, the execution of machine language program is extremely fast.

Disadvantage
 It is hard to find errors in a program written in the machine language.
 Writhing program in machine language is a time consuming process.

2. Assembly language
 Assembly language is logically equivalent to machine language but it is easier for people
to read, write and understand.
 Assembly language is symbolic representation of machine language.
 Assembly language is symbolic programming language that uses symbolic notation to
represent machine language instructions. They are called low level language because
they are so closely related to the machines.

Assembler: Assembler is the program which 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 assembly language program is more than machine
language program because assembler is needed to convert from assembly language to
machine language.

3. High level language


 High level language contains English words and symbols.
 The specified rules are to be followed while writing program in high level language.
 The interpreter or compilers are used for converting these programs into machine
readable form.
 Translating high level language to machine language: The programs that translate
high level language in to machine language are called interpreter or compiler.
o Compiler: A compiler is a program which translates the source code written in a
high level language in to object code which is in machine language program.
Compiler reads the whole program written in high level language and translates
it to machine language. If any error is found it display error message on the
screen.
o Interpreter: 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 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 program have the advantage of being
portable between machines.
 Easy debugging: Easy to find and correct error in high level language

Disadvantages
 Less efficient
 The translation process increases the execution time of the program. Programs in high
level language require more memory and take more execution time to execute.

High Level Languages are divided into 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

1 Interpreted programming languages

An interpreted language is a programming language for which most of its


implementation executes instructions directly, without previously compiling a program into
machine language instructions. The interpreter executes the program directly translating each
statement into a sequence of one or more subroutines already compiled into machine code.
Example: Pascal and Python

2 Functional programming languages

Functional programming language defines every computation as a mathematical


evaluation. They focus on the programming languages bound to mathematical calculations
Examples: Clean, Haskell.
3 Compiled Programming language
A compiled programming is a programming language whose implementation is typically
compilers and not interpreters. It will produce a machine code from source code.
Examples: C, C++

4 Procedural programming languages


Procedural (imperative) programming implies specifying the steps that the programs
should take to reach to an intended state. A procedure is a group of statements that can be
referred through a procedure call. Procedures help in the reuse of code. Procedural
programming makes the programs structured and easily traceable for program flow.
Examples: Hyper talk, MATLAB

5 Scripting language

Scripting language are programming languages that control an application. Scripts can
execute independent of any other application. They are mostly embedded in the application
that they control and are used to automate frequently executed tasks like communicating with
external program.
Examples: Apple script, VB script, Java script

6 Markup languages

A markup language is an artificial language that uses annotations to text that define how
the text is to be displayed.
Examples: HTML, XML

7 Concurrent programming languages

Concurrent programming is a computer programming technique that provides for the


execution of operation concurrently, either with in a single computer or across a number of
systems. Examples: Joule, Limbo

8 Object oriented programming language

Object oriented programming is a programming pattern based on the concept of objects


which may contain data in the form of procedures often known as methods.
Examples: Java, C++

1.6 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 is not one that works most of the time, but one that works correctly
for all permitted inputs.
Understand the problem

Problem solving technique

Design an Algorithm

Prove correctness

Analyze the Algorithm

Code the Algorithm


Flowchart for algorithmic problem solving

Problem solving technique:

 This means solving problems that require the formulation of an algorithm for the
solution.
 Algorithms are procedural solutions to problems.
 Good algorithms take less time and memory to perform a task.
 Problems to which algorithmic solutions may be required fall into 4 basic classes:

i. Tractable problems: Those that admit solutions that runs in 'reasonable


time.(eg sorting and searching).
ii. Intractable problems: Those that probably don't have reasonable• time
algorithmic solutions. (eg the Travelling Salesman problem).
iii. Non-solvable problems: Those that definitely don't have exact solutions (e.g
the Towers of Hanoi).
iv. Non-computable problems: Those that can't be solved algorithmically
at all.(eg the halting problem).

Designing an Algorithm

 Algorithm design refers to a method or a mathematical process for problem-solving


and engineering algorithms. The design of algorithms is part of many solution
theories of operation research, such as dynamic programming and divide-and-conquer.
 The algorithm design techniques provide a powerful set of general approaches.
 Several techniques need to be combined known design techniques.
Proving an Algorithm's Correctness:
Once an algorithm is specified, its correctness has to be proved. That is, to prove that,
the algorithm yields a required result for every legitimate input in a finite amount of time.

Analyzing an Algorithm
Next to correctness, the desirable characteristic features to be analyzed are
• Efficiency
• Simplicity
• Generality
The two kinds of algorithm efficiency are
• time efficiency-indicating how fast the algorithm runs
• space efficiency-indicating how much extra memory it uses
Coding an Algorithm
• Most of algorithms are implemented as computer programs.
• The validity of programs is still established by testing.
• A working program provides an additional opportunity in allowing an
experimental analysis of the basic algorithm.
• Such an analysis is based on timing the program on several inputs and then
analyzing the results obtained.
• A good algorithm is a result of repeated effort and rework.
• The important is use of algorithmic problem solving is the question of whether all
problems can be solved by an algorithm or not.

1.7. SIMPLE STRATEGIES FOR DEVELOPING ALGORITHMS

1.7.1 ITERATION
 Iteration is the act of repeating a process, either to generate an unbounded sequence of
outcomes, or with the aim of approaching a desired goal, target or result.
 Each repetition of the process is also called iteration
 Result of each iteration as the starting point for the next iteration.
 When a sequence of instructions is executed in a repeated manner, it is called a loop.
They are used in programming to repeat sections of code.
Examples where iteration is important
• Calculating f(x) for 100 values of x
• Computing a result iteratively until a certain accuracy is reached, such as in
evaluating a series like x = x+1+2+3+ .... n
• Printing a table of results
• Reading large quantities of data from a data file
• Working with arrays
Iterative Structures
There are three types of iterative structures
i. for loop: Loop for a specific number of iterations based on an index variable
ii. while loop: Continue looping while a condition is true - Pre-test on the condition,
so loop is executed 0 or more times.
iii. do-while loop:- Continue looping while a condition is true - Post-test on the
condition, so loop is executed 1 or more times.
1.7.2 RECURSION

 The process of calling a function by itself is called recursion and the function which
calls itself is called recursive function.
 Recursion is used to solve various mathematical problems by dividing it into smaller
problems. This method of solving a problem is called Divide and Conquer.
 In programming, it is used to divide complex problem into simpler ones and solving
them individually.

Advantages of recursion
 Recursive functions make the code look clean and elegant.
 A complex task can be broken down into simpler sub-problems using recursion.
 Sequence generation is easier with recursion than using some nested iteration.

Disadvantages of recursion
 Sometimes the logic behind recursion is hard to follow through.
 Recursive calls are expensive (inefficient) as they take up a lot of memory and time.
 Recursive functions are hard to debug.

Pseudo code for factorial using recursion:

Main function:
BEGIN
GET n
CALL factorial(n)
PRINT fact
END

Sub function factorial(n):


IF(n==1) THEN
fact=1
RETURN fact
ELSE
RETURN fact=n*factorial(n-1)
Differences between Recursion and Iterative
S.No RECURSION ITERATION
1 Recursive function is a function that calls Iterative Instructions are loop based
itself repetitions of a process
2 Uses selection structure Uses repetition structure
3 Terminates when a base case is recognized Terminates when the loop-condition
fails
4 Uses more memory than iteration Consume less memory
5 recursion makes code smaller Iteration makes code longer

ILLUSTRATIVE PROGRAMS
1. To find a minimum value in a given list
To find the minimum element, take the first element and compare its value against
the values of all other elements. Once we find a smaller element, we continue the
comparisons with its value. Finally we find the minimum
Example : Let the list of elements be 2,5,8,4,1,3,4,9,6
Diagram

Algorithm
Step 1: Start
Step 2: Set Initial value list[10], i=0,n=0,min=0
Step 3: Input n;
Step 4: Loop: Read i from 0 to n-1
Step 5: Input list[i]
Step 6: End Loop
Step 7: Set Min=list[0]
Step 8: Loop: Read i from 1 to n-1
Step 9: if list[i]<min then
Step 10: Set min=list[i]
Step 11: End Loop
Step 12: Print min
Step 13: Stop.
Pseudo code
BEGIN
GET n
INITIALIZE list[10],i=0;
WHILE(i<n-1) DO
GET list[i]
END WHILE
Min=list[0]
WHILE(i+1<n-1) DO
IF list[i]<min
min=list[i]
END WHILE
PRINT min
END

Flow chart
2. Insert a card in a list of sorted cards

To insert a card is set of list of sorted cards, first find the position of to insert. Then shift
the other cards and insert the card
Example : Let the list contains 2,3,7,8 10, 13. Let the value to insert is 5, then
position to insert is 2
Diagram :

Algorithm
Step 1: Start
Step2: Read all nos in an array a[]
Step 3: Read value to be inserted Flow Chart
Step 4: if value<a[i] then assign i to pos
Step 5: Return pos
Step 6: From n-1 to pos move one position down
Step 7: Store the value in pos location
Step 8: Stop

Pseudo code
START
GET a[]
INT n, value, pos
FOR I IN 0 to N-1
IF(value<a[i])
pos=i
RETURN pos
BREAK
END IF
END FOR
FOR i IN n-1 to pos, i--
a[i+1]=a[i]
END FOR
a[pos]=value
STOP
3. To guess an integer number in a range problem.

Procedure
 To guess an integer, first set a number in the range as hidden.
 Get the guess from the user and check if they are same.
 Print according to the result

Algorithm

Step 1: Start
Step 2: Declare hidden, guess, range =1 to 100
Step 3: compute hidden=choose a random value in a range
Step 4: Read guess
Step 5: If guess=hidden, then
Step 6: print “Guess is hit”
Step 7: Else
Step 8: Print “Guess is not hit”
Step 9: Print hidden
Step 10: Stop

Pseudo code
BEGIN
GET a random number
Hidden = random number
Read Guess
IF (value=guess)
Print(“ Success. Guess is hit”)
Else
Print(“ not success. Guess is not hit”)
STOP

Flowchart
4. Tower of Hanoi problem

Procedure: The mission is to move all the disks to some another tower without violating the
sequence of arrangement. A few rules to be followed for tower of Hanoi are

Rules
 Only one disk can be moved among the towers at any given time
 Only the top disk can be removed
 No large disk can sit over a small disk

Diagram

Algorithm
Here we mark 3 towers with names, source and destination and aux.

Hanoi(disk, source, dest, aux)


Step 1: If only one disk is there then move disk from source to destination
Step 2: If more than one disk is there, then
2.1 recursively call Hanoi(disk-1, source, aux,dest)
2.2 Move disk from source to destination
2.3 Recusively call Hanoi(disk-1,aux, dest, source)
Step 3: Stop.

Pseudo Code
START
Procedure Hanoi(disk, source, dest, aux)
IF disk==1 THEN
move disk from source to dest
ELSE
Hanoi(disk-1, source, aux, dest,)
move disk from source to dest
Hanoi(disk-1, aux, de s t , s o u r c e )
ENDIF
END Procedure
Stop
Flow Chart

MORE ILLUSTRATIVE PROGRAMS [FOR STUDENT’S PRACTICE]

1 Write an algorithm to find area of a rectangle

Algorithm Flowchart
Step 1: Start
Step 2: get l,b values
Step 3: Calculate A=l*b
Step 4: Display A
Step 5: Stop

Pseudo Code

BEGIN READ l,b


CALCULATE A=l*b
DISPLAY A
END
2 Write an algorithm to Calculate a) Area & b) Circumference of circle

Algorithm Flowchart

Step 1: Start
Step 2: get r value
Step 3: Calculate A=3.14*r*r
Step 4: Calculate C=2.3.14*r
Step 5: Display A,C
Step 6: Stop

Pseudo Code
BEGIN
READ r
CALCULATE A and C
A=3.14*r*r
C=2*3.14*r
DISPLAY A, C
END

3. Write an algorithm to Calculate Simple interest


Algorithm Flowchart
Step 1: Start
Step 2: get p, n, r value
Step3: Calculate SI=(p*n*r)/100
Step 4: Display SI
Step 5: Stop

Pseudo Code
BEGIN
READ p, n, r
CALCULATE SI=(p*n*r)/100
DISPLAY SI
END
4 Write an algorithm to Calculate engineering cutoff

Algorithm Flowchart
Step 1: Start
Step2: get P,C,M value
Step3: calculate Cutoff= P/4+C/4+M/2
Step 4: Display Cutoff
Step 5: Stop

Pseudo Code

BEGIN
READ P,C,M
CALCULATE Cutoff= P/4+C/4+M/2
DISPLAY Cutoff
END

5 To check greatest of two numbers


Algorithm Flowchart
Step 1: Start
Step 2: get a,b value
Step 3: check if(a>b) print “A is greater”
Step 4: else “B is greater”
Step 5: Stop

Pseudo Code
BEGIN
READ a,b
IF (a>b) THEN
DISPLAY “A is greater”
ELSE
DISPLAY “B is greater”
END IF
END

6 To check leap year or not


Algorithm
Flowchart
Step 1: Start
Step 2: get y
Step 3: if(y%4==0) print ” leap year”
Step 4: else print “ not leap year”
Step 5: Stop

23 PANIMALAR ENGINE ERING COLLEGE CHENNAI CITY CAMPUS – Python R 2021


Pseudo Code
BEGIN
READ y
IF (y%4==0) THEN
DISPLAY “leap year”
ELSE
DISPLAY “not leap year”
END IF
END

7 To check positive or negative number


Algorithm Flowchart
Step 1: Start
Step 2: get n
Step 3: check if(n>0) print “N is positive”
Step 4: else “N is negative”
Step 5: Stop

PseudoCode
BEGIN
READ n
IF (n>0) THEN
DISPLAY “n is positive”
ELSE
DISPLAY “n is negative”
END IF
END

8 To check odd or even number


Algorithm Flowchart
Step 1: Start
Step 2: get n
Step 3: check if(n%2==0) print n is even
Step 4: else n is odd
Step 5: Stop
Pseudo Code
BEGIN
READ n
IF (n%2==0) THEN
DISPLAY “n is even”
ELSE
DISPLAY “n is odd”
END IF
END

24 PANIMALAR ENGINEERING COLLEGE CHENNAI CITY CAMPUS – Python R 2021


9 To check greatest of three numbers
Algorithm Flowchart
Step1: Start
Step2: Get A, B, C
Step3: if(A>B) & (A>C) Print “A is greatest”
Step4: else If(B>C) Print “B is greatest”
Step4: else Print “C is greatest”
Step6: Stop
PseudoCode
BEGIN
READ a, b, c
IF (a>b) && (a>c) THEN
DISPLAY “A is greatest”
ELSE IF(b>c) THEN
DISPLAY “B is greatest”
ELSE
DISPLAY “ C is greatest”
END IF
END IF
END

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


Algorithm Flowchart
Step 1: Start
Step 2: Get n value.
Step 3: If (n==0) print “n is Zero”
Step 4: Else if(n > 0) Print “ n is +ve”
Step 5: Else Print “n is -ve”
Step 6: Stop
PseudoCode
BEGIN
GET n
IF(n==0) THEN
DISPLAY “ n is zero”
ELSE IF(n>0) THEN
DISPLAY “n is positive”
ELSE
DISPLAY “n is positive”
END IF
END IF
END

25 PANIMALAR ENGINEERING CO LLEGE CHENNAI CITY CAMPUS – Python R 2021


11 Write an algorithm to print all N number of natural numbers

ALGORITHM FLOWCHART

Step 1: Start
Step 2: get n value.
Step 3: initialize i=1
Step 4: if (i<=n) print i value
Step 5: else go to step 8
step 6 : increment i value by 1
Step 7: go to step 4
Step 8: Stop

PSEUDO CODE
BEGIN
GET n
INITIALIZE i=1
WHILE(i<=n) DO
PRINT i
i=i+1
END WHILE
END

12 Write an algorithm to print Even numbers

Algorithm Flowchart
Step 1: start
step 2: get n value
step 3: set initial value i=2
step 4: check if(i<=n) print i value
step 5: else goto step 8
step 6: increment i value by 2
step 7: goto step 4
step 8: stop

BEGIN GET n
INITIALIZE i=2
WHILE(i<=n) DO
PRINT i
i=i+2
END WHILE
END

26 PANIMALAR ENGINEERING
COLLEGE CHENNAI CITY CAMPUS – Python R 2021
13 Write an algorithm to print squares of the given N numbers
Algorithm Flowchart
Step 1: start
step 2: get n value
step 3: set initial value i=1
step 4: check if(i<=n) print i*i value
step 5: else goto step8
step 6: increment i value by 1
step 7: goto step 4
step 8: stop

Pseudo code
BEGIN GET n
INITIALIZE i=1
WHILE(i<=n) DO
PRINT i*i
i=i+1
ENDWHILE
END

14 Write an algorithm to find the sum of the given N number


Algorithm Flowchart
Step 1: start
step 2: get n value
step 3: set initial value i=1, sum=0
Step 4: check if(i<=n) goto step 5 else goto step8
step 5: calculate sum=sum+i
step 6: increment i value by 1
step 7: goto step 4
step 8: print sum value
step 9: stop

Pseudo code
BEGIN
GET n
INITIALIZE i=1,sum=0
WHILE(i<=n) DO
sum=sum+i
i=i+1
ENDWHILE
PRINT sum
END

15 Write an algorithm to find factorial of a given number

27 PANIMALAR ENGINEERING COLLEGE CHENNAI CITY CAMPUS – Python R 2021


Algorithm Flowchart
Step 1: start
step 2: get n value
step 3: set initial value i=1, fact=1
Step 4: check i value if(i<=n) goto step 5 else
goto step8
step 5: calculate fact=fact*I
step 6: increment i value by 1
step 7: goto step 4
step 8: print fact value
step 9: stop

Pseudo Code
BEGIN
GET n
INITIALIZE i=1,fact=1
WHILE(i<=n) DO
fact=fact*i
i=i+1
ENDWHILE
PRINT fact
END

28 PANIMALAR ENGINEERING COLLEGE CHENNAI CITY CAMPUS – Python R 2021

You might also like