Skip to main content

Introduction to Algorithms and Algorithmic Notation With Examples

Topic: Introduction to Algorithm and Algorithmic Notation With Examples

What is an Algorithm?


The step by step procedure to solve a particular problem is called an Algorithm.  A Good Programmer always writes algorithms before developing programs.

Algorithmic Notation with example algorithms 2020
Algorithmic Notation with example algorithms 


Advantages of  writing an Algorithm


  • The process of solving a problem becomes simpler and easier with the help of  algorithm.
  • There are less chance of errors in the program, if we design algorithms before writing our programs in any programming language.
  • We can convert an algorithm into a program of any programming language.
  • It is not dependent on any programming language. Therefore it is easy to understand for anyone even without programming knowledge.

What is the Relationship between an Algorithm and a Program


Sponsored Links

An algorithm is the sequenc of steps to perform a task, whereas a program is the set of statements to perform a specific task. Normally, we will use a programming statement (instruction of a programming language) for each step of an algorithm.

For example, consider the following four steps of an algorithm of a program to find square of a number:
  1. Print "Enter a number:"
  2. Input a number in n.
  3. Calculate square = n * n
  4. Print "Square=",square
Now we will write the program instructions for the above steps of the algorithm:

1. printf("Enter a number:");
2. scanf("%d",n);
3. square = n * n;
4. printf("Square=%d",square); 

Introduction to Algorithmic Notation

The algorithms are written using an algorithmic noataion. Normally there is no standard algorithmic otation. Every author uses his own set of algorithmic statements to design the algorithms.
Introduction to Algorithms and Algorithmic Notation With Examples


For example, we will use the following set of statements as algorithmic notation to design the algorithms:

The Name of Algorithm

First of  all we will write the name of the algorithm for example:
Algorithm square.

The Description of Algorithm

Here we will describe the purpose of the algorithm.
For example:
// This algorithm is used to input a number and display its square.

The Comments

The comments are used to explain the purpose of  a step of an algorithm. For example:
// Compute square of the number n
   square = n * n
We will use double slash // to start a single line comment.

The Variables

A variable is an entity whose value can be changed. It is used to store data. We do not need to declare variables. We simply use variables in an algorithm according to the requirements. We will use simple variable names in small letters. For example, n or num1, num2, square, factorial, first_name etc.

The Statements

There are differnt statements in an algorithmic notation to perform differnt tasks.

Input Statement

Inmput statement is used to input data into different variables.
For example:
Input a number in n.
Or more precisely
Input n
Input radius of circle in rad.
Note: rad is a variable to store the value of the radius of the circle.
Or more precisely,
Input rad

Output statement

The output statement is used to display string messages and the values of variables:
For example:
Print "Enter a number"
or
Print "Sum=",sum

Assignment statement

Assignment statement is used to assign a value to a variable or to calculate a formula. For example,
i = 1
or
sum = num1 + num2

If statement

If statement is used to decide the execution of a statement or a set of statements if the given condition is true.
For example:
If (num1 == num2)
   Print "both numbers are equal"
End If

If Else Statement

If else statement is a two way selection. If condition is true, the if block is executed otherwise, the else block of statements is executed:
If (num1 ==  num2)
   Print "both numbers are equal"
Else
   Print "numbers are different"
End If

Repeat - For

A Repeat - for construct is a looping statement. It is used to execute a block of one or more statements repeatedly for a fixed number of times. For example: the following for statement print 1 to 5 numbers.
Step 1. Repeat steps 1.1 For i = 1 to 5 step 1
                    Step 1.1 Print i
            End For

Repeat - While

A Repeat - While statement is used to execute a block of one or more statements as long as the given condition remains true. For example: Input a number, and sum it as long as we enter a number gretare than zero.


Step 1. sum = 0

Step 2. Print "Enter a number:"

Step 3. Input n

Step 3. Repeat Step 3.1,3.2,3.3 While ( n > 0)

               Step 3.1  sum = sum + n

               Step 3.2  Print "Enter a number:"

               Step 3.3  Input n

End While

Step 4. Print "Sum=",sum

Algorithm to Add Two Numbers

