Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

1

Unit- 1 Introduction: The role Algorithm in computing:


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.

From the data structure point of view, following are some important categories of algorithms:
Search − Algorithm to search an item in a data structure.
Sort − Algorithm to sort items in a certain order.
Insert − Algorithm to insert item in a data structure.
Update − Algorithm to update an existing item in a data structure.
Delete − Algorithm to delete an existing item from a data structure.

The role Algorithm in computing:


 It depends on how efficient the algorithm when higher order of inputs is given.
 The possible restrictions/ constraints on the values.
 The architecture of the computer and the kind of storage devices to be used.
 Another important aspect is the correctness of the algorithm implying that
algorithm is correct if, for every instance, it produces correct output.
 An incorrect algorithm might not halt at all on some input instances, or give in
correct output.

Algorithm as Technology:
An algorithm is defined as a finite set of instructions that if followed, accomplishes a
particular task.
Suppose computers were infinitely fast and computer memory was free. Would you have any reason
to study algorithms? The answer is yes, if for no other reason than that you would still like to
demonstrate that your solution method terminates and does so with the correct answer.

If computers were infinitely fast, any correct method for solving a problem would do. You would
probably want your implementation to be within the bounds of good software engineering practice
(i.e., well designed and documented), but you would most often use whichever method was the
easiest to implement.

Of course, computers may be fast, but they are not infinitely fast. And memory may be cheap, but it is
not free. Computing time is therefore a bounded resource, and so is space in memory. These
resources should be used wisely, and algorithms that are efficient in terms of time or space will help
you do so.

Efficiency

Algorithms devised to solve the same problem often differ dramatically in their efficiency. These
differences can be much more significant than differences due to hardware and software.

The first, known as insertion sort, takes time roughly equal to c1n2 to sort n items, where c1 is
a constant that does not depend on n. That is, it takes time roughly proportional to n2. The
second, merge sort, takes time roughly equal to c2n lg n, where lg n stands for log2 n and c2 is
another constant that also does not depend on n. Insertion sort usually has a smaller constant
factor than merge sort, so that c1 < c2. We shall see that the constant factors can be far less
significant in the running time than the dependence on the input size n. Where merge sort has
2

a factor of lg n in its running time, insertion sort has a factor of n, which is much larger.
Although insertion sort is usually faster than merge sort for small input sizes, once the input
size n becomes large enough, merge sort's advantage of lg n vs. n will more than compensate
for the difference in constant factors. No matter how much smaller c1 is than c2, there will
always be a crossover point beyond which merge sort is faster.
For a concrete example, let us pit a faster computer (computer A) running insertion sort
against a slower computer (computer B) running merge sort. They each must sort an array of
one million numbers. Suppose that computer A executes one billion instructions per second
and computer B executes only ten million instructions per second, so that computer A is 100
times faster than computer B in raw computing power. To make the difference even more
dramatic, suppose that the world's craftiest programmer codes insertion sort in machine
language for computer A, and the resulting code requires 2n2 instructions to sort n numbers.
(Here, c1 = 2.) Merge sort, on the other hand, is programmed for computer B by an average
programmer using a high-level language with an inefficient compiler, with the resulting code
taking 50n lg n instructions (so that c2 = 50). To sort one million numbers, computer A takes

while computer B takes

By using an algorithm whose running time grows more slowly, even with a poor compiler,
computer B runs 20 times faster than computer A! The advantage of merge sort is even more
pronounced when we sort ten million numbers: where insertion sort takes approximately 2.3
days, merge sort takes under 20 minutes. In general, as the problem size increases, so does
the relative advantage of merge sort.
Algorithms and other technologies
The example above shows that algorithms, like computer hardware, are a technology. Total
system performance depends on choosing efficient algorithms as much as on choosing fast
hardware. Just as rapid advances are being made in other computer technologies, they are
being made in algorithms as well.
You might wonder whether algorithms are truly that important on contemporary computers in
light of other advanced technologies, such as
 hardware with high clock rates, pipelining, and superscalar architectures,
 easy-to-use, intuitive graphical user interfaces (GUIs),
 object-oriented systems, and
 local-area and wide-area networking.

Different phases of the study of algorithm:


 Designing Algorithm: a various design technique is available to design algorithm.
