0% found this document useful (0 votes)
32 views80 pages

Introduction (30.07.2024)

Uploaded by

pushpatiwary1
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)
32 views80 pages

Introduction (30.07.2024)

Uploaded by

pushpatiwary1
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/ 80

BCSE101E Computer Programming: Python

Dr. Raviteja Buddala.


School of Mechanical Engineering
Vellore Institute of Technology, Vellore-632014

31-07-2024 1
Introduction
• Purpose
Every engineering student should learn how to program for problem solving in the
domain and can optimize the tech-task easily and efficiently with computer
programming knowledge
• Objectives
1) To provide exposure to basic problem-solving techniques using computers.
2) To inculcate the art of logical thinking abilities and propose novel solutions for real
world problems through programming language constructs.
• Outcomes
1) Classify various algorithmic approaches, categorize the appropriate data
representation, and demonstrate various control constructs.
2) Choose appropriate programming paradigms.
3) Interpret and handle data using files to propose solution through reusable modules.
4) Idealize the importance of modules and packages.

31-07-2024 2
31-07-2024 3
Programming paradigm (Contd…)

 Paradigm can also be termed as method to solve some problem or do


some task.
 Programming paradigm is an approach to solve problem using some
programming language
 It is a method to solve a problem using tools and techniques that are
available to us following some approach.
 There are lots for programming languages that are known but all of them
need to follow some strategy when they are implemented and this
methodology/strategy is paradigms.
Programming paradigm (Contd…)
Imperative programming paradigm:
 It is one of the oldest programming paradigm.
 It features close relation to machine architecture.
 It is based on Von Neumann architecture.
 It works by changing the program state through assignment statements.
 It performs step by step task by changing state.
 The main focus is on how to achieve the goal.
 The paradigm consist of several statements and after execution of all, the
result is stored.
Advantages: Disadvantage:
 Very simple to implement  Complex problem cannot be solved
 Less efficient and less productive
 It contains loops, variables etc.
 Parallel programming is not possible
Programming paradigm (Contd…)

Examples of Imperative programming paradigm:

C : developed by Dennis Ritchie and Ken Thompson

Fortran : developed by John Backus for IBM

Basic : developed by John G Kemeny and Thomas E Kurtz


Programming paradigm (Contd…)
Procedural programming paradigm
 This paradigm emphasizes on procedure in terms of under lying machine
model.
 It has the ability to reuse the code.

Examples:
Programming paradigm (Contd…)
Object oriented programming
 The program is written as a collection of classes and object which are
meant for communication.
 The smallest and basic entity is object and all kind of computation is
performed on the objects only.
 More emphasis is on data rather procedure.
 It can handle almost all kind of real life problems which are today in
scenario.
Advantages:
 Data security
 Inheritance
 Code reusability
 Flexible and abstraction is also present
Programming paradigm (Contd…)

Examples of Object Oriented programming paradigm:


Programming paradigm (Contd…)

Parallel processing approach:

 Parallel processing is the processing of program instructions by dividing


them among multiple processors.
 A parallel processing system posses many numbers of processor with the
objective of running a program in less time by dividing them.
 This approach seems to be like divide and conquer.

Examples:
NESL (National E-Governance Services Limited) - one of the oldest one and
C/C++ also supports because of some library function.
Programming paradigm (Contd…)

Declarative programming paradigm:

 It is divided as Logic, Functional, Database.


 In computer science the declarative programming is a style of building
programs that expresses logic of computation without talking about its
control flow.
 It often considers programs as theories of some logic.
 The focus is on what needs to be done rather how it should be done
basically emphasize on what code is actually doing.
 It just declares the result we want rather how it has be produced.
Programming paradigm (Contd…)
Logic programming paradigms:

 It can be termed as abstract model of computation.


 It would solve logical problems like puzzles, series etc.
 In logic programming we have a knowledge base which we know before
and along with the question and knowledge base which is given to machine,
it produces result.
 In logical programming the main emphasize is on knowledge base and the
problem.
 The execution of the program is very much like proof of mathematical
statement, e.g., Prolog
Programming paradigm (Contd…)
Functional programming paradigms:
 The functional programming paradigms has its roots in mathematics and
it is language independent.
 The key principle of this paradigms is the execution of series of
mathematical functions.
Some of the languages like perl, javascript mostly uses this paradigm.
Examples:
Programming paradigm (Contd…)
Database/Data driven programming approach
 This programming methodology is based on data and its movement.
 Program statements are defined by data rather than hard-coding a series of
