0% found this document useful (0 votes)
16 views

C Unit-1 Notes

Uploaded by

ff17062007
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)
16 views

C Unit-1 Notes

Uploaded by

ff17062007
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/ 37

UNIT I - INTRODUCTION TO C PROGRAMMING

Programming Paradigm Defined


A programming paradigm is the classification, style or way of programming. It is
an approach to solve problems by using programming languages. Depending on
the language, the difficulty of using a paradigm differs.

There are several programming languages that use paradigms, but to do this, they
need to follow a strategy or methodology. Paradigms are not meant to be mutually
exclusive; a single program can feature multiple paradigms. Below is an overview
of programming languages and their paradigm methodology.

 Imperative: Programming with an explicit sequence of commands.


 Declarative: Programming by specifying the result a user wants, instead of how to
get it.
 Structured: Programming with clean control structures.
 Procedural: Imperative programming with procedure calls.
 Functional: Programming with function calls that avoid any global state.
 Function-Level: Programming with no variables at all.
 Object-Oriented: Programming by defining objects that send messages to each
other.
 Event-Driven: Programming with emitters and listeners of asynchronous actions.
 Flow-Driven: Programming processes communicating with each other over
predefined channels.
 Logic: Programming by specifying a set of facts and rules.
 Constraint: Programming by specifying a set of constraints.
 Aspect-Oriented: Programming cross-cutting concerns applied transparently.
 Reflective: Programming by manipulating the program elements.
 Array: Programming with powerful array operators.

Simple model of a Computer


A computer device is made up of various elements which help in its effective
functioning and processing. There are five basic components of the computer which
help in making this processing of data easier and convenient.
There are five basic components which include:
1. Input Unit
2. Output Unit
3. Memory Unit
4. Control Unit
5. Arithmetical and Logical Unit
The exterior of any computerised device may look different and may also have
varied features, but the basic components remain the same for their functioning.
Since computers (in various forms) have become a part of
everyday life,understanding these components of a computer system is important
for everyone.

● Input Unit

A computer will only respond when a command is given to the device. These
commands can be given using the input unit or the input devices.
For example: Using a keyboard we can type things on a Notepad and the computer
processes the entered data and then displays the output of the same of the screen.
The data entered can be in the form of numbers, alphabet, images, etc. We enter the
information using an input device, the processing units convert it into computer
understandable languages and then the final output is received by a human-
understandable language.
● Output Unit

When we command a computer to perform a task, it reverts for the action performed
and gives us a result. This result is called output. There are various output devices
connected to the computer. The most basic of which is a monitor. Whatever we write
using a keyboard or click using a mouse, is all displayed on the monitor.Thus, the
output unit gives us the final result once the entire processing is done within the
mechanism of a device.
For example: when we visit an ATM, we enter our details like language, pin, amount
to be withdrawn, etc. and then the final money which the cash dispenser releases is
our outcome. In this case, the cash dispenser acts as an output unit.
To get a list of computer input and output devices and the function of the various I/O
devices, visit the linked article.
For better understanding and more interactive analysis of the components of the
computer, candidates can check the video given below and get detailed information
reading the five major components responsible for the functioning of a computer
device.
● Memory Unit

When we enter the data into the computer using an input device, the entered
information immediately gets saved in the memory unit of the Central Processing
Unit (CPU). Because of the presence of some existing programming, the Memory
Unit transmits the data further to the other parts of the CPU.
Similarly, when the output of our command is processed by the computer, it is saved
in the memory unit before giving the output to the user.
● Control Unit

This is the core unit which manages the entire functioning of the computer device.It
is one of the most essential components of the computer system.
The Control Unit collects the data entered using the input unit, leads it on for
processing and once that is done, receives the output and presents it to the user. Itcan
be said to the centre of all processing actions taking place inside a computer device.
Basically, the instructions taken, interpretation of entered data, issuing signals to
execute the data and then finally retrieving the data is all done in the Control Unit.
Arithmetic & Logical Unit

As the name suggests, all the mathematical calculations or arithmetic operations are
performed in the Arithmetic and Logical Unit of the CPU.
It can also perform actions like a comparison of data and decision-making actions.
The ALU comprises circuits using which addition, subtraction, multiplication,
division and other numerical based calculations can be performed.