They include.
1. Divide and conquer.
2. Greedy Method.
3. Dynamic Programming.
4. Backtracking.
5. Branch and Bound.
 Algorithm Validation: once the algorithm is devised (plan or invent), it is necessary
to show that it computes the correct answer for all possible legal inputs. The purpose
3

of validation is to ensure that the algorithm will work correctly independent of the
issues concerning the programming language to be used.
 Analysis of Algorithm: It refers to the task of determining the computing time (Time
Complexity) and storage space required (Space Complexity) to execute an algorithm
on a computer. It allows you to compare various algorithm used to solve a particular
problem. Performance of the algorithm in best, average and worst case is measured.
 Testing Programs: Debugging the program is done in this phase. It is the process of
executing a program on sample data sets to determine whether errors our and
correcting them.

Different Criteria that satisfy an algorithm: An algorithm must satisfy the following
criteria.
 Input: Zero or more quantities are externally supplying.
 Output: At least one quantity is produced.
 Definiteness: Each instruction is clear and unambiguous.
 Finiteness: If we trace out the instruction of an algorithm, then for all cases, the
algorithm terminates after a finite no. of steps.
 Effectiveness: Every instruction must also be feasible.

Analysis of Algorithm:
It is process of analyzing the problem- solving capability of the algorithm in terms of the time
and size required (the size of the memory for storage while implementation). The analysis is
a process of estimating the efficiency of an algorithm. There are two fundamental parameters
based on which we can analysis the algorithm:
o Space Complexity: The space complexity can be understood as the amount of space
required by an algorithm to run to completion.
o Time Complexity: Time complexity is a function of input size n that refers to the
amount of time needed by an algorithm to run to completion.

Before you implement any algorithm as a program, it is better to find out which among these
algorithms are good in terms of time and memory. It would be best to analyze every
algorithm in terms of Time that relates to which one could execute faster
and Memory corresponding to which one will take less memory.
we will be focusing more on time rather than space because time is instead a more limiting
parameter in terms of the hardware. It is not easy to take a computer and change its speed. So,
if we are running an algorithm on a particular platform, we are more or less stuck with the
performance that platform can give us in terms of speed.
However, on the other hand, memory is relatively more flexible. We can increase the
memory as when required by simply adding a memory card. So, we will focus on time than
that of the space.
The running time is measured in terms of a particular piece of hardware, not a robust
measure. When we run the same algorithm on a different computer or use different
programming languages, we will encounter that the same algorithm takes a different time.
Generally, we make three types of analysis, which is as follows:
o Worst-case time complexity: For 'n' input size, the worst-case time complexity can
be defined as the maximum amount of time needed by an algorithm to complete its
4

execution. Thus, it is nothing but a function defined by the maximum number of steps
performed on an instance having an input size of n.
o Average case time complexity: For 'n' input size, the average-case time complexity
can be defined as the average amount of time needed by an algorithm to complete its
execution. Thus, it is nothing but a function defined by the average number of steps
performed on an instance having an input size of n.
o Best case time complexity: For 'n' input size, the best-case time complexity can be
defined as the minimum amount of time needed by an algorithm to complete its
execution. Thus, it is nothing but a function defined by the minimum number of steps
performed on an instance having an input size of n.
o Amortized: The sequence of operations applied to the input of size a averaged over
time.
Usually, the efficiency or running time of an algorithm is stated as function relating the input
length to the number of steps known as time complexity, or volume of memory known as
space complexity.
An algorithm should have the following characteristics −
 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.
 Input − An algorithm should have 0 or more well-defined inputs.
 Output − An algorithm should have 1 or more well-defined outputs, and should
match the desired output.
 Finiteness − Algorithms must terminate after a finite number of steps.
 Feasibility − Should be feasible with the available resources.
 Independent − An algorithm should have step-by-step directions, which should be
independent of any programming code.

Designing Algorithm:
Classification by Design Method:
 Greedy Method: In the greedy method, at each step, a decision is made to choose the
local optimum, without thinking about the future consequences.
Example: Fractional knapsack, Activity Selection.
 Divide and Conquer: This method involves dividing the problem into sub problem,
recursively solving them, and then recombining them for the final answer.

Example: Merge sort, Quick sort.


5

 Dynamic Programming: The approach of this method is similar to divide and