steps.
 A database program is the heart of a business information system and
provides file creation, data entry, update, query and reporting functions.

Several programming languages are developed for database application.


Example SQL.
It is applied to streams of structured data, for filtering, transforming,
aggregating (such as computing statistics), or calling other programs.
Programming paradigm (Contd…)

Example for Database/Data driven programming approach


Course Content
Introduction to Problem
Solving

Python Programming
Control Structures

Collections
Computer
Programming:
Strings and Regular
Python
Functions and Files Expressions

Modules and Packages

References:
1. Eric Matthes, Python Crash Course: A Hands-On, Programming, 2nd Edition, No starch Press, 2019.
2. Martic C Brown, Python: The Complete Reference, 4th Edition, McGraw Hill Publishers, 2018.
3. John V. Guttag, Introduction to computation and programming using python: with applications to understanding data, 2nd
Edition, MIT Press, 2016.
31-07-2024 16
Course Details
• Embedded Theory
Theory component and Laboratory component
• Course Credits

31-07-2024 17
Rubrics Assessment (Evaluation)
Component Max. Marks Weightage Total
Marks (%) Marks
Periodic Assessment Test (PAT) - 3 10 * 3 10 30

Theory - Quiz / Test 20 10 10

Midterm Test 50 20 20
FAT* 50 40 40
Total 100

*schedule of FAT as per the academic calendar from Dean, Academic

31-07-2024 18
What is Python?

 Python is a very popular general-purpose


interpreted, interactive, object-oriented,
and high-level programming language.
 Python isdynamically-typed and
garbage-collectedprogramming language.
 It was created by Guido van Rossum during
1985- 1990.
 Like Perl, Python source code is also
available under the GNU General Public
License (GPL).

31-07-2024 19
Characteristics of Python:
Python is Interpreted − Python is processed at runtime by the interpreter.
You do not need to compile your program before executing it. This is similar
to PERL and PHP.

Python is Interactive − You can actually sit at a Python prompt and interact
with the interpreter directly to write your programs.

Python is Object-Oriented − Python supports Object- Oriented style or


technique of programming that encapsulatescode within objects.
Python is a Beginner's Language − Python is a great language for the
beginner-level programmers and supports the development of a wide range of
applications from simple text processing to WWW browsers to games.
31-07-2024 20
Characteristics of Python:

Easy-to-learn − Python has few keywords, simple structure, and a clearly


defined syntax. This allows the student to pick upthe language quickly.
Easy-to-read − Python code is more clearly defined and visible to the eyes.
Easy-to-maintain − Python's source code is fairly easy-to- maintain.
A broad standard library − Python's bulk of the library is very portable
and cross-platform compatible on UNIX,Windows, and Macintosh.
Interactive Mode − Python has support for an interactive mode which
allows interactive testing and debugging of snippets of code.

31-07-2024 21
Characteristics of Python:
Portable − Python can run on a wide variety of hardware platforms and has the
same interface on all platforms.
Extendable − You can add low-level modules to the Python interpreter. These
modules enable programmers to add to or customize their tools to be more
efficient.
Databases − Python provides interfaces to all major commercial databases.
GUI Programming − Python supports GUI applications that can be created
and ported to many system calls, libraries andwindows systems, such as
Windows MFC, Macintosh, and the XWindow system of Unix.
Scalable − Python provides a better structure and support for large programs
than shell scripting.
31-07-2024 22
Way to Run Python:

 IDLE (Integrated Development and Learning Environment)


 Sublime Text 3
 Atom
 Thonny
 PyCharm
 Visual Studio Code
 Vim
 Spyder

31-07-2024 23
Introduction to Problem Solving

Have you ever observed this scenario?


What are the problems in the scenario?
31-07-2024 24
Introduction to Problem Solving
• Problem is a set of questions on a scenario which consists of description of
reality and set of constraints about the scenario and that requires logical
thought and /or mathematics to solve.
• All Problems do not have a straightforward solutions.
• Some problems, such as
• balancing a checkbook or
• baking a cake,
-can be solved with a series of actions.
-These solutions are called algorithmic solutions.

• There may be more than one solution for a problem


• Identify all possible ways to solve a problem and choose one among them
31-07-2024 25
Introduction to Problem Solving
• The solutions of other problems, such as
• how to buy the best stock or
• whether to expand the company
are not so straightforward

• These solutions require reasoning, built in knowledge and experience, and a


process of trial and error.

• Solutions that cannot be reached through a direct set of steps are called
heuristic solution