Central Processing Unit (CPU)


The Central Processing Unit is the core of any computer devices. It comprises
three major components of the computer which have been discussed above:
● Memory Unit
● Control Unit
● Arithmetic and Logical Unit

All these three units are elements of CPU and together help in the efficient working
and processing of data. It is also known as the “Brain of Computer” and no action
can be conducted by a device without the execution and permission of the Central
Processing Unit.
The device is a close-knit circuit comparison microprocessor which helps infetching
the data and proving suitable results to the user. Thus, CPU is the main processing
unit of the computer.

Algorithm :
Step by step procedure of solving the problem
Definition : It is a sequence of steps to solve a particular problem or algorithm is an
ordered set of unambiguous steps that produces a result and terminates in a finite
number of steps/time.
Algorithm has following Characteristics:
Input : an algorithm may or may not require input
Finiteness : an algorithm must consist of finite number of steps
Effectiveness : an algorithm must consist of single entry and single exit.
Definiteness : each step of the algorithm must be defined precisely.
Readability : the steps of the algorithm must be easily readable.
Output : Each algorithm is expected to produce at least one output/result.

Time complexity : time required by the algorithm for performing

Space complexity : memory required by the algorithm for performing

Example :

Normal instructions :

Step 1 : fill the kettle with water

Step 2 : place it on the stove and turn on the burner

Step 3 : turn off the burner when the water starts boiling .

Computer instruction / algorithm :


Step 1 : start
Step 2 : put the kettle under the tap

Step 3 : turn on the tap

Step 4 : check if 90% of the kettle is filled

Step 5 : if not , repeat the above steps

Step 6 : turn off the tap


Step 7 : place the kettle in the burner

Step 8 : turn on the burner

Step 9 : check if the water is 100 C Step 10 : if not , repeat the above step

Step 11 : turn off the burner

Step 12 : stop

Example of Algorithm

Problem 1: Find the area of a Circle of radius r.

Inputs to the algorithm: Radius r of the Circle.

Expected output: Area of the Circle

Algorithm:

Step1: Read\input the Radius r of the Circle

Step2: Area PI*r*r // calculation of area

Step3: Print Area

Problem2: Write an algorithm to read two numbers and find theirsum.

Inputs to the algorithm: First num1. Second num2.


Expected output: Sum of the two numbers.
Algorithm:

Step1: Start

Step2: Read\input the first num1.

Step3: Read\input the second num2.

Step4: Sum num1+num2 // calculation of sum

Step5: Print Sum

Step6: End

Problem 3: Convert temperature Fahrenheit to Celsius

Inputs to the algorithm: Temperature in Fahrenheit

Expected output: Temperature in Celsius

Algorithm:

Step1: Start
Step 2: Read Temperature in Fahrenheit F

Step 3: C 5/9*(F32)

Step 4: Print Temperature in Celsius: C

Step5: End
Type of Algorithms

The algorithm and flowchart, classification to the three types of controlstructures.

They are:

1. Sequence

2. Branching (Selection)

3. Loop (Repetition)

These three control structures are sufficient for all purposes.

● The sequence is exemplified by a sequence of statements placed one after the other
– the one above or before another gets executed first. In flowcharts, sequence of
statements is usually contained in the rectangular process box.
● The branch refers to a binary decision based on some condition. If the condition is
true, one of the two branches is explored; if the condition is false, the other
alternative is taken. This is usually represented by the ‘if-then’ construct in pseudo-
codes andprograms. In flowcharts, this is represented by the diamond-shaped
Decision box.This structure is also known as the selectionstructure.

Problem1: write algorithm to find the greater number between two numbers

Step1: Start

Step2: Read/input A and B

Step3: If A greater than B then C=A


Step4: if B greater than A then C=BStep5: Print C

Step6: End