conquer. The difference is that whenever recursive function calls with the same result,
instead of calling them again we try to store the result in data structure in the form of
a table and retrieve the results from the table. The overall time complexity is reduced.
Dynamic means dynamically decide whether to call a function or retrieve values from
the table.
Example: 0-1 Knapsack, subset-sum problem.
 Linear Programming: In linear programming, there are inequalities in terms of input
and maximizing or minimizing some linear function of input.
Example: Maximum flow of directed graph.
 Reduction (Transform and Conquer): In this method, we solve a difficult problem
by transforming in into a known problem for which we have an optimal solution.
Basically, the goal is to find a reducing algorithm whose complexity is not determined
by the resulting reduced algorithm.
 Backtracking Algorithm: It is an optimization technique to solve combinational
problems. It is applied to both programmatic and real-life problems. It is an
algorithmic method to solve a problem with an additional way.

 Each leaf node in a tree is a parent of one or more other nodes


 Each node in the tree, other than the root, has exactly one parent.
To "explore" node N:
1. If N is a goal node, return "success"
2. If N is a leaf node, return "failure"
3. For each child C of N,
Explore C
If C was successful, return "success"
4. Return "failure"
Branch and Bound: It is n optimization technique to get an optimal solution to the problem.
It looks for the best solution given problem in the entire space of the solution. The purpose of
the branch and bound search is to maintain lowest-cost path to a target.
6

Other Classifications: Apart from classifying the algorithms into the above broad categories,
the algorithm can be classified into other broad categories like:

 Randomized Algorithms: Algorithms that make random choices for faster solutions
are known as randomized algorithms.
Example: Randomized Quicksort Algorithm.

 Classification by complexity: Algorithms that are classified on the basis of time


taken to get a solution to any problem for input size. This analysis is known as time
complexity analysis.
Example: Some algorithms take O(n), while some take exponential time.

 Classification by Research Area: In CS each field has its own problems and needs
efficient algorithms.
Example: Sorting Algorithm, Searching Algorithm, Machine Learning
etc.

 Branch and Bound Enumeration and Backtracking: These are mostly used in
Artificial Intelligence.

Growth of Functions:
Algorithm’s rate of growth enables us to figure out an algorithm’s efficiency along with the
ability to compare the performance of another algorithm. Input size matters as constants and
lower order terms are influenced by the large sized of inputs.

Asymptotic Notation:
Types:
 Big-O Notation (O).
 Omega Notation (Ω).
 Theta Notation (θ).
7

Big-O Notation (O): It defines an upper bound of an algorithm, it bounds a function only
from above.
O(g(n)) = {f(n): there exist positive constants c and n 0 such that 0 ≤ f(n) ≤ c g(n) for all n
≥ n0}.

 g(n) is an asymptotic upper bound for f(n)


 if f(n) ∈ O(g(n)), we write f(n) = O(g(n))
There is no claim how tight the upper bound is!
Example: 2n2 = O(n3) with c = 1 and n0 = 2.
Examples of functions in O(n2).

Omega Notation (Ω):


Just as a Big O Notation provides an Asymptotic upper bound on a function, Ω notation
provides an asymptotic lower bound.
Ω(g(n)) = {f(n): there exist positive constants c and n 0 such that 0 ≤ c g(n) ≤ f(n) for all n
≥ n0}.

 g(n) is an asymptotic lower bound for f(n).


 Example: √n = Ω (lg n) with c = 1 and n0 = 16.
Examples of functions in Ω(n2)
8

Theta Notation (θ): The theta notation bounds a function from above and below, so it
defines exact asymptotic behaviour.
Θ(g(n)) = {f(n):
there exist positive constants c1, c2 and n0 such that
0 ≤ c1 g(n) ≤ f(n) ≤ c2 g(n) for all n ≥ n0}.

 g(n) is an asymptotically tight bound for f(n).


 Example: ½ n2 - 3n = Θ(n2) with c1 ≤ 1/14, c2 = ½ and n0 = 7.
½ n2 - 3n ≤ c2n2 for c2 = ½ is true for all n.
c1n2 ≤ ½ n2 - 3n for c1 = 1/14 is true for n=7
Remember that the constants MUST be positive
 Theorem:
 For any two functions f(n) and g(n), f(n) = Θ(g(n)) if and only if f = O(g(n)) and f =
Ω(g(n)).
 Leading constants and low order terms don’t matter.
 Ex. an2+bn+c= Θ(n2) for any constant a>0, b, and c implies that an 2+bn+c= O(n2) and