Algorithm Sum
//This algorithm inputs two numbers and display their sum.
// It uses three integer variables num1, num2 and sum.
Step 1.  Start
Step 2.  Print "Enter two numbers to Add="
Step 3.  Input num1, num2
step  4.  Calculate sum = num1 + num2
Step 5.  Print "Sum=", sum
Step 6.  End 


Algorithm to Check whether Two Numbers are Equal

Algorithm Check_Equal
//This algorithm inputs two numbers and display if they are equal or not
// It uses two integer variables num1, num2
Step 1.  Start
Step 2.  Print "Enter two numbers to Add="
Step 3.  Input num1, num2
step  4.  If ( num1 == num2 )
                 Print "Both numbers are equal"
            Else
                 Print "Both numbers are different"
            End If
Step 5. End 

Algorithm To Find Factorial of a Number

Algorithm Factorial
//This algorithm inputs a number and displays its factorial using Repeat-While
Step 1. Start
Step 2. Print "Enter a number:"
Step 3. Input n
Step 4. Set fact = 1, i = 1
Step 5. Repeat Step 5.1 To 5.2 While (i<=n)
               Step 5.1   Set fact = fact * i
               Step 5.2   Set i = i + 1
            End While
Step 6. Print "Factorial =", fact
Step 7. End

Algorithms, Algorithmic Notation and Problem Solving

We have discussed the following concepts in this Algorithms and Algorithmic Notation tutorial:
Algorithms, Importance of Algorithm, Notation used to describe algorithms and some important examples of algorithms.

Comments

Popular posts from this blog

What are Dual Devices or Both Input/ Output Devices

The devices that can be used to perform both functions -input and output- are called Dual Devices. These devices are also called Dual Purpose Devices or Both Input/Output Devices. With the help of a dual purpose device, we can enter data into computer as well as we can output data from the computer to outside world.  For example, Dual devices include:  Touch Screen Monitor, Modem, Network Interface Card, Sound card, Hard disk drive, Floppy disk Drive, Magnetic Tape Drive CD-Writer and DVD-Writer etc. These are also called Input/output devices, because they perform both functions. Touch Screen Monitor Dual Devices or Both Input / Output Devices - Touch screens Touch Screen Monitor is an input/output device. It uses a special touch sensitive screen. The User can enter data by touching icons or menus on the screen. As soon as the user selects a command from menu, output is displayed on screen. Commonly touch screen monitors use sensors to detect touch of finger. ...

Using Simple Formulas in Excel With Arithmetic Operators-2

What are Arithmetic Operators in Excel? Arithmetic Operators are used to perform arithmetic operations like addition, subtraction, multiplication and division etc. There are following arithmetic operators in Microsoft Excel: Using Arithmetic / Mathematical Operators in MS Excel Formulas 1.  + Operator The + arithmetic operator is used to perform addition of numbers. It is used with in formulas along with Cell References normally. For example, to add the number 10 in A1 cell and the number 20 in B1 cell, we can write a formula =A1+B1 in cell C1. After pressing Enter key we will see the answer 30 in the cell C1. Important Points To Write a Simple Formula in Excel Every formula in Microsoft Excel is started with an Equal sign ( = ). Arithmetic operators (+ for addition, - for subtraction, * for multiplication, / for division) are used. Percentage is calculated by % operator. ( =B2*10%       means 10% of the number in cell B2) Pow...

Electricity Bill Calculator Formulas in Excel

Today, we will discuss How to create an Electricity Bill Calculator Worksheet by using Electricity Bill Calculator Formulas in Excel . The following formulas will be used in Electricity Bill Calculator Worksheet: Q: Prepare a worksheet according to the following instructions: Create a worksheet in MS Excel according to the requirements Enter sample data of Electricity units consumed by customers in a city Apply formula to calculate Units Consumed Apply formula to calculate Electricity Charges Apply formula to calculate Surcharge Apply formula to calculate Bill Payable Note: Every formula will start from  =  Units Consumed Formula=D5-C5 Electricity Charges Formula=IF(E5>200, E5*15,IF(E5>100, 1000+(E5-100)*12, E5*10)) SURCHARGE FORMULA = IF(F5>5000, F5*5%,IF(F5>1000, F5*3%, 0)) BILL PAYABLE FORMULA = SUM(F5:G5) Download Excel Electricity Bill Calculation Worksheet Free  Image for Electricity Bill Calculator Formulas ...