31-07-2024 26
Introduction to Problem Solving
• Computers are built to solve problems with algorithmic solutions, which are
often difficult or very time consuming when input is large

• Solving a complicated calculus problem or alphabetizing 10,000 names is an


easy task for the computer

• So the basis for solving any problem through computers is by developing an


algorithm

31-07-2024 27
Introduction to Problem Solving
• Field of computers that deals with heuristic types of problems is called
Artificial Intelligence (AI)
• Artificial intelligence enables a computer to do things like human by building
its own knowledge bank
• As a result, the computer’s problem-solving abilities are similar to those of a
human being.
• Artificial intelligence is an expanding computer field, especially with the
increased use of Robotics.
Computational Problems:
• Computation is the process of evolution from one state to another in
accordance with some rules.

31-07-2024 28
Introduction to Problem Solving
• Where the answer for every instance is either yes or No
Decision • Deciding whether the given number is prime
Problems

• Searching an element from a given set of elements or arranging them in an order


Searching • Finding product name for given product ID and arranging products in alphabetical order
and Sorting of names
Problems

• Counting number of occurrences of a type of element in a set of elements


Counting • Counting how many different type of items are available in the store
Problem

• Finding the best solution out of several feasible solutions


Optimization • Finding best combination of products for promotional campaign
Problem

31-07-2024 29
Introduction to Problem Solving - steps
Defining / Understanding a Problem

Identifying Input / Output of the problem

Analyse alternate ways to solve the problem

Flow Chart/Pseudo Code

Debugging to fix bug if any

31-07-2024 Testing 30
Introduction to Problem Solving- Logic (Basis for solving
any problem)
• Definition : A method of human thought that involves thinking in a linear,
step by step manner about how a problem can be solved
• Logic is a language for reasoning.
It is a collection of rules we use when doing reasoning.
• Example: John's mum has four children.
• The first child is called April.
• The second May.
• The third June.
• What is the name of the fourth child?

31-07-2024 31
Problem Solving by Computer

• Solving problem by computer undergo two phases:

• Phase 1:
• Organizing the problem or pre-programming phase
(Problem analysis diagram, flow-chart, algorithm)

• Phase 2:
• Programming phase (coding the program)

31-07-2024 32
Problem Solving by Computer
Organizing the problem or pre-programming phase – tools

Analyzing the problem – Problem Analysis Chart

Developing the Hierarchy Input Process Output (HIPO) chart

Developing the Input Process Output chart

Drawing the program flowcharts

Writing the algorithms

31-07-2024 33
Problem Solving by Computer- PAC
Analyzing the problem – Problem Analysis Chart/Diagram

PAC is used as basis in establishing method of coding, testing, and data type description

Understand and analyze the problem to determine whether it can be solved by a computer.

• Analyze the requirements of the problem.

• Identify the following:


Data requirement. (DATA)
Processing requirement or procedures that will be needed to solve the problem.
(PROCESS)
The output. (INFORMATION)

31-07-2024 34
Problem Solving -PAC
Output Solution
Data Processing Alternatives

given in List of
the List of ideas for
problem processing Output the
required requirement solution of
or
or the
provided problem
by the user procedures

31-07-2024 35
PAC – example1
• Payroll Problem
Calculate the salary of an employee who works by hourly basis. The formula to be used is
Salary = Hour works * Pay rate

Solution
Data Processing Output
Alternatives
1. Define the hours
worked
Hours Salary = and pay rate as
work, constants.
Hours work * Salary 2. Define the hours
Pay rate pay rate worked
and pay rate as input
values.

31-07-2024 36
PAC – Example2
• Write a Problem Analysis Chart (PAC) to convert the distance in
miles to kilometers where 1.609 kilometers per mile.

Alternate
Alternate Alternate
Alternate
Data Processing Output Solutions Solutions
Solutions Solutions
1. Define the the
1. Define 11.. Define
Define the
the
miles as as
miles miles
miles as
as
Distance in Kilometers = Distance in constants.
constants. constants.
constants.
miles 1.609 x kilometers ∗2.
∗2. ∗2.
Define the the
Define ∗2. Define
Define the
the
miles miles as input
miles as input miles
miles as
as input
input
values.
values. values.
values.
31-07-2024 37
PAC – example3
• Write a Problem Analysis Chart (PAC) to find an area of a circle
where area = pi * radius * radius

Data Processing
Processing Output
Output

area = 3.14 x
radius radius x area
radius

31-07-2024 38
Problem Solving by Computer- HIPO