an2+bn+c= Ω (n2).

Standard Notations and Common Functions:


 Monotonicity
◦ f(n) is monotonically increasing if m ≤ n ⇒ f(m) ≤ f(n)
◦ f(n) is monotonically decreasing if m ≥ n ⇒ f(m) ≥f(n)
◦ f(n) is strictly increasing if m < n ⇒ f(m) < f(n)
◦ f(n) is strictly decreasing if m > n ⇒ f(m) > f(n)
 Exponentials
◦ Useful identities:
 a-1 = 1/a
 (am)n = amn
 aman = am+n
◦ Can relate rates of growth of exponentials and polynomials: for all real
constants a and b such that a > 1
 Which implies that nb = o(an).
◦ For all real x, ex ≥ 1 + x.

 Logarithms
9

◦ lg n = log2 n (binary logarithm).


◦ ln n = loge n (natural logarithm).
◦ lgk n = (lg n)k (exponentiation).
◦ lg lg n = lg(lg n) (composition).
◦ Useful identities for all real a > 0, b > 0, c > 0 and logarithm bases not 0

 Factorials
 n! = 1 ⋅ 2 ⋅ 3 ⋅ … ⋅ n
 Special case 0! = 1.
◦ A weak upper bound on the factorial is n! ≤n n since the Stirling’s
approximation says that

◦ If n! ≤ nn the lg(n!) ≤ lg nn i.e. lg(n!) = Θ (n lg n).

I. Exchange the values of two variables: It is also called as Swapping. It is a


process of exchange the values of two variable with each other. It is done with the data in
memory. The simplest method to swap two variables is to use a third temporary variable.
Define swap (a, b)
Temp= a;
A=b;
B=temp;
Exchanging the value of two variables means interchanging or swapping their values.
Suppose we have two values which is assigned into the two variables such as x and y are two
variables and suppose we take two values in it as x=10 and y=20 then after the interchanging
of values the output will be x=20 and y=10. There is different method for exchanging the
values of two variables which is as follows:
Using the temporary variable
In this method we take another variable as temporary. So there are three variable as a, b and
temp. Here temp is the temporary variable.
Algorithm:
Step 1: Start
Step 2: Take the value of variables a and b.
Step 3: Assign the value of variable a into the temp variable.
Step 4: Assign the value of variable b into a variable.
Step 5: Assign the value of variable temp into the b variable.
10

Step 6: Print the value of a and b.


Step 7: Stop

II. Summation of a set of numbers


Summation means adding the number, which is given in the series.
Note: Computer can add two numbers at a time and return the sum of two numbers. So for
this purpose we assign the variable sum as zero.
sum= sum+a1 (Here a1 is the first number) then we do
sum=sum+a2 (Here the new value of sum contains a1+a2) then
sum=sum+a3 (Here the new value of sum contains a1+a2+a3) and so on
sum=sum+an (Here the new value of sum contains a1+a2+a3+.........+ an )
Algorithm:
Step 1: Start
Step 2: Read n numbers to be summed from the input device (such as keyboard).
Step 3: Initialize the variable sum as zero.
Step 4: Initialize the variable count as one.
Step 5: While count is less than or equal to n, i.e. numbers to be added, repeatedly do: Read
the number at position count, i.e. when count is 1 read first number, when count is 2, read
second number and so on. Update the current sum by adding to it the number read.
Step 6: Write the sum of nth numbers.
Step 7: Stop.

Factorial Computation: In mathematical concept, the end product of given positive (+)
integers (n) or number. Factorial is also called as the end product of an integer and all the
integers falling under it.
Product of all consecutive Integer numbers up to n is called Factorial of a Number and is
denoted by n! For Example, the value of 5! is 120.

Mathematically it is written as,


n! = 1 * 2 * 3 * 4 * ... * (n-1) * n
For example, the factorial of 5 is,
5! = 1 * 2 * 3 * 4 * 5 = 120

Step 1: Start
Step 2: Declare Variable n, fact, i
Step 3: Read number from User
Step 4: Initialize Variable fact=1 and i=1
Step 5: Repeat Until i<=number
5.1 fact=fact*i
5.2 i=i+1
Step 6: Print fact
Step 7: Stop

Generating Fibonacci Sequence:


