0% found this document useful (0 votes)
7 views32 pages

GE 8151-Notes

The document provides an overview of algorithmic problem solving, including the properties of algorithms, problem-solving techniques, and the building blocks of algorithms such as sequence, selection, and iteration. It also discusses the importance of flowcharts and pseudocode in representing algorithms, along with guidelines for writing them. Additionally, it covers programming languages, their types, and the roles of assemblers, compilers, and interpreters in translating high-level languages to machine code.

Uploaded by

saraswathig258
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)
7 views32 pages

GE 8151-Notes

The document provides an overview of algorithmic problem solving, including the properties of algorithms, problem-solving techniques, and the building blocks of algorithms such as sequence, selection, and iteration. It also discusses the importance of flowcharts and pseudocode in representing algorithms, along with guidelines for writing them. Additionally, it covers programming languages, their types, and the roles of assemblers, compilers, and interpreters in translating high-level languages to machine code.

Uploaded by

saraswathig258
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/ 32

IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN

CHINNASALEM – 606 201. CHINNASALEM – 606 201.


UNIT I ALGORITHMIC PROBLEM SOLVING 9 Properties of Algorithms
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  Should be written in simple English
cards, and guess an integer number in a range, Towers of Hanoi.
 Each and every instruction should be precise and unambiguous.
PROBLEM SOLVING  Instructions in an algorithm should not be repeated infinitely.
 Algorithm should conclude after a finite number of steps.
Problem solving is the systematic approach to define the problem and creating number
 Should have an end point
of solutions.
 Derived results should be obtained only after the algorithm terminates.

The problem solving process starts with the problem specifications and ends with a
Qualities of a good algorithm
Correct program.
The following are the primary factors that are often used to judge the quality of the
PROBLEM SOLVING TECHNIQUES algorithms.

Problem solving technique is a set of techniques that helps in providing logic for solving Time – To execute a program, the computer system takes some amount of time. The
a problem. lesser is the time required, the better is the algorithm.

Problem Solving Techniques: Memory – To execute a program, computer system takes some amount of memory
space. The lesser is the memory required, the better is the algorithm.
Problem solving can be expressed in the form of

Accuracy – Multiple algorithms may provide suitable or correct solutions to a given


1. Algorithms.
problem, some of these may provide more accurate results than others, and such
2. Flowcharts.
algorithms may be suitable.
3. Pseudo codes.
4. programs Example

ALGORITHM Write an algorithm to print „Good Morning”

It is defined as a sequence of instructions that describe a method for solving a problem. Step 1: Start
In other words it is a step by step procedure for solving a problem.
Step 2: Print “Good Morning”

Step 3: Stop

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 1 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 2
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.

BUILDING BLOCKS OF ALGORITHMS (statements, state, control flow, Sequence:

functions)
All the instructions are executed one after another is called sequence execution.

Algorithms can be constructed from basic building blocks namely, sequence, selection
Example:
and iteration.
Add two numbers:
Statements:
Step 1: Start
Statement is a single action in a computer.
Step 2: get a,b
In a computer statements might include some of the following actions
Step 3: calculate c=a+b
 input data-information given to the program
Step 4: Display c
 process data-perform operation on a given input
 output data-processed result Step 5: Stop

State: Selection:

Transition from one process to another process under specified condition with in a A selection statement causes the program control to be transferred to a specific part of
time is called state. the program based upon the condition.

Control flow: If the conditional test is true, one part of the program will be executed, otherwise it will
execute the other part of the program.
The process of executing the individual statements in a given order is called control
flow.

The control can be executed in three ways

1. sequence
2. selection
3. iteration

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 3 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 4
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.

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
Example
Step 7: Stop
Write an algorithm to check whether he is eligible to vote?
Functions:
Step 1: Start
 Function is a sub program which consists of block of code(set of instructions) that
Step 2: Get age
performs a particular task.

Step 3: if age >= 18 print “Eligible to vote”  For complex problems, the problem is been divided into smaller and simpler tasks
during algorithm design.
Step 4: else print “Not eligible to vote”
Benefits of Using Functions
Step 6: Stop
 Reduction in line of code
Iteration:  code reuse
 Better readability
In some programs, certain set of statements are executed again and again based upon
 Information hiding