• Developing the Hierarchy Input Process Output (HIPO) or


Interactivity Chart
When problem is normally big and complex.
Processing can be divided into subtasks called modules
Each module accomplishes one function
These modules are connected to each other to show the interaction of
processing between the modules
Main/control module controls the flow of all other modules

31-07-2024 39
Problem Solving by Computer- HIPO
• Programming which use this approach (problem is divided into
subtasks) is called Structured Programming

Main
Module

Module-1 Module-2

Module 4 Module 5 Module 6

31-07-2024 40
Problem Solving by Computer- HIPO
• HIPO chart for Payroll problem

PAYROLL

READ CALCULATE PRINT

31-07-2024 41
Problem Solving by Computer- HIPO
Extended Payroll problem
• Write a program to calculate both the gross pay and the net pay of
every employee of your company. Use the following formulae for
calculation:
Gross pay = number of hours worked * pay rate
Net pay = gross pay – deductions
• The program should also print the cheque that tells the total net pay.

31-07-2024 42
Problem Solving by Computer- HIPO
Extended Payroll problem -PAC

Input Processing Output

Number of Gross pay = number Net pay and


hours worked, of hours * pay rate write net pay
pay rate, in cheque
deductions Net pay = Gross pay
– deductions

31-07-2024 43
Problem Solving by Computer- HIPO
Extended Payroll problem -HIPO

Payroll

Calculate Calculate Write


Gross Pay Net Pay Cheque

Get
Get Calculate
Hourly
Pay rate Deductions
Worked

31-07-2024 44
Developing an Algorithm
• Algorithm is a step-by-step procedure, which defines a set of instructions to
be executed in a certain order to get the desired output.

• Algorithms are generally created independent of underlying languages, i.e.


an algorithm can be implemented in more than one programming language.

• Not all procedures can be called an algorithm.

31-07-2024 45
Developing an Algorithm cont…
• An algorithm should have the following characteristics −
1. Unambiguous − Algorithm should be clear and unambiguous. Each of its
steps (or phases), and their inputs/outputs should be clear and must lead to
only one meaning.
2. Input − An algorithm should have 0 or more well-defined inputs.
3. Output − An algorithm should have 1 or more well-defined outputs, and
should match the desired output.
4. Finiteness − Algorithms must terminate after a finite number of steps.
5. Feasibility − Should be feasible with the available resources.
6. Independent − An algorithm should have step-by-step directions, which
should be independent of any programming code.
31-07-2024 46
Developing an Algorithm cont…
How to Write an Algorithm?
• There are no well-defined standards for writing algorithms.
• Rather, it is problem and resource dependent.
• Algorithms are never written to support a particular programming code.
• As we know that all programming languages share basic code constructs like
loops (do, for, while), flow-control (if-else), etc.
• These common constructs can be used to write an algorithm.
• Sometimes algorithm writing is a process and is executed after the problem
domain is well-defined.
• That is, we should know the problem domain, for which we are designing a
solution.

31-07-2024 47
Developing an Algorithm cont…
• Example
• Problem − Design an algorithm to add two numbers and display the result.
• step 1 − START
• step 2 − declare three integers a, b & c
• step 3 − define values of a & b
• step 4 − add values of a & b
• step 5 − store output of step 4 to c
• step 6 − print c
• step 7 − STOP

31-07-2024 48
Developing an Algorithm cont…
• Algorithms tell the programmers how to code the program.
• Alternatively, the algorithm can be written as −
• step 1 − START ADD
• step 2 − get values of a & b
• step 3 − c ← a + b
• step 4 − display c
• step 5 − STOP
• In design and analysis of algorithms, usually the second method is used to
describe an algorithm.
• It makes it easy for the analyst to analyze the algorithm ignoring all unwanted
definitions.

31-07-2024 49
Developing an Algorithm cont…
• He can observe what operations are being used and how the process is
flowing.
• Writing step numbers, is optional.
• We design an algorithm to get a solution of a given problem.
• A problem can be solved in more than one ways.

31-07-2024 50
Flowcharts
• Flowcharts graphically represent the flow of a program.
• There are four basic shapes used in a flow chart.

• Each shape has a specific use:

• oval: start / end


• parallelogram: input / output
• rectangle: calculations/operations
• diamond: selection structures