11

In the Fibonacci sequence expect for the first two terms of the sequence, every other term is
the sum of the previous two terms. The first two numbers of the Fibonacci series are 0 and 1.
From the 3rd number onwards, the series will be the sum of the previous 2 numbers.

Step 1: Start.
Step 2: Declare variables I, a, b, show
Step 3: Initialize the variables, a=, b=, and show= 0
Step 4: Enter the number of terms of Fibonacci series to be printed
Step 5: Print First two terms of series
Step 6: Use loop for the following steps
 Show= a+b
 A=b
 B=show
 Increase value of I each time by 1
 Print the values of show
Step 7: End

0,1,1,2,3,5,8,13,21,34…

Reverse the digits of an integer: It will read an integer positive number and reverse that
number.

Step 1: Start.
Step 2: Read the number n.
Step 3: [Initialize]
Set rev = 0
Step 4: Repeat step 5, 6 & 7 until n! =0
Step 5: set r= n% 10
Step 6: set rev= rev*10+r
Step 7: set n=n/10
Step 8: print rev
Step 9: Stop

Base Conversion:
 Decimal to binary, octal or hex base
 Binary, octal or hex to decimal base
 Between binary and octal or hex
12

 Between octal hex

This activity introduces the concept that abstractions built upon binary sequences
can be used to represent all digital data. It also introduces the concept of
an algorithm. It focuses on the following learning objectives:

 5b. Explanation of how number bases, including binary and decimal, are
used for reasoning about digital data.
 16a. Use of natural language, pseudo-code, or a visual or textual
programming language to express an algorithm.

Introduction

This lesson assumes you have completed the homework on binary and
hexadecimal number systems. That homework decribed how the binary, decimal,
and hexadecimal number systems work and showed how to convert from one
number system to another.

In this lesson we want generalize what we learned their by seeing those number
systems as specific examples of a more general concept, a positional number
system.

We will develop algorithms that will enable you to perform conversions from one
number system to another.

The type of generalization we are doing in this lesson is another example of


the abstraction principle in computer science -- here we are focusing on a general
pattern that holds true for all positional number systems.

Algorithms and Pseudocode

An algorithm is a step-by-step procedure to perform some computation. For


example, the steps you take in the Hello Purr app when the button is clicked is an
example of a simple 2-step algorithm:

To help us talk about algorithms we will use pseudocode, a language or notation


that has many of the structures of a programming language but is easy to read.
13

Pseudocdes are halfway between natural languages like English and formal
programming languages.

Positional Number Systems

Let's review some of the key points that you learned in the Khan Academy videos.

 Our decimal number system (and the binary and hexadecimal systems) are
particular instances of the more general concept of a positional number
system.
 In a positional number system the same symbol can represent different
values depending on its position (or place) in the numeral. For example, in
91, the 9 represents 90 (the 10s place) but in 19 it represents 9 (the ones
place). Contrast this with how symbols work in a non-positional system, like
Roman numerals, where X always represents 10.
 The base of a number system represents the number of symbols it has:

Name Base Symbols


Decimal 10 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Binary 2 0, 1
Hexadecimal 16 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
Octal 8 0, 1, 2, 3, 4, 5, 6, 7

 Positional number systems use exponentiation to determine a symbol's value


based on its place. We can use this idea to convert from any system into the
decimal system:

System Base Value Conversion Formula Decimal Value


(1 × 102) + (0 × 101) +
Decimal 10 104 100 + 0 + 4 = 104
(4 × 100)
(1 × 22) + (1 × 21) + (1
Binary 2 111 4+2+1=7
× 20)
(1 × 82) + (0 × 81) + (4
Octal 8 104 64 + 0 + 4 = 68
× 80)
(F × 162) + (E × 161) + 15 × 256 + 14 × 16 + 12 × 1 =
Hexadecimal 16 FEC
(C × 80) 3840 + 224 + 12 = 4076

Conversion Algorithms

Let's summarize these conversion formulas by developing an general algorithm


that will convert from any base into decimal.

Algorithm to Convert From Any Base to Base 10 Decimal


14

1. Let n be the number of digits in the number. For example, 104 has 3 digits,
so n=3.
2. Let b be the base of the number. For example, 104 is decimal so b = 10.
3. Let s be a running total, initially 0.
4. For each digit in the number, working left to right do:
Subtract 1 from n.
Multiply the digit times bn and add it to s.
5. When your done with all the digits in the number, its decimal value will be s

