Algorithm Vs Program

Distribute Education like Computer Geek

Algorithm

An algorithm is a set of finite, step-by-step rules or instructions used to convert input into output.
It only gives logic to solve the problem.
It is simple, clear, and independent of any programming language.

.

Program

A computer program is a collection of instructions written in a programming language that performs a specific task when executed by a computer.
A program must follow the syntax and rules of the programming language.

.

But my point is not about defining the word.

My point is to tell you what an algorithm can do but a program cannot and what a program can do but an algorithm cannot.

To solve a problem, first we design an algorithm, and then we implement that algorithm in any programming language.

But there are some problems where algorithm can exist, but a perfect program cannot exist.

.

Example – Square Root Problem

Ques – Write a program to compute the square root of any number.

You can easily make an algorithm for square root. But you cannot make a program that gives the exact square root for every input.

In an algorithm, you only need logic.
But in a program, output must be exact according to computer calculation.

Example

√4 = 2
√9 = 3
√16 = 4
√25 = 5

These are perfect square numbers.

But what about

√2, √3, √5, √6, √7 & √8

These are not perfect squares.

The computer cannot give exact value. It can only give approximation.

Example

√2 ≈ 1.414213562…

No matter how many digits you write, it will never end. So the algorithm can describe the logic of square root,
but the program cannot give exact output for every input.

External reference → https://en.wikipedia.org/wiki/Irrational_number

.

Greek Philosopher Example – Hippasus

There are some assumptions that √2 = 1.414, But Hippasus, a Greek philosopher, proved that √2 is an irrational number.

Hippasus - A greek philosopher - Algorithm Vs Program
Hippasus – A Greek Philosopher

.

That means it cannot be written as a ratio of two whole numbers.

You can keep adding digits 1.414213562373095… but the answer will never be perfect.

So a computer program cannot give exact value. But an algorithm can explain the logic of finding square root.

This shows
1. Algorithm is more powerful in thinking
2. Program is limited by computation

.

Example – Friendship Algorithm

This example is for students who believe in friendship.

Suppose you like someone and want to become friends, but you do not know how to talk. So you ask your best friend.

Your friend gives you steps –

Go and say hello
Talk politely
Find common interest
Help when needed
Be honest

This is an algorithm. It gives logic, but making a program is different.

Program means success must be guaranteed. But in real life

50% chance friend accepts
50% chance friend rejects

So algorithm exists, but program cannot guarantee output.

This shows that algorithm gives idea but Program needs exact result

Test Yourself

Q1- Define Computer Program?

A computer program is a collection of instructions that performs a specific task when executed by a computer. It follows the syntax of the programming language.

Q2- Can cube root program gives accurate output on every input?

Perfect cube root numbers can give accurate output but every input can’t give accurate results. 

Q3- Can cube root algorithm give accurate logic on every input?

There is a domain of input which will pass in the algorithm and algorithm instruction will give accurate logic.

 

Q4- An algorithm becomes a ________ when it is written in the form of a programming language.

Program

Q5- Is it possible for a program to evolve into an algorithm? 

Expert programmers can do that. There is a certain way you have to follow to make an algorithm from a program.
1. Write input domain
2. Write all the test cases
3. Note every output.
4. Break the code into modules.
5. Write algorithms for the modules.
6. Find integration of the module’s algorithm.
7. Dry run the program’s algorithm.

Q6- The method that uses a list of well-defined instructions to complete a task starting from a given initial state to an end state is known as

Algorithm

Q7- Algorithms are used to
1. better planning.
2. Better programming
3. Easier testing and debugging

 

All of the above

Q8- What is the pictorial representation of algorithm?

Flowchart

Q9- Pseudocode chooses to focus on
1. Development
2. Design
3. Coding

 

Design

Q10- The pseudocode instructions are written in the order in which they will be executed is _________.

 

Sequence

BOOKS

Algorithm T H CoremanAlgorithm by Udit AgarwalAlgorithm Ellis HorowitzData Structure & Algorithm Made Easy Narasimha