31-07-2024 51
Flowcharts cont…
Symbol Symbol Name Purpose
Used at the beginning and end of the
Start/Stop algorithm to show start and end of the
program.
Indicates processes like mathematical
Process
operations.
Used for denoting program inputs and
Input/Output
outputs.
Stands for decision statements in a
Decision program, where answer is usually Yes or
No.
31-07-2024 52
Flowcharts cont…
Symbol Symbol Name Purpose
Shows relationships between different
Arrow
shapes.
Connects two or more parts of a
On-page
flowchart, which are on the same
Connector
page.
Off-page Connects two parts of a flowchart
Connector which are spread over different pages.

31-07-2024 53
Flowcharts cont…
• Guidelines for Developing Flowcharts

• Flowchart can have only one start and one stop symbol
• On-page connectors are referenced using numbers
• Off-page connectors are referenced using alphabets
• General flow of processes is top to bottom or left to right
• Arrows should not cross each other.

31-07-2024 54
Flowcharts cont…
• Advantages of Flowchart:
• Flowcharts are a better way of communicating the logic of the system.
• Flowcharts act as a guide for blueprint during program designed.
• Flowcharts help in debugging process.
• With the help of flowcharts, programs can be easily analyzed.
• It provides better documentation.
• Easy to trace errors in the software.
• Easy to understand.
• The flowchart can be reused for inconvenience in the future.
• It helps to provide correct logic.
31-07-2024 55
Flowcharts cont…
• Disadvantages of Flowchart:
• It is difficult to draw flowcharts for large and complex programs.
• There is no standard to determine the amount of detail.
• Difficult to reproduce the flowcharts.
• It is very difficult to modify the Flowchart.
• Making a flowchart is costly.
• If changes are done in software, then the flowchart must be redrawn.

31-07-2024 56
Flowcharts cont…
• Arrows connect the basic shapes in a flowchart.
• The shapes and arrows of a flowchart describe the flow of a program from
start to end.
• Flowcharts typically flow from the top to the bottom or flow from the left to
the right.

• Below is the description of a simple program:


• The program starts.
• Then the program prints out "Output!".
• Finally, the program ends.

31-07-2024 57
Flowcharts cont…
• A flowchart that describes this simple program is shown.
• The Python code that corresponds to this flowchart is:
# start
print("Output!")
# end

31-07-2024 58
Flowcharts cont…
• A description of a program that includes a calculation is below:

The program starts.


Next, the program asks a user for a number.
Two is added to the number.
Next, the resulting sum is printed.
Finally, the program ends.

31-07-2024 59
Flowcharts cont…
• The Python code that corresponds to this flow chart is:
# start
num = input("Enter a number: ")
num = float(num)
num_plus_2 = num + 2
print(num_plus_2)
# end

31-07-2024 60
Flowcharts cont…

31-07-2024 61
Flowcharts cont…

31-07-2024 62
Flowcharts cont…

31-07-2024 63
Flowcharts cont…

31-07-2024 64
Flowcharts cont…

31-07-2024 65
Flowcharts cont…

31-07-2024 66
Pseudocode
• Pseudocode is a method of planning which enables the programmer to plan
without worrying about syntax.
• It’s simply an implementation of an algorithm in the form of annotations and
informative text written in plain English.
• It has no syntax like any of the programming language and thus can’t be compiled
or interpreted by the computer.
• Pseudocode, as the name suggests, is a false code or a representation of code which
can be understood by even a layman with some school level programming
knowledge.
• Pseudocode is a term which is often used in programming and algorithm based
fields.
• It is a methodology that allows the programmer to represent the implementation of
an algorithm.

31-07-2024 67
Pseudocode cont…
• Advantages of Pseudocode
• Improves the readability of any approach.
• It’s one of the best approaches to start implementation of an algorithm.
• Acts as a bridge between the program and the algorithm or flowchart.
• Also works as a rough documentation, so the program of one developer can
be understood easily when a pseudo code is written out.
• In industries, the approach of documentation is essential. And that’s where a
pseudo-code proves vital.
• The main goal of a pseudo code is to explain what exactly each line of a
program should do, hence making the code construction phase easier for the
programmer.
31-07-2024 68
Pseudocode cont…
• Five major steps in Python pseudocode
• The pseudocode in python must be line by line so every statement involved
must be represented as just one single line in pseudocode.
• Just as in python code how indentations are used, these indentations must be
preferred in python pseudocode too.
• Ensure that each statement of the pseudocode is simple and easy to
understand.
• Ensure the first word of the pseudocode is always in Upper case letters.

