Algorithms and programming logic

Pybeginner
By -
4 minute read
0


Algorithm


What is an algorithm in programming?

It is a step-by-step procedure for solving a problem.

A sequential solution to any problem written in natural language.

Using the algorithm, the programmer records the actual program.

 

What is the purpose of using algorithms?

The main use of the algorithm is to help us translate English (or another language) into the programming language. It is a basic or logical outline or structure of the problem.

 

Algorithm characteristics

Each and every instruction must be precise and unambiguous.

Each instruction's execution time must be terminated.

There must be a termination condition.

It must accept 0 or more inputs and produce a mandatory output.

It can accept any type of input and produce a corresponding output.

 

General way of writing the algorithm

The name of the algorithm must be specified.

Algorithm start must be specified as Start

Input and output descriptions can be included.

The step number must be included for identification.

Each step can have an explanatory note enclosed in square brackets, followed by an operation.

Algorithm completion must be specified as Final or Stopped.

 

Algorithm Representation Methods

There are two ways to represent algorithms: 

Flowchart Graphic representation

Pseudocode (Structured Portuguese) Textual representation. 

The pseudocode, the description will depend on the language in which the algorithm is being written.


Pseudocode (a solution to the problem)

 

Definition

It is a series of steps to solve a given problem written using a mixture of English and programming languages ​​(C, Java, Python, Visualg, etc..).

It acts as a troubleshooting tool.

It's the first step in writing a program.

 

Objective:

It is to express the solution to a given problem using a mixture of English language and programming language (C, Java, Python, etc..). as code.

 

Advantage

Easy to write and understand

It is relatively easy to convert the description solution in English (or any other programming language) from small programs to program (C, Java, Python, etc..).

 

Example-1: Addition of two numbers

1. Get the numbers [a, b]
2. Calculate addition [Sum = a + b]
3. Print the results [Sum]


Example-2: Area of ​​Circle

1. Get radius [r]
2. Compute area [Area = 3141 * r * r]
3. Print results [Area]

 

Disadvantage

It is very difficult to translate the solution of the long and complex problem into English for programming languages (C, Java, Python, etc..).

 

Flowcharts

 

A flowchart is a graphical representation of an algorithm or program.

It consists of sequences of instructions executed in an algorithm.

It is mainly used to help the programmer understand program logic.

 

Notations used in flowcharts

 

Flowcharts use special shapes to represent different types of actions or steps in a process. Lines and arrows show the sequence of steps and the relationships between them. These are known as flowchart symbols.

 






Example: pseudocode and flowchart to enter the dimensions of a rectangle and print its area.

pseudocode 

Step 1: Start
Step 2: [enter length and width values]
               read length, width
Step 3: [Calculate 'area']
                area = length * width
Step 4: display area
Step 5: stop

 

Algorithmic Exercises:

 

Exercise # 01 - Write an algorithm for adding two numbers in Python

Algorithm

STEP 1: Start
STEP 2: Accept the first number a.
STEP 3: Accept the second number b.
STEP 4: Add a and b and store in SUM.
STEP 5: Display the SUM value.
STEP 6. Stop


flowchart

Exercise #02 - Write an algorithm to print the largest number between two numbers in Python

 

Algorithm

STEP 1: Start
STEP 2: Read the two numbers a and b.
STEP 3: Compare a and b.
If a is greater than b, print a, otherwise, print b.
STEP 4: Stop

 

flowchart




Exercise #03 - Write an algorithm to print the largest number out of three numbers in Python

 

Algorithm

STEP 1: GET STARTED
STEP 2: Read three numbers and store them in A, B, C
STEP 3: It's A> B
{Yes: Go to Step 6, No: Go to Step 4}
STEP 4: It's B> C
{Yes: printing B is better, no: go to step 5}
STEP 5: Printing C is better and go go to step 8}
STEP 6: It is A> C
{Yes: printing A is better, No: go to step 7}
STEP 7: Printing C is better and go to Step 8
STEP 8: Stop

 

flowchart




Exercise #04 - Write an algorithm to print the first ten natural numbers in Python

 

Algorithm

STEP 1: Get started.
STEP 2: Initialize the variable count to one.
STEP 3: Display the variable count.
STEP 4: Increase the variable count by one.
STEP 5: Check that the count variable exceeds 10.
If yes, go to step 3, if not go to step 6
STEP 6: Stop

 

flowchart




Exercise #05 - Write an algorithm to print Circle Area in Python

 

Algorithm

STEP 1 : Start
STEP 2: Read the 'r' ray.
STEP 3: Store 3.14 worth of pi.
STEP 4: Find the area of ​​the circle (Area = pi * r * r)
STEP 5: Display area.
STEP 6: Stop

 

flowchart




Exercise #06 - Write an Algorithm to Calculate Simple Interest in Python


Algorithm

STEP 1: Begin
STEP 2: Read Principal, Time and Rate (P, T, R)
STEP 3: Calculate Simple Interest (JS = P * T * R / 100)
STEP 4: Display JS
STEP 5: Stop

 

flowchart





 

Exercise #07 - Write an algorithm to print out whether a number is Positive, Negative or Zero in Python.

 

Algorithm

Step 1Read number.
Step 2If the number is less than zero, then it is a negative integer.
Step 3Else if the number is greater than zero, then it is a positive integer.
Step 4Else, the number is equal to zero.
Step 3: End.

 

flowchart




Post a Comment

0Comments

Post a Comment (0)