conditional test. i.e. executed more than one time. This type of execution is called
 Easy to debug and test
looping or iteration.
 Improved maintainability

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 5 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 6
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.

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

Rules for drawing a flowchart


Step 5: Return

1. The flowchart should be clear, neat and easy to follow.

2. The flowchart must have a logical start and finish.


FLOW CHART
3. Only one flow line should come out from a process symbol.
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.

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 7 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 8
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.

4. Only one flow line should enter a decision symbol. However, two or three flow lines 6. Efficient Program Maintenance: - The maintenance of operating program
may leave the decision symbol. becomes easy with the help of flowchart. It helps the programmer to put efforts more
efficiently on that part.

Disadvantages of flow chart:

1. Complex logic: - Sometimes, the program logic is quite complicated. In that case,
5. Only one flow line is used with a terminal symbol.
flowchart becomes complex and clumsy.

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
6. Within standard symbols, write briefly and precisely. flowchart becomes a problem.
4. Cost: For large application the time and cost of flowchart drawing becomes
7. Intersection of flow lines should be avoided.
costly.
Advantages of flowchart:
PSEUDO CODE:
1. Communication: - Flowcharts are better way of communicating the logic of a
system to all concerned.
 Pseudo code consists of short, readable and formally styled English languages used

2. Effective analysis: - With the help of flowchart, problem can be analyzed in more for explain an algorithm.
 It does not include details like variable declaration, subroutines.
effective way.
 It is easier to understand for the programmer or non-programmer to understand the
3. Proper documentation: - Program flowcharts serve as a good program
documentation, which is needed for various purposes. general working of the program, because it is not based on any programming

4. Efficient Coding: - The flowcharts act as a guide or blueprint during the systems language.
 It gives us the sketch of the program before actual coding.
analysis and program development phase.
 It is not a machine readable
5. Proper Debugging: - The flowchart helps in debugging process.
 Pseudo code can’t be compiled and executed.

 There is no standard syntax for pseudo code.

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 9 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 10
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.

Guidelines for writing pseudo code: Example: Greatest of two numbers

 Write one statement per line BEGIN


 Capitalize initial keyword READ a, b
 Indent to hierarchy
IF (a>b) THEN
 End multiline structure
DISPLAY a is greater
 Keep statements language independent
ELSE
Common keywords used in pseudocode
DISPLAY b is greater
The following gives common keywords used in pseudocodes.
END IF
1. //: This keyword used to represent a comment.
END
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.


Syntax for For:
4. COMPUTE, CALCULATE: used for calculation of the result of the given expression. FOR( start-value to end-value) DO
5. ADD, SUBTRACT, INITIALIZE used for addition, subtraction and initialization. statement
6. OUTPUT, PRINT, DISPLAY: It is used to display the output of the program.
...
7. IF, ELSE, ENDIF: used to make decision.
ENDFOR
8. WHILE, ENDWHILE: used for iterative statements.

9. FOR, ENDFOR: Another iterative incremented/decremented tested automatically. Example: Print n natural numbers

BEGIN
Syntax for if else:
GET n
IF (condition)THEN
statement INITIALIZE i=1

FOR (i<=n) DO
ELSE
statement PRINT i
...
i=i+1
ENDIF
ENDFOR

END

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 11 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 12
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.

Syntax for While:  Converting a pseudo code to programming language is very easy as compared with

converting a flowchart to programming language.


WHILE (condition) DO

statement
Disadvantages:

...  It does not provide visual representation of the program’s logic.

ENDWHILE  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
Example: Print n natural numbers compared to flowchart.

BEGIN

GET n Example:

INITIALIZE i=1 Addition of two numbers:

WHILE(i<=n) DO BEGIN

PRINT i GET a,b

ADD c=a+b
i=i+1
PRINT c
ENDWHILE
END

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.

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 13 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 14
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.

PROGRAMMING LANGUAGE Disadvantage:

A programming language is a set of symbols and rules for instructing a computer to  It is hard to find errors in a program written in the machine language.

perform specific tasks. The programmers have to follow all the specified rules before  Writhing program in machine language is a time consuming process.
writing program using programming language. The user has to communicate with the
computer using language which it can understand.
Machine dependent: According to architecture used, the computer differs from each