Let's try it on the binary number 1011.


Let n = 4.
Let b = 2.
Let s = 0.
First digit, 1: n = 3, 1 × bn is 1 × 23 = 8. So s = 8.
Second digit, 0: n = 2, 0 × bn is 0 × 22 = 0. So s = 8.
Third digit, 1: n = 1, 1 × bn is 1 × 21 = 2. So s = 10
Last digit, 1: n = 0, 1 × bn is 1 × 20 = 1. So 10112 = 1110

Digit n Value = Digit * bn Running Total


1 3 1 × 23 = 8 8
2
0 2 0×2 =0 8
1
1 1 1×2 =2 10
0
1 0 1×2 =1 11

Let's try it on the hex number 7E.


Let n = 2.
Let b = 16.
Let s = 0.
First digit, 7: n = 1, 7 × bn is 7 × 161 = 7 × 16 = 112. So s = 112.
Last digit, E: n = 0, 14 × bn is 14 × 160 = 14. So s = 112 + 14 = 126.
So 7E16 = 12610

Digit n Value = Digit * bn Running Total


7 1 7 × 161 = 112 114
0
E 0 14 × 16 = 14 126

Let's try it on the octal number 124.


Let n = 3.
Let b = 8.
Let s = 0.
First digit, 1: n = 2, 1 × bn is 1 × 82 = 1 × 64 = 64. So s = 64.
Second digit, 2: n = 1, 2 × bn is 2 × 81 = 2 × 8 = 16. So s = 64 + 16 =
80.
Last digit, 4: n = 0, 4 × bn is 4 × 80 = 4. So s = 80 + 4 = 84. So 1248
= 8410

Digit n Value = Digit * bn Running Total


1 2 1 × 82 = 64 64
15

2 1 2 × 81 = 16 80
4 0 4 × 80 = 4 84

Algorithm to Convert From Decimal To Another Base


1. Let n be the decimal number.
2. Let m be the number, initially empty, that we are converting to. We'll be
composing it right to left.
3. Let b be the base of the number we are converting to.
4. Repeat until n becomes 0
Divide n by b, letting the result be d and the remainder be r.
Write the remainder, r, as the leftmost digit of b.
Let d be the new value of n.

Let's use the algorithm to convert 45 into binary.


Let n = 45.
Let b = 2.
Repeat
45 divided by b is 45/2 = 22 remainder 1. So d=22 and r=1. So m= 1
and the new n is 22.
22 divided by b is 22/2 = 11 remainder 0. So d=11 and r=1. So m= 01
and the new n is 11.
11 divided by b is 11/2 = 5 remainder 1. So d=5 and r=1. So m= 101
and the new n is 5.
5 divided by b is 5/2 = 2 remainder 1. So d=2 and r=1. So m= 1101
and the new n is 2.
2 divided by b is 2/2 = 1 remainder 0. So d=1 and r=0. So m= 01101
and the new n is 1.
1 divided by b is 1/2 = 0 remainder 1. So d=0 and r=1. So m=101101
and the new n is 0. So 4510 = 1011012

Let's use it to convert 99 into binary.


Let n = 99.
Let b = 2.
Repeat
99 divided by b is 99/2 = 49 remainder 1. So d=49 and r=1. So m= 1
and the new n is 49.
49 divided by b is 49/2 = 24 remainder 1. So d=24 and r=1. So m= 11
and the new n is 24.
24 divided by b is 24/2 = 12 remainder 0. So d=12 and r=0. So m= 011
and the new n is 12.
12 divided by b is 12/2 = 6 remainder 0. So d=6 and r=0. So m= 0011
and the new n is 6.
6 divided by b is 6/2 = 3 remainder 0. So d=3 and r=0. So m= 00011
and the new n is 3.
3 divided by b is 3/2 = 1 remainder 1. So d=1 and r=1. So m= 100011
and the new n is 1.
1 divided by b is 1/2 = 0 remainder 1. So d=0 and r=1. So m=1100011
and the new n is 0. So 9910 = 11000112

Let's use it to convert 45 into hexadecimal.


16