Problem2: write algorithm to find the result of equation: ( ) {

Step1: Start

Step2: Read/input x

Step3: If X Less than zero then F=-X

Step4: if X greater than or equal zero then F=X Step5: Print F

Step6: End

Problem3: An algorithm to find the largest value of any threenumbers.


Step1: Start

Step2: Read/input A,B and C

Step3: If (A>=B) and (A>=C) then Max=A

Step4: If (B>=A) and (B>=C) then Max=B

Step5:If (C>=A) and (C>=B) then Max=C


Step6: Print Max

Step7: End
The loop allows a statement or a sequence of statements to be repeatedly
executed based on some loop condition. It is represented by the ‘while’ and
‘for’ constructs in most programming languages, for unbounded loops and
bounded loops respectively. (Unbounded loops refer to those whose number of
iterations depends on the eventuality that the termination condition is satisfied;
bounded loopsrefer to those whose number of iterations is known before-hand.)
In the flowcharts, a back arrow hints the presence of a loop. A trip around the
loop is known as iteration. You must ensure that the condition for the
termination of the looping must be satisfied after some finite number of
iterations, otherwise it ends up as an infinite loop, a common mistake made by
inexperienced programmers. The loop is also known as the repetition structure.
Examples:

Problem1: An algorithm to calculate even numbers between 0 and99


1. Start
2. I ← 0
3. Write I in standard output
4. I ← I+2
5.If (I <=98) then go to line 3
6.End

Problem2: Design an algorithm which gets a natural value, n,as its input and
calculates odd numbers equal or less than n. Then write them in the standard
output:
1. Start
2. Read n
3. I ← 1
4. Write I
5. I ← I + 2
6. If ( I <= n) then go to line 4
7. End

Problem3: Design an algorithm which generates even numbersbetween


1000 and 2000 and then prints them in the standard output. Itshould also
print total sum:
1. Start
2. I ← 1000 and S ← 0
3.Write I
4.S ← S + I
5. I ← I + 2
6.If (I <= 2000) then go to line 3else go to line 7
7.Write S
8.End

Problem4: Design an algorithm with a natural number, n, as itsinput which


Calculates the following formula and writes the result in the standard output:
S = 1⁄2 + 1⁄4 + ... +1/n
1.Start
2.Read n
3.I ← 2 and S ← 0
4. S= S + 1/I
5. I ← I + 2
6.If (I <= n) then go to line 4 else write S in standard output
7.End

Flowchart

A flowchart is simply a graphical representation of steps. It shows steps in


sequential order and is widely used in presenting the flow of algorithms,
workflow or processes. Typically, a flowchart shows the steps as boxes of
various kinds, and their order by connecting them with arrows.

Flowchart Symbols
Different flowchart shapes have different conventional meanings. The

meanings of some of the more common shapes are as follows:

Terminator
The terminator symbol represents the starting or ending point of the system.

Process

A box indicates some particular operation.

Document
This represents a printout, such as a document or a report.
Decision
A diamond represents a decision or branching point. Lines coming out

from the diamond indicates different possible situations, leading to different

sub-processes.

Data
It represents information entering or leaving the system. An input might be

an order from a customer. Output can be a product to be delivered.

On-Page Reference
This symbol would contain a letter inside. It indicates that the flow

continues on a matching symbol containing the same letter somewhere else on

the same page.


Off-Page Reference
This symbol would contain a letter inside. It indicates that the flow

continues on a matching symbol containing the same letter somewhere else on

a different page.

Delay or Bottleneck
Identifies a delay or a bottleneck.

Flow
Lines represent the flow of the sequence and direction of a process.

Flowchart Example – Simple Algorithms


A flowchart can also be used in visualizing algorithms, regardless of its

complexity. Here is an example that shows how flowchart can be used in


showing a simple summation process.

Link for Flowchart Example/Assignments :

https://www.cimt.org.uk/projects/mepres/book8/bk8i1/bk8_1i2.htm

https://dyclassroom.com/flowchart/exercise-1

https://www.cimt.org.uk/projects/mepres/book8/bk8i1/bk8_1i3.htm
https://www.cimt.org.uk/projects/mepres/book8/book8int.htm
Introduction to ‘C’ programming
C is a general-purpose programming language that is extremely
popular, simple, and flexible to use. It is a structured programming language that is
machine-independent and extensively used to write various applications, Operating
Systems like Windows, and many other complex programs like Oracle database, Git,
Python interpreter, and more.
It is said that ‘C’ is a god’s programming language. One can say, C is a base for
the programming. If you know ‘C,’ you can easily grasp the knowledge of the other
programming languages that uses the concept of ‘C’.

It is essential to have a background in computer memory mechanisms because it is


an important aspect when dealing with the C programming language.

‘C’ is a powerful programming language which is strongly associated


with the UNIX operating system. Even most of the UNIX operating system is coded
in ‘C’. Initially ‘C’ programming was limited to the UNIX operating system, but as
it started spreading around the world, it became commercial, and many compilers
were released for cross-platform systems. Today ‘C’ runs under a variety of
operating systems and hardware platforms. As it started evolving many different
versions of the language were released. At times it became difficult for the
developers to keep up with the latest version as the systems were running under the
older versions. To assure that ‘C’ language will remain standard, American National
Standards Institute (ANSI) defined a commercial standard for ‘C’ language in 1989.
Later, it was approved by the International StandardsOrganization (ISO) in 1990.
‘C’ programming language is also called as ‘ANSI C’.

Structure of c program
The structure of a C program means the specific structure to start the programming
in the C language. Without a proper structure, it becomes difficult to analyze the
problem and the solution. It also gives us a reference to write more complex
programs.
Importance of structure of a C program

Sometimes, when we begin with a new programming language, we are not aware
about the basic structure of a program. The sections of a program usually get
shuffled and the chance of omission of error rises. The structure of a language gives
us a basic idea of the order of the sections in a program. We get to know when and
where to use a particular statement, variable, function, curly braces,
parentheses, etc. It also increases our interest in that programming language.

Thus, the structure helps us analyze the format to write a program for the least
errors. It gives better clarity and the concept of a program.
Parts of C program-

1. # include <stdio.h> – This command is a preprocessor directive in C that includes


all standard input-output files before compiling any C program so as to make use of
all those functions in our C program.
2. int main() – This is the line from where the execution of the program starts.The
main() function starts the execution of any C program.
3. { (Opening bracket) – This indicates the beginning of any function in the program
(Here it indicates the beginning of the main function).
4. /* some comments */ – Whatever is inside /*——-*/ are not compiled and
executed; they are only written for user understanding or for making the
program interactive by inserting a comment line. These are known as multiline
comments. Single line comments are represented with the help of 2 forward
slashes “//——”.
5. printf(“Hello World”) –The printf() command is included in the C stdio.h library,
which helps to display the message on the output screen.
6. getch() – This command helps to hold the screen.
7. return 0 –This command terminates the C program and returns a null value, that is,
0.
8. } (Closing brackets)- This indicates the end of the function. (Here it indicates the
end of the main function)

A C program basically consists of the following parts −


● Preprocessor Commands
● Functions
● Variables
● Statements & Expressions
● Comments
Let us look at a simple code that would print the words "Hello World" −

Live Demo

#include <stdio.h>
int main() {

/* my first program in C */

printf("Hello, World! \n");

return 0;

Compile and Link C Program

There are three basic phases occurred when we execute any C program.

● Preprocessing
● Compiling
● Linking
Preprocessing Phase

A C pre-processor is a program that accepts C code with preprocessing statements

and produces a pure form of C code that contains no preprocessing statements (like

#include).

Compilation Phase

The C compiler accepts a preprocessed output file from the preprocessor and

produces a special file called an object file. Object file contains machine code

generated from the program


Linking Phase

The link phase is implemented by the linker. The linker is a process that

accepts as input object files and libraries to produce the final executable program.

Conversion of simple algorithm to program

Example of Algorithm to C program conversion.


/* Example of Algorithm to C program conversion.

Problem Definition: Write an algorithm for Subtracting two Numbers. Step 1:

Start.

Step 2: Read two numbers A and B. Step 3: Answer = A - B.

Step 4: Display Answer. Step 5: Stop

#include<stdio.h>int main()

int A, B, Answer;

printf("Enter number A:");scanf("%d",&A);

printf("Enter number B:");scanf("%d",&B);

Answer = A - B; printf("%d", Answer);


return 0;

}
Where is C used? Key Applications

1. ‘C’ language is widely used in embedded systems.


2. It is used for developing system applications.
3. It is widely used for developing desktop applications.
4. Most of the applications by Adobe are developed using ‘C’ programming language.
5. It is used for developing browsers and their extensions. Google’s Chromium is
builtusing ‘C’ programming language.
6. It is used to develop databases. MySQL is the most popular database software
which is built using ‘C’.
7. It is used in developing an operating system. Operating systems such as Apple’sOS
X, Microsoft’s Windows, and Symbian are developed using ‘C’ language. It isused
for developing desktop as well as mobile phone’s operating system.
8. It is used for compiler production.
9. It is widely used in IOT applications.

What are Identifiers


■ C identifier is a name used to identify a variable, function, or any other user-defined item.
■ An identifier starts with a letter A to Z, a to z, or an underscore '_' followed by zero or more
letters, underscores, and digits (0 to 9).
What are keywords
The following list shows the reserved words in C. These reserved words may not be used as
constants or variables or any other identifier names.
C-Datatypes
■ Data types specify how we enter data into our programs and what type of data we enter.
■ C language has some predefined set of data types to handle various kinds of data that we can use
in our program.
■ These datatypes have different storage capacities.
■ C language supports 2 different type of data types:
■ Primary data types: These are fundamental data types in C namely integer(int), floating
point(float), character(char) and void.
■ Derived data types: Derived data types are nothing but primary datatypes but a little twisted or
grouped together like array, structure, union and pointer.

Variables in C
■ A variable is a name of the memory location. It is used to store data. Its value can be changed,
and it can be reused many times.
■ It is a way to represent memory location through symbol so that it can be easily identified.
Let's see the syntax to declare a variable:
type variable_list;
int a;
float b;
char c;
Rules for defining variables
■ A variable can have alphabets, digits, and underscore.it should always begin with alphabet.
■ A variable name can start with the alphabet, and underscore only. It can't start with a digit.
■ No whitespace is allowed within the variable name.
■ No special characters are allowed except underscores(_).
■ A variable name must not be any reserved word or keyword,
e.g. int, float, etc.
■ Maximum length of variables is 31 characters. But when the first eight characters are the same
then the variable names are not different.
Valid variable names:
int a;
int _ab;
int a30;
Types of Variables in C
■ Local variable
■ Global variable
■ Static variable
■ Automatic variable
■ External variable

Local Variable
■ A variable that is declared inside the function or block is called a local variable.
■ It must be declared at the start of the block.
Example code:
void function1()
{
int x=10;//local variable
}
■ You must have to initialize the local variable before it is used.

Global Variable
■ A variable that is declared outside the function or block is called a global variable. Any
function can change the value of the global variable. It is available to all the functions.
■ It must be declared at the start of the block.
Example code:
int value=20;//global variable
void function1(){
int x=10;//local variable
}
Static Variable
■ A variable that is declared with the static keyword is called static variable.
■ It retains its value between multiple function calls.
void function1(){
int x=10;//local variable
static int y=10;//static variable
x=x+1;
y=y+1;
printf("%d,%d",x,y);
}
■ If you call this function many times, the local variable will print the same value for each
function call, e.g, 11,11,11 and so on. But the static variable will print the incremented
value in each function call, e.g. 11, 12, 13 and so on.
Automatic Variable
■ All variables in C that are declared inside the block, are automatic variables by default. We
can explicitly declare an automatic variable using auto keyword
Example code:
void main(){
int x=10;//local variable (also automatic)
auto int y=20;//automatic variable
}
External Variable
■ We can share a variable in multiple C source files by using an external variable. To declare
an external variable, you need to use extern keyword
■ myfile.h
extern int x=10;//external variable (also global)

■ program1.c
#include "myfile.h"
#include <stdio.h>
void printValue()
{
printf("Global variable: %d", global_variable);
}

Program to swap two numbers (using two methods)


Swapping of two numbers in C (using third variable )
#include <stdio.h>
int main()
{
int x, y, t;
printf("Enter two integers\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);
t = x;
x = y;
y = t;
printf("After Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);
return 0;
}
The output of the program:
Enter two integers
23
45
Before Swapping
First integer = 23
Second integer = 45
After Swapping
First integer = 45
Second integer = 23
Program to swap two numbers (using two methods)
Swapping of two numbers without third variable
#include <stdio.h>
int main()
{
int a, b;

printf("Input two integers (a & b) to swap\n");


scanf("%d%d", &a, &b);

a = a + b;
b = a - b;
a = a - b;
printf("a = %d\nb = %d\n",a,b);
return 0;
}
What is constants ?
■ As the name suggests the name constants is given to such variables or values in C/C++
programming language which cannot be modified once they are defined.
■ They are fixed values in a program.
■ There can be any types of constants like
■ Integer, float, octal, hexadecimal, character constants etc.

Range of constants
■ Every constant has some range.
■ The integers that are too big to fit into an int will be taken as long.
■ Now there are various ranges that differ from unsigned to signed bits.
■ Under the signed bit, the range of an int varies from -128 to +127 and under the unsigned bit, int
varies from 0 to 255.
Defining Constants
■ In C/C++ program we can define constants in two ways as shown below:
■ Using #define preprocessor directive
■ Using a const keyword
■ What is Literals:
■ The values assigned to each constant variables are referred to as the literals. Generally, both
terms, constants and literals are used interchangeably.
NUMBER SYSTEM
Number systems are the technique to represent numbers in the computer system architecture,
every value that you are saving or getting into/from computer memory has a defined number
system.

Computer architecture supports following number systems.

 Binary number system


 Octal number system
 Decimal number system
 Hexadecimal (hex) number system

BINARY NUMBER SYSTEM

A Binary number system has only two digits that are 0 and 1. Every number (value) represents
with 0 and 1 in this number system. The base of binary number system is 2, because it has only
two digits.

OCTAL NUMBER SYSTEM

Octal number system has only eight (8) digits from 0 to 7. Every number (value) represents with
0,1,2,3,4,5,6 and 7 in this number system. The base of octal number system is 8, because it has
only 8 digits.

DECIMAL NUMBER SYSTEM

Decimal number system has only ten (10) digits from 0 to 9. Every number (value) represents
with 0,1,2,3,4,5,6, 7,8 and 9 in this number system. The base of decimal number system is 10,
because it has only 10 digits.

HEXADECIMAL NUMBER SYSTEM

A Hexadecimal number system has sixteen (16) alphanumeric values from 0 to 9 and A to F.
Every number (value) represents with 0,1,2,3,4,5,6, 7,8,9,A,B,C,D,E and F in this number
system. The base of hexadecimal number system is 16, because it has 16 alphanumeric values.
Here A is 10, B is 11, C is 12, D is 14, E is 15 and F is 16.

Number system Base(Radix) Used digits Example


Binary 2 0,1 (11110000)2
Octal 8 0,1,2,3,4,5,6,7 (360)8
Decimal 10 0,1,2,3,4,5,6,7,8,9 (240)10
0,1,2,3,4,5,6,7,8,9,
Hexadecimal 16 (F0)16
A,B,C,D,E,F

CONVERSIONS
DECIMAL TO OTHER

1. DECIMAL TO BINARY

Decimal Number System to Other Base

To convert Number system from Decimal Number System to Any Other Base is quite easy;
you have to follow just two steps:
A) Divide the Number (Decimal Number) by the base of target base system (in which you want
to convert the number: Binary (2), octal (8) and Hexadecimal (16)).
B) Write the remainder from step 1 as a Least Signification Bit (LSB) to Step last as a Most
Significant Bit (MSB).

Decimal to Binary Conversion Result


Decimal Number is : (12345)10

Binary Number is
(11000000111001)2

2. DECIMAL TO OCTAL

Decimal to Octal Conversion Result


Decimal Number is : (12345)10

Octal Number is
(30071)8

3. DECIMAL TO HEXADECIMAL
Decimal to Hexadecimal Conversion Result
Example 1
Decimal Number is : (12345)10

Hexadecimal Number is
(3039)16

Example 2
Decimal Number is : (725)10 Hexadecimal Number is
(2D5)16
Convert
10, 11, 12, 13, 14, 15
to its equivalent...
A, B, C, D, E, F

BINARY TO OTHER
A) Multiply the digit with 2(with place value exponent). Eventually add all the multiplication
becomes the Decimal number.
1. BINARY TO DECIMAL

2. BINARY TO OCTAL

An easy way to convert from binary to octal is to group binary digits into sets of three, starting
with the least significant (rightmost) digits.

Binary: 11100101 = 11 100 101


011 100 101 Pad the most significant digits with zeros if
necessary to complete a group of three.

Then, look up each group in a table:


Binary: 000 001 010 011 100 101 110 111
Octal: 0 1 2 3 4 5 6 7

Binary = 011 100 101


Octal = 3 4 5 = 345 oct

3. BINARY TO HEXADECIMAL

An equally easy way to convert from binary to hexadecimal is to group binary digits into sets of
four, starting with the least significant (rightmost) digits.

Binary: 11100101 = 1110 0101

Then, look up each group in a table:


Binary: 0000 0001 0010 0011 0100 0101 0110 0111
Hexadecimal: 0 1 2 3 4 5 6 7
Binary: 1000 1001 1010 1011 1100 1101 1110 1111
Hexadecimal: 8 9 A B C D E F

Binary = 1110 0101


Hexadecimal = E 5 = E5 hex

OCTAL TO OTHER

1. OCTAL TO BINARY

Converting from octal to binary is as easy as converting from binary to octal. Simply look up
each octal digit to obtain the equivalent group of three binary digits.

Octal: 0 1 2 3 4 5 6 7
Binary: 000 001 010 011 100 101 110 111

Octal = 3 4 5
Binary = 011 100 101 = 011100101 binary

2. OCTAL TO HEXADECIMAL

When converting from octal to hexadecimal, it is often easier to first convert the octal number
into binary and then from binary into hexadecimal. For example, to convert 345 octal into hex:

(from the previous example)


Octal = 3 4 5

Binary = 011 100 101 = 011100101 binary


Drop any leading zeros or pad with leading zeros to get groups of four binary digits (bits):
Binary 011100101 = 1110 0101

Then, look up the groups in a table to convert to hexadecimal digits.

Binary: 0000 0001 0010 0011 0100 0101 0110 0111

Hexadecimal: 0 1 2 3 4 5 6 7

Binary: 1000 1001 1010 1011 1100 1101 1110 1111

Hexadecimal: 8 9 A B C D E F

Binary = 1110 0101

Hexadecimal = E 5 = E5 hex

Therefore, through a two-step conversion process, octal 345 equals binary 011100101 equals
hexadecimal E5.

3. OCTAL TO DECIMAL

The conversion can also be performed in the conventional mathematical way, by showing each
digit place as an increasing power of 8.

345 octal = (3 * 82) + (4 * 81) + (5 * 80) = (3 * 64) + (4 * 8) + (5 * 1) = 229 decimal

OR

Converting octal to decimal can be done with repeated division.

1. Start the decimal result at 0.


2. Remove the most significant octal digit (leftmost) and add it to the result.
3. If all octal digits have been removed, you’re done. Stop.
4. Otherwise, multiply the result by 8.
5. Go to step 2.

Octal Digits Operation Decimal Result Operation Decimal Result


345 +3 3 ×8 24
45 +4 28 ×8 224
5 +5 229 done.
Hexadecimal ---->Binary----> Octal
Let's convert (ff)16 into Octal.
Step 1: Convert (ff)16 into Binary
In order to convert the hexadecimal number into binary, we need to express every hexadecimal value
using 4 binary bits.
Binary equivalent of f is (1111)2
= (ff)16
= (1111)(1111)
= (11111111)2
Step 2 : Convert (11111111)2 into Octal
In order to convert the binary number into octal, we need to group every 3 binary bits and calculate
the value[From left to right].
(11111111)2 in Octal
= (11111111)2
= (11)(111)(111)
= (377)8
3. HEXADECIMAL TO DECIMAL

1. Convert (2C7)16 to decimal number.

Solution:

In Hexadecimal Number System,

2=2

C = 12

7=7

Hence,

(2C7)16 = (2 * 16²) + (12 * 16¹) + (7 * 16⁰)

= (2 * 256) + (12 * 16) + (7 * 1)

= 512 + 192 + 7

= 711

Therefore, (2C7)16 = (711)10

2. Convert (1A7D)16 to decimal number

Solution:

In Hexadecimal Number System,

1=1

A = 10

7=7

D = 13

Hence,

(1A7D)16 = (1 * 16³) + (10 * 16²) + (7 * 16¹) + (13 * 16º)

= (1 * 4096) + (10 * 256) + (7 * 16) + (13 * 1)

= 4096 + 2560 + 112 + 13


= 6781

Therefore, (1A7D)16 = (6781)10

**********************************************

You might also like