Types of programming language other. So machine language differs from computer to computer. So a program
developed for a particular type of computer may not run on other type of computer.
1. Machine language

2. Assembly language Assembly language:


3. High level language
To overcome the issues in programming language and make the programming process

Machine language: easier, an assembly language is developed which is logically equivalent to machine
language but it is easier for people to read, write and understand.
The computer can understand only machine language which uses 0’s and 1’s. In
 Assembly language is symbolic representation of machine language. Assembly
machine language the different instructions are formed by taking different
languages are symbolic programming language that uses symbolic notation to
combinations of 0’s and 1’s.
represent machine language instructions. They are called low level language because
Advantages: they are so closely related to the machines.

Translation free: Assembler

Machine language is the only language which the computer understands. For executing Assembler is the program which translates assembly language instruction in to a
any program written in any programming language, the conversion to machine machine language.
language is necessary. The program written in machine language can be executed
 Easy to understand and use.
directly on computer. In this case any conversion process is not required.

 It is easy to locate and correct errors.


High speed

The machine language program is translation free. Since the conversion time is saved,
the execution of machine language program is extremely fast.

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 15 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 16
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.

Disadvantage Compiler:

Machine dependent 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
The assembly language program which can be executed on the machine depends on the
whole program written in high level language and translates it to machine language. If
architecture of that computer.
any error is found it display error message on the screen.
Hard to learn
Interpreter
It is machine dependent, so the programmer should have the hardware knowledge to
Interpreter translates the high level language program in line by line manner. The
create applications using assembly language.
interpreter translates a high level language statement in a source program to a machine
Less efficient 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
 Execution time of assembly language program is more than machine language
screen.
program.
 Because assembler is needed to convert from assembly language to machine Advantages
language. Readability

High level language is closer to natural language so they are easier to learn and
High level language
understand

High level language contains English words and symbols. The specified rules are to be Machine independent
followed while writing program in high level language. The interpreter or compilers are High level language program have the advantage of being portable between machines.
used for converting these programs in to machine readable form.
Easy debugging

Translating high level language to machine language Easy to find and correct error in high level language

The programs that translate high level language in to machine language are called Disadvantages

interpreter or compiler. 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.

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 17 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 18
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.

Examples:
Categories of programming languages
Clean
They are divided into following categories:
Haskell
1. Interpreted programming languages
Compiled Programming language:
2. Functional programming languages
3. Compiled programming languages A compiled programming is a programming language whose implementation are
4. Procedural programming languages typically compilers and not interpreters.
5. Scripting programming language
It will produce a machine code from source code.
6. Markup programming language
7. Concurrent programming language Examples:
8. Object oriented programming language
C

Interpreted programming languages: C++

C#
An interpreted language is a programming language for which most of its
implementation executes instructions directly, without previously compiling a program JAVA

into machine language instructions. The interpreter executes the program directly Procedural programming language:
translating each statement into a sequence of one or more subroutines already
Procedural (imperative) programming implies specifying the steps that the programs
compiled into machine code.
should take to reach to an intended state.
Examples:
A procedure is a group of statements that can be referred through a procedure call.
Pascal
Procedures help in the reuse of code. Procedural programming makes the programs
Python structured and easily traceable for program flow.
Functional programming language:
Examples:
Functional programming language defines every computation as a mathematical
Hyper talk
evaluation. They focus on the programming languages are bound to mathematical
MATLAB
calculations

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 19 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 20
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.

Scripting language: Object oriented programming language:

Scripting language are programming languages that control an application. Scripts can Object oriented programming is a programming paradigm based on the concept of
execute independent of any other application. They are mostly embedded in the
objects which may contain data in the form of procedures often known as methods.
application that they control and are used to automate frequently executed tasks like
communicating with external program.
Examples:
Examples:
Lava
Apple script
Moto
VB script
ALGORITHMIC PROBLEM SOLVING:
Markup languages:
Algorithmic problem solving is solving problem that require the formulation of an
A markup language is an artificial language that uses annotations to text that define
algorithm for the solution.
hoe the text is to be displayed.

Examples:

HTML

XML

Concurrent programming language:

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

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 21 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 22
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.

Understanding the Problem  First, they provide guidance for designing algorithms for new problems,

 Second, algorithms are the cornerstone of computer science


 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. Methods of Specifying an Algorithm
 A correct algorithm is not one that works most of the time, but one that works
 Pseudocode is a mixture of a natural language and programming language-like