31-07-2024 69
Pseudocode cont…
• Special Keyword:
• START: To begin the pseudocode.
• INPUT: Take input from the user.
• PRINT: To print the output on the screen.
• READ/GET: Input format while reading data from the file.
• SET, INIT: Initialize a value.
• INCREMENT, BUMP: Increase the value of the variable, equivalent to a++.
• DECREMENT: Decrease the value of the variable, equivalent to a--.
• COMPUTE, CALCULATE, DETERMINE: To calculate the expression result.

31-07-2024 70
Pseudocode cont…
• Write the FizzBuzz Algorithm Using pseudocode
• FizzBuzz is a standard coding, in which we write a code to print Fizz, Buzz, and
FizzBuzz when the multiple of 3 and 5 occurs.
• The problem states:
• Write a code that prints each number from 1 to 30 in a new line.
• Print "Fizz" if the number is the multiple of 3.
• Print "Buzz" if the number is a multiple of 5.
• For a number that is multiple of both 3 and 5 print “FizzBuzz.”

31-07-2024 71
Pseudocode cont…
• FizzBuzz Pseudocode
num : 1
FOR num -> 1 to 20
IF num MOD 15 ==0
PRINT “FizzBuzz”
ELSE IF num MOD 3 ==0
PRINT “Fizz”
ELSE IF num MOD 5==0
PRINT “Buzz”
ELSE PRINT num
ENDIF
ENDFOR
31-07-2024 72
Pseudocode cont…
• Equivalent Python Code:
for num in range(1, 21):
if num % 15 ==0:
print("FizzBuzz")
elif num %3 ==0:
print("Fizz")
elif num %5==0:
print("Buzz")
else:
print(num)
31-07-2024 73
Pseudocode cont…
• Python pseudocode Examples
• Retrieve the number to be reversed from the user into variable
sample_number.
• Initialize the temporary variable test_number as zero.
• Perform a while loop until the sample_number variable is greater than zero.
• Modulus the sample_number variable by 10 and store the remainder.
• Multiply the temporary number of test_number by 10 and add the returned
value to the remainder.
• Print the generated test_number onto the console.

31-07-2024 74
Pseudocode cont…
• Pseudocode:
sample_number = int(input("Number to be reversed: "))
test_number = 0
while(sample_number>0):
remainder_number = sample_number % 10
test_number = (test_number * 10) + remainder_number
sample_number = sample_number//10
print("Value after reverse : {}".format(test_number))
31-07-2024 75
Pseudocode cont…
• WRITE A PSEUDOCODE TO FIND THE LARGEST OF TWO NUMBERS.
BEGIN
NUMERIC nNum1,nNum2
DISPLAY "ENTER THE FIRST NUMBER : "
INPUT nNum1
DISPLAY "ENTER THE SECOND NUMBER : "
INPUT nNum2
IF nNum1 > nNum2
DISPLAY nNum1 + " is larger than "+ nNum2
ELSE
DISPLAY nNum2 + " is larger than " + nNum1
END

31-07-2024 76
Pseudocode cont…
• WRITE A PSEUDOCODE TO FIND THE SUM OF TWO NUMBERS.
begin
numeric nNum1,nNum2,nSum
display "ENTER THE FIRST NUMBER : "
accept nNum1
display "ENTER THE SECOND NUMBER : "
accept nNum2
compute nSum=nNum1+nNum2
display "SUM OF THESE NUMBER : " nSum
end
31-07-2024 77
Pseudocode cont…
• WRITE A PSEUDOCODE TO FIND THE AREA OF RECTANGLE.
begin
numeric nLen,nBrd,nAre
display "ENTER THE LENGTH OF RECTANGLE : "
accept nLen
display "ENTER THE BREADTH OF RECTANGLE : "
accept nBrd
nAre=nLen*nBrd
display "AREA OF RECTANGLE : " nAre
end
31-07-2024 78
Pseudocode cont…
• WRITE A PSEUDOCODE TO FIND THE GREATEST OF TWO NUMBERS.
begin
numeric nNum1, nNum2
display "ENTER THE FIRST NUMBER : "
accept nNum1
display "ENTER THE SECOND NUMBER : "
accept nNum2
if(nNum1>nNum2)
begin
display "GREATEST ONE : " nNum1
end
else
begin
display "GREATEST ONE : " nNum2
end
end

31-07-2024 79
Conclusion

• To design an algorithm you can draw a flowchart or write pseudo-


code.
• Your algorithm (flowchart or pseudo-code) can then be converted by
a programmer using the programming language of their choice (e.g.
Python, Java, Visual Basic, etc.).

31-07-2024 80

You might also like