Let n = 45.
Let b = 16.
Repeat
45 divided by b is 45/16 = 2 remainder 13. So d=2 and r=13. So m= D and
the new n is 2.
2 divided by b is 2/16 = 0 remainder 2. So d=0 and r=2. So m=2D and
the new n is 0. So 4510 = 2D16.

Let's use it to convert 99 into hexadecimal.


Let n = 99.
Let b = 16.
Repeat
99 divided by b is 99/16 = 6 remainder 3. So d=6 and r=3. So m= 3 and
the new n is 6.
6 divided by b is 6/16 = 0 remainder 6. So d=0 and r=6. So m=63 and
the new n is 0. So 9910 is 6316.

Decimal to Binary Number:


Suppose if we have to convert decimal to binary, then divide the decimal number by 2.
Example 1. Convert (25)10 to binary number.
Solution: Let us create a table based on this question.

Operation Output Remainder

25 ÷ 2 12 1(MSB)

12 ÷ 2` 6 0

6÷2 3 0

3÷2 1 1

1÷2 0 1(LSB)
Therefore, from the above table, we can write,
(25)10 = (11001)2

Decimal to Octal Number:


To convert decimal to octal number we have to divide the given original number by 8 such
that base 10 changes to base 8. Let us understand with the help of an example.
Example 2: Convert 12810 to octal number.
Solution: Let us represent the conversion in tabular form.
17

Operation Output Remainder

128÷8 16 0(MSB)

16÷8 2 0

2÷8 0 2(LSB)
Therefore, the equivalent octal number = 2008
Decimal to Hexadecimal:
Again in decimal to hex conversion, we have to divide the given decimal number by 16.
Example 3: Convert 12810 to hex.
Solution: As per the method, we can create a table;

Operation Output Remainder

128÷16 8 0(MSB)

8÷16 0 8(LSB)
Therefore, the equivalent hexadecimal number is 8016
Here MSB stands for a Most significant bit and LSB stands for a least significant bit.

Other Base System to Decimal Conversion


Binary to Decimal:
In this conversion, binary number to a decimal number, we use multiplication method, in
such a way that, if a number with base n has to be converted into a number with base 10, then
each digit of the given number is multiplied from MSB to LSB with reducing the power of
the base. Let us understand this conversion with the help of an example.
Example 1. Convert (1101)2 into a decimal number.
Solution: Given a binary number (1101)2.
Now, multiplying each digit from MSB to LSB with reducing the power of the base number
2.
1 × 23 + 1 × 22 + 0 × 21 + 1 × 20
=8+4+0+1
= 13
Therefore, (1101)2 = (13)10
Octal to Decimal:
To convert octal to decimal, we multiply the digits of octal number with decreasing power of
the base number 8, starting from MSB to LSB and then add them all together.
Example 2: Convert 228 to decimal number.
18

Solution: Given, 228


2 x 81 + 2 x 80
= 16 + 2
= 18
Therefore, 228 = 1810
Hexadecimal to Decimal:
Example 3: Convert 12116 to decimal number.
Solution: 1 x 162 + 2 x 161 + 1 x 160
= 16 x 16 + 2 x 16 + 1 x 1
= 289
Therefore, 12116 = 28910

Hexadecimal to Binary Shortcut Method


To convert hexadecimal numbers to binary and vice versa is easy, you just have to memorize
the table given below.

Hexadecimal Number Binary

0 0000

1 0001

2 0010

3 0011

4 0100

5 0101

6 0110

7 0111

8 1000

9 1001

A (10) 1010

B (11) 1011

C (12) 1100

D (13) 1101
19

E (14) 1110

F (15) 1111
You can easily solve the problems based on hexadecimal and binary conversions with the
help of this table. Let us take an example.
Example: Convert (89)16 into a binary number.
Solution: From the table, we can get the binary value of 8 and 9, hexadecimal base numbers.
8 = 1000 and 9 = 1001
Therefore, (89)16 = (10001001)2

Octal to Binary Shortcut Method


To convert octal to binary number, we can simply use the table. Just like having a table for
hexadecimal and its equivalent binary, in the same way, we have a table for octal and its
equivalent binary number.

Octal Number Binary

0 000

1 001

2 010

3 011

4 100

5 101

6 110

7 111
Example: Convert (214)8 into a binary number.
Solution: From the table, we know,
2 → 010
1 → 001
4 → 100
Therefore,(214)8 = (010001100)2

You might also like