correctly for all legitimate inputs.
constructs. Pseudocode is usually more precise than natural language, and its usage
Ascertaining the Capabilities of the Computational Device often yields more succinct algorithm descriptions.
 In the earlier days of computing, the dominant vehicle for specifying algorithms was
 If the instructions are executed one after another, it is called sequential algorithm.
a flowchart, a method of expressing an algorithm by a collection of connected
 If the instructions are executed concurrently, it is called parallel algorithm.
geometric shapes containing descriptions of the algorithm’s steps.

Choosing between Exact and Approximate Problem Solving  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


 The next principal decision is to choose between solving the problem exactly or
language. We can look at such a program as yet another way ofspecifying the
solving it approximately.
algorithm, although it is preferable to consider it as the algorithm’s implementation.
 Based on this, the algorithms are classified as exact algorithm and
approximationalgorithm. Proving an Algorithm’s Correctness

Deciding a data structure:  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
 Data structure plays a vital role in designing and analysis the algorithms.
a finite amount of time.
 Some of the algorithm design techniques also depend on the structuring data
 A common technique for proving correctness is to use mathematical induction
specifying a problem’s instance
because an algorithm’s iterations provide a natural sequence of steps needed for
 Algorithm+ Data structure=programs.
such proofs.
 It might be worth mentioning that although tracing the algorithm’s performance for
Algorithm Design Techniques
a few specific inputs can be a very worthwhile activity, it cannot prove the
 An algorithm design technique (or “strategy” or “paradigm”) is a general approach algorithm’s correctness conclusively. But in order to show that an algorithm is
to solving problems algorithmically that is applicable to a variety of problems from incorrect, you need just one instance of its input for which the algorithm fails.
different areas of computing.
 Learning these techniques is of utmost importance for the following reasons.

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 23 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 24
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.

Analysing an Algorithm 1. for loop


2. While loop
1. Efficiency.
Syntax for For:
Time efficiency, indicating how fast the algorithm runs,

FOR( start-value to end-value) DO


Space efficiency, indicating how much extra memory it uses.
Statement
2. simplicity.
...
 An algorithm should be precisely defined and investigated with mathematical ENDFOR
expressions.
Example: Print n natural numbers
 Simpler algorithms are easier to understand and easier to program.

 Simple algorithms usually contain fewer bugs. BEGIN

GET n
Coding an Algorithm
INITIALIZE i=1
 Most algorithms are destined to be ultimately implemented as computer programs.
FOR (i<=n) DO
Programming an algorithm presents both a peril and an opportunity.
 A working program provides an additional opportunity in allowing an empirical
PRINT i

analysis of the underlying algorithm. Such an analysis is based on timing the i=i+1
program on several inputs and then analysing the results obtained. ENDFOR

SIMPLE STRATEGIES FOR DEVELOPING ALGORITHMS: END

Syntax for While:


1. iterations
WHILE (condition) DO
2. Recursions
Statement
1. Iterations: ...

A sequence of statements is executed until a specified condition is true is called ENDWHILE

iterations.

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 25 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 26
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.

Example: Print n natural numbers Recursions:

BEGIN  A function that calls itself is known as recursion.

GET n  Recursion is a process by which a function calls itself repeatedly until some specified

condition has been satisfied.


INITIALIZE i=1

WHILE(i<=n) DO

PRINT i
Algorithm for factorial of n numbers using recursion:
i=i+1
Main function:
ENDWHILE

END Step1: Start

Step2: Get n

Step3: call factorial(n)

Step4: print fact

Step5: Stop

Sub function factorial(n):

Step1: if(n==1) then fact=1 return fact

Step2: else fact=n*factorial(n-1) and return fact

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 27 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 28
IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201.

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)

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 29


IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201.

More examples:

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

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 30


IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201.

Write an algorithm for Calculating area and circumference of circle

Step 1: Start BEGIN

Step 2: get r value READ r

Step 3: Calculate A=3.14*r*r CALCULATE A and C

Step 4: Calculate C=2.3.14*r A=3.14*r*r

Step 5: Display A,C C=2*3.14*r

Step 6: Stop DISPLAY A

END

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 31


IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201.

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

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 32


IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201.

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

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 33


IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201.

To check greatest of two numbers

Step 1: Start BEGIN

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

END

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 34


IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201.

To check leap year or not

Step 1: Start BEGIN

READ y
Step 2: get y
IF (y%4=0) THEN
Step 3: if(y%4=0) print leap year
DISPLAY leap year
Step 4: else print not leap year ELSE

Step 5: Stop DISPLAY not leap year

END IF

END

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 35


IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201.

To check positive or negative number

Step 1: Start BEGIN

Step 2: get n READ n

Step 3: check if(n>0) print a IF (n>0) THEN


is positive
DISPLAY n is positive
Step 4: else n is negative
ELSE
Step 5: Stop
DISPLAY n is negative

END IF

END

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 36


IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201.

To check odd or even number

Step 1: Start BEGIN

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

END

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 37


IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201.

To check greatest of three numbers

Step1: Start BEGIN


READ a, b, c
Step2: Get A, B, C IF (a>b) THEN
IF(a>c) THEN
Step3: if(A>B) then DISPLAY a is greater
ELSE
3.1: If(A>C) print A else DISPLAY c is greater
print C END IF
ELSE
else If(B>C) print B else IF(b>c) THEN
print C DISPLAY b is greater
ELSE
Step6: Stop
DISPLAY c is greater
END IF
END IF
END

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 38


IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201.

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

Step 1: Start BEGIN

GET n
Step 2: Get n value.
IF(n==0) THEN
Step 3: if (n ==0) print
DISPLAY “ n is zero”
then “Given number is
Zero” ELSE

IF(n>0) THEN
3.1 : Else if (n > 0) then
DISPLAY “n is positive”
Print then “Given number
is +ve” ELSE

DISPLAY “n is positive”
3.2: else Print “Given
number is -ve” END IF

END IF
Step 4: Stop
END

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 39


IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201.

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) then repeat the WHILE(i<=n) DO


steps
PRINT i
4.1: Print i value
i=i+1
4.2 : increment i value by 1
ENDWHILE
Step 5: Stop
END

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 40


IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201.

Write an algorithm to print n odd numbers

Step 1: Start BEGIN

Step 2: get n value. GET n

Step 3: initialize i=1 INITIALIZE i=1

Step 4: if (i<=n) then repeat the WHILE(i<=n) DO


steps
PRINT i
4.1: Print i value
i=i+2
4.2 : increment i value by 2
ENDWHILE
Step 5: Stop
END

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 41


IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201.

Write an algorithm to print n even numbers

Step 1: Start BEGIN

Step 2: get n value. GET n

Step 3: initialize i=2 INITIALIZE i=2

Step 4: if (i<=n) then repeat the WHILE(i<=n) DO


steps
PRINT i
4.1: Print i value
i=i+2
4.2 : increment i value by 2
ENDWHILE
Step 5: Stop
END

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 42


IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201.

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 if(i<=n) then WHILE(i<=n) DO


repeat the following steps
PRINT i*i
step 4.1: print i*i value
i=i+2
step 4.2: increment i value by 1
ENDWHILE
step 5: stop
END

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 43


IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201.

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 if(i<=n) then WHILE(i<=n) DO


repeat the following steps
PRINT i*i*i
step 4.1: print i*i*i value
i=i+2
step 4.2: increment i value by 1
ENDWHILE
step 5: stop
END

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 44


IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201.

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 i=1, sum=0 INITIALIZE i=1,sum=0

Step 4: check i value if(i<=n) then WHILE(i<=n) DO


repeat the following steps
sum=sum+i
step 4.1: calculate sum=sum+i
i=i+1
step 4.2: increment i value by 1
ENDWHILE
step 5: print sum value
PRINT sum
step 6: stop
END

Write an algorithm to find factorial of a given number


GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 45
IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201.

Step 1: start BEGIN

step 2: get n value GET n

step 3: set initial value i=1, fact=1 INITIALIZE i=1,fact=1

Step 4: check i value if(i<=n) then WHILE(i<=n) DO


repeat the following steps
fact=fact*i
step 4.1: calculate fact=fact*i
i=i+1
step 4.2: increment i value by 1
ENDWHILE
step 5: print fact value
PRINT fact
step 6: stop
END

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 46

You might also like