Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
1. Programming in C - Introduction.pdf
1. Programming in C (CS-1001)
KALINGA INSTITUTE OF INDUSTRIAL
TECHNOLOGY
School of Computer Engineering
3 Credit
Strictly for internal circulation (within KIIT) and reference only. Not for outside circulation without permission
Lecture Note
Mr. Rajat Behera - Associate Professor
2. Outcome based Learning Objectives
School of Computer Engineering
Understand the basic terminology in C Programming
Write, Compile and debug programs written in C
Use different data types in a C program
Design programs using decision structure, loops and functions
Explain the difference between call by value and call by reference
Understand the dynamic of memory by using pointers
Create/update basic data files
By the end of this course, students will be able to
2
3. Course Contents
School of Computer Engineering
Sr # Major and Detailed Coverage Area Hrs
1 Introduction 3
Introduction to computer and it‟s organization
Computer hardware and memory
Introduction to programming (High level/low level languages, procedural/structural programming)
Introduction to Flowchart & Algorithm
Number system representation
2 Variables, constants, Data types, Operators 3
Types of variables and constants
Console input/output operations (library functions)
Operators
Type casting
3 Control statements 5
Decision control and branching statements (if, nested if and switch case statements, etc.)
Looping control and their types (while, do-while, for, etc.)
break and continue statements
3
4. Course Contents cont…
School of Computer Engineering
Sr # Major and Detailed Coverage Area Hrs
4 Arrays 4
Introduction to Array
Single Dimensional Array
Multidimensional array
Mid Semester
5 Functions 4
Library & User defined Functions, Formal and Actual parameters
Declaring, defining and calling functions
Parameter Passing – call-by-value and call-by-reference, Recursion
6 Storage Classes 2
Different types of storage classes – (auto, static, extern, register)
7 Strings 2
Character Arrays and Strings, String Manipulation
8 Pointers 3
Pointer variable, Pointer Arithmetic, Passing parameters by reference, Pointer to pointer, Pointer to
functions
4
5. Course Contents cont…
School of Computer Engineering
Sr # Major and Detailed Coverage Area Hrs
9 User Defined Data Types – Structures and Unions 3
Structure: definition, structure variable, creation, initialization and assignment
Pointers to structures
Union and their uses
Enum and their uses
10 Dynamic Memory Allocation 2
Memory allocation functions (malloc, calloc, realloc, etc.)
Memory de-allocation function (free)
11 File Handling 2
File operations - opening, closing, reading, writing etc.
12 Additional Features 3
Command line arguments
Bitwise operators
Macros
End Semester
5
6. Recommended Books
School of Computer Engineering
Textbook
Computer fundamentals and programming in C – Pradip Dey & Manas Ghosh,
Second Edition 2013, OXFORD University Press
Programming in C – Bryon Gottfried, Third Edition. 2010, TMH
The ‘C’ programming language – Ritchim Kenighan, Second Edition, 2012 D.M.
Ritche, PHI
Programming in ANSI C – E. Balaguruswami, Sixth Edition, TMH
C The Complete Reference – H. Sohildt, Fourth Edition, 2000 TMH
Let us C – Y. Kanetkar, Twelfth Edition, 2012 BPB Publications
Computer Science – A Structured Programming Approach using C – B.A.
Forouzan & R.F Gillberg,, Third Edition, 2007, Cengage Learning
Reference Books
6
7. Evaluation and Prerequisites
School of Computer Engineering
Assignments/class tests/quizzes = 15 marks
Midterm exam - 25 marks
End term exam - 60 marks
Grades will be based on
Prerequisites
NIL O – greater than or equal to 90
E - greater than or equal to 80
A – greater than or equal to 70
B - greater than or equal to 60
Grades
C - greater than or equal to 50
D – greater than or equal to 40
F – Less than 40
Pass mark
Pass mark – 40
7
8. What is a computer?
School of Computer Engineering
A computer is an electronic
device, operating under the
control of instructions (i.e.
software) stored in its own
memory unit, that can:
accept data (input)
manipulate data (process)
produce results (output)
store the results (storage)
Computer system is used to
describe a collection of devices
that function together as a system.
8
10. Data and Information
School of Computer Engineering
All computer processing requires data, which is a collection of
raw facts, figures and symbols, such as numbers, words,
images, video and sound, given to the computer during the
input phase.
Computers manipulate data to create information. Information
is data that is organized, meaningful, and useful.
During the output Phase, the information that has been
created is put into some form, such as a printed report.
The information can also be put in computer storage for future
use.
10
11. Computer system organization
School of Computer Engineering
11
Memory
Output
Devices
Input
Devices
Storage
Devices
Processor
Instructions, data &Information
Instructions, data &Information
data Information
12. Input Device
School of Computer Engineering
An input device is any hardware component
that allows you to enter data and
instructions into a computer.
A keyboard is an input device that
contains keys users press to enter data
and instructions into the computer
A mouse is a pointing device that fits
comfortably under the palm of your hand
Most notebook computers have a
touchpad, a small, flat, rectangular
pointing device
12
14. Output Devices
School of Computer Engineering
Output devices are hardware components that convey information to one or
more people. A printer is an output device that produces text and graphics
on a physical medium such as paper
Ink-jet printer
Photo printer
Laser printer
Multifunction peripheral (all-in-one device)
14
15. Display Device
School of Computer Engineering
A display device is an output device that visually conveys text, graphics,
and video information.
LCD monitors use a liquid crystal display to produce images on the screen
15
16. Storage Devices
School of Computer Engineering
A storage device is the computer
hardware that records and/or
retrieves items to and from
storage media.
A hard disk is a storage device
that contains one or more
inflexible, circular platters that
use magnetic particles to store
data, instructions, and
information in tracks and sectors
16
17. Storage Devices cont…
School of Computer Engineering
An external hard disk is a
separate freestanding hard disk
that connects with a cable to a
port on the system unit and
communicates
A removable hard disk is a
hard disk that you insert and
remove from a drive.
17
18. Storage Devices cont…
School of Computer Engineering
Flash memory is a type of memory
that can be erased electronically and
rewritten
A memory card is a removable flash
memory device, usually no bigger than
1.5 inches in height or width, that you
insert and remove from a slot in a
computer, mobile device, or card
reader/writer
18
19. Storage Devices
School of Computer Engineering
A USB flash drive, sometimes called a thumb
drive, is a flash memory storage device that plugs
into a USB port on a computer or mobile device
19
20. Communication Devices
School of Computer Engineering
A communications device is a hardware
component that enables a computer to send
(transmit) and receive data, instructions, and
information to and from one or more computers.
Widely used communication devices are modem
and router
20
Modem Router
21. Software
School of Computer Engineering
Software consists of a series of instructions, organized for a
common purpose, that tells the computer hardware what tasks to
perform and how to perform them. This set of instructions is
sequenced and organized in a computer program. Therefore, a
program is a series of instructions which is intended to direct a
computer to perform certain functions and executed by the
processors.
Software can be described as a set of related programs and it is
more than a collection of programs.
So in broader sense
21
22. Software cont…
School of Computer Engineering
Software
System software is designed to
facilitate and coordinate the use
of the computer by making
hardware operational. It
interacts with the computer
system at low level. Examples
are operating system, loader,
linker etc
Application software is
designed to perform specific
usages of the users. Examples
are Microsoft Word, Microsoft
Excel, Microsoft Power Point,
Microsoft Access, Page Maker,
Coral Draw, Photoshop, Tally,
AutoCAD, Acrobat, Win Amp,
Micro Media Flash, iLeap, Xing
MP3 Player etc
22
23. Hardware
School of Computer Engineering
Hardware is the physical components of a computer that
includes all mechanical, electrical, electronic and magnetic
parts attached to it.
A computer consists of the following major hardware
components:
1. I/O i.e. Input/output devices
2. CPU i.e. central processing unit
3. Memory unit and storage devices
4. Interface unit
23
24. 1. Hardware - Input devices
School of Computer Engineering
The data and instructions are typed, submitted, Or transmitted
to a computer through input devices. Most common input
devices are :
Keyboard
Mouse
Scanner
Touchpad
24
25. 1. Hardware - Output devices
School of Computer Engineering
Output devices display the output results of the operations on
the input data or to print the data. Most common output devices
are :
Monitor
Printer
25
26. 2. Hardware - CPU
School of Computer Engineering
CPU can be thought of as the brain of the computer where most of the
processing takes place. During processing, it locates and executes the program
instructions and fetches data from memory and input/output devices and sends
back the computed data. It is divided into following functional units
Registers - These are high-speed storage devices. In most CPUs, some
registers are reserved for special purposes.
ALU (Arithmetic logic unit) - part of the CPU that performs arithmetic
operations, such as addition and subtraction as well as logical operations,
such as comparing two numbers to see if they are the equal or greater or
less.
CU (Control unit) - coordinates the processing by controlling the transfer of
data and instructions between main memory and the registers in the CPU.
26
27. 3. Hardware - Memory Unit
School of Computer Engineering
Components such as the I/O devices and CPU are not sufficient for the
working of the computer and hence the storage area is needed to store
instructions and data either temporarily or permanently so that
subsequent retrieval of the instructions and data can be possible on
demand. Data are stored in the memory as binary digits, called bits. Data
of various types are encoded as series of bits and stored in consecutive
memory locations. Each memory location is comprises of a single byte
which is equals to 8 bits and has a unique address so that the contents of
the desired memory locations can be accessed independently by
referring to its address. A single data item is stored in one or more
consecutive bytes of memory. The address of the first byte is used as the
address of the entire memory location.
27
28. Unit of memory
School of Computer Engineering
28
Sr No Unit Description
1 Bit 0 or 1
2 8 bit 1 Byte
3 1024 byte 1 Kilobyte (1 KB)
4 1024 Kilobytes 1 Megabyte (1 MB)
5 1024 Megabytes 1 Gigabyte (1 GB)
6 1024 Gigabytes 1 Terabyte (1 TB)
7 1024 Terabytes 1 Petabyte (1 PB)
8 1024 Petabytes 1 Exabyte (1 EB)
9 1024 Exabytes 1 Zettabyte (1 ZB)
10 1024 Zettabytes 1 Yottabyte (1 YB)
29. 4. Hardware - Interface Unit
School of Computer Engineering
The interface unit interconnects the CPU with memory and also with the
various input/output (I/O) devices and hence the instructions and data
move between the CPU and other hardware components through it. It is
a set of parallel wires or lines which connects all the internal computer
components to the CPU and main memory. Depending on the type of
data transmitted, a bus can be classified into the following three types:
Data bus : The bus used to carry actual data
Address bus memory or Input/output device : Addresses travel
via the address bus.
Control bus: This bus carries control information between the CPU
and other devices within the computer.
29
30. Control Bus
Bus based computer organization
School of Computer Engineering
30
CPU Memory I/O
Control Bus
Address Bus
Data Bus
System
Bus
31. Computer Memory
School of Computer Engineering
Primary memory – It is the
area in a computer where data
and programs are stored while
the program is being executed
along with the data. This is also
known as main memory &
forms the working area of the
program and is accessed
directly by the processor.
Secondary memory – It is the area
in the computer where programs
and data are kept on a long-term
basis. Common secondary storage
devices are the hard disk and
optical disks. The hard disk has
enormous storage capacity
compared to main memory. It is
also known as auxiliary memory
that stores huge volume of data at a
less cost than primary memory
devices
31
32. Computer Memory cont…
School of Computer Engineering
Cache – Cache memory is a small-sized type of volatile computer
memory that provides high-speed data access to a processor and
stores frequently used computer programs, applications and
data. It stores and retains data only until a computer is powered
up.
Registers - A special, high-speed storage area within the CPU. All
data must be represented in a register before it can be processed.
For example, if two numbers are to be multiplied, both numbers
must be in registers, and the result is also placed in a register.
32
33. Memory hierarchy
School of Computer Engineering
33
Secondary Memory
Main Memory
Cache
Registers
Speed
Cost
34. Layered view of the Computer
School of Computer Engineering
Machine with all its hardware
System Software
Compilers, Interpreters, Preprocessors,
Operating System, Device Drivers
etc.
Application Software
Browsing software, multimedia,
Word processors, Spreadsheets,
Database Software, IDEs etc…
34
35. Operating System (OS)
School of Computer Engineering
Provides several essential services:
Loading & running application programs
Allocating memory & processor time
Providing input & output facilities
Managing files of information
35
36. Programming Languages
School of Computer Engineering
Programs are written in programming languages
(Abbreviation PL). A PL is
A special purpose language
A set of rules and symbols used to construct a
computer program
A language used to interact with the computer
36
37. Computer Languages
School of Computer Engineering
Machine Language
Uses binary code
Machine-dependent
Not portable
Assembly Language
Uses mnemonics
Machine-dependent
Not usually portable
High Level Language
Uses English-like language
Machine independent
Portable (but must be compiled for different platforms)
Examples: Pascal, C, C++, Java, Fortran, . . .
Low Level Language
37
38. Procedural Language
The given problem is divided in to a number of sub problems
depending upon its functionality.
The sub problems are called procedures or Methods.
Any procedure can be called at any point during the program
execution.
The program has global and local variables.
E.g. Fortran, COBOL
Structural Language
The program is divided into modules and the modules are then
divided into functions.
The usage of goto statement is removed or reduced.
Each module can work independent of one another.
E.g. Algol, Pascal, C
Programming Languages
School of Computer Engineering
38
39. Programming Languages cont…
School of Computer Engineering
Procedural Programming Structural Programming
39
Global Data
Main Program
Local Data
Function - 1
Local Data
Function - 2
Local Data
Function - 3
Local Data
Function - 4
Local Data
Global Data
Main Program
Module - 1 Module - 2
Function - 1
Local Data
Function - 2
Local Data
40. Algorithm
School of Computer Engineering
A step by step procedure for solving a particular problem is an Algorithm. To be an
algorithm, a set of rules must be unambiguous and have a clear stopping point.
Algorithms can be expressed in any language, from natural languages like English or
French to programming languages like C++, C. Following are the common ways of
representing algorithm i.e. Step-form, Pseudo code and Flowchart
40
Design an algorithm to add two
numbers and display the average
Step 1 − START
Step 2 − input x
Step 3 − input y
Step 4 − sum = x + y
Step 5 – average = sum / 2
Step 5 − display average
Step 6 − STOP
Example 1 Example 2
Design an algorithm to calculate the
simple interest using the formula
Simple interest = P*N* R/100
Step 1 − START
Step 2 − input P, N, R
Step 3 − SI = P*N* R/100
Step 4 − display SI
Step 6 − STOP
41. Algorithm Examples
School of Computer Engineering
Design an algorithm to find the largest
of three numbers X, Y,Z
Step 1 − START
Step 2 − input X, Y, Z
Step 3 − BIG = X
Step 4 − if (X > Y) then
BIG = X
else
BIG = Y
Step 5 – if (BIG > Z) then
BIG = Z
Step 5 − display BIG
Step 6 − STOP
41
Example 3 Example 4
Design an algorithm find the largest data
value of a set of given positive data values
Step 1 − START
Step 2 − input NUM
Step 3 – LARGE = NUM
Step 4 − While (NUM >=0)
if (NUM > LARGE) then
LARGE = NUM
input NUM
Step 5 – display “Largest Value is:”, LARGE
Step 6 − STOP
42. Algorithm Examples
School of Computer Engineering
Design an algorithm to test whether the given integer value is prime or not
Step 1 − START
Step 2 − input N
Step 3 – MAX = N/2
Step 4 – M = 2
Step 4 − While (M <= MAX)
if (N REMINDER M = 0) then
Goto Step 8
else
M = M +1
Step 6 − Display N “is a prime number”
Step 7 – Goto Step 9
Step 8 - Display N “is not a prime number”
Step 9 - Stop
42
Example 5
43. Algorithm Examples
School of Computer Engineering
Design an algorithm to find the factorial of a given number N
Step 1 − START
Step 2 − input N
Step 3 – I = 0
Step 4 – PROD = 1
Step 5 − While (I < N)
I = I + 1
PROD = PROD * I
Step 6 − Display “Factorial of” N “is” PROD
Step 7 - Stop
43
Example 6
44. Class Work (CW)
School of Computer Engineering
44
Design an algorithm to subtract two numbers
and display the result
CW 1
Design an algorithm to calculate & display the
area of a triangle
CW 2
Design an algorithm to calculate & display
the area of a circle
CW 3
Design an algorithm to calculate & display the
largest among 2 numbers
CW 4
Design an algorithm to calculate the simple
interest using the formula : Simple interest =
P*N* R/100.
CW 5
Design an algorithm to calculate & display the
largest among 3 numbers
CW 6
Design an algorithm to calculate & display the
largest of a set of given data values
CW 7
CW 8
Design an algorithm to add the integers from
1 to 100 and to display the sum.
CW 9
CW 10
Design an algorithm to find sum of given data
values until negative value is entered.
Design an algorithm to calculate & display
the factorial of a given number N
45. Flowchart
School of Computer Engineering
A flowchart is a graphical representation of an algorithm. These flowcharts play a vital
role in the programming of a problem and are quite helpful in understanding the logic
of complicated and lengthy problems. Once the flowchart is drawn, it becomes easy to
write the program in any high level language. Often we see how flowcharts are helpful
in explaining the program to others. Hence, it is correct to say that a flowchart is a must
for the better documentation of a complex program.
Standard
Symbols:
45
46. Flowchart cont…
School of Computer Engineering
Below is the flowchart to add two numbers and display the average
46
Start
Input
x
Input
y
sum = x + y
average = sum/2
Output
average
End
47. Flowchart Guidelines
School of Computer Engineering
In drawing a proper flowchart, all necessary requirements/steps should
be listed out in logical order.
The flowchart should be clear, neat and easy to follow. There should not be
any room for ambiguity in understanding the flowchart.
The usual direction of the flow of a procedure or system is from left to
right or top to bottom
Only one flow line should come out from a process symbol
Only one flow line should enter a decision symbol, but two or three flow
lines, one for each possible answer, should leave the decision symbol.
47
48. Flowchart Guidelines cont…
School of Computer Engineering
If the flowchart becomes complex, it is better to use connector symbols to
reduce the number of flow lines. Avoid the intersection of flow lines if you
want to make it more effective and better way of communication.
Ensure that the flowchart has a logical start and finish.
It is useful to test the validity of the flowchart by passing through it with a
simple test data.
48
49. Flowchart Example
School of Computer Engineering
Below is the flowchart to find the factorial of given positive integer N
49
Start
Input
N
I = 0
PROD = 1
Is I<N
Output
PROD End
I = I + 1
PROD = PROD * I
Yes
No
50. Flowchart Example cont…
School of Computer Engineering
Below is the flowchart to find the biggest of the three unequal positive
number
50
Start
Input
A,B,C
Is
A>B
Is
A>C
Is
B>C
Output
A
Output
C
Output
A
End
Yes No
Yes
Yes
No
No
51. Class Work (CW)
School of Computer Engineering
51
Draw a flowchart to subtract two numbers
and display the result
CW 1
Draw a flowchart to calculate & display the
area of a triangle
CW 2
Draw a flowchart to calculate & display the
area of a circle
CW 3
Draw a flowchart to calculate & display the
largest among 2 numbers
CW 4
Draw a flowchart to calculate the simple
interest using the formula : Simple interest =
P*N* R/100.
CW 5
Draw a flowchart to calculate & display the
largest among 3 numbers
CW 6
Draw a flowchart to calculate & display the
largest of a set of given data values
CW 7
CW 8
Draw a flowchart to add the integers from 1
to 100 and to display the sum.
CW 9
CW 10
Draw a flowchart to find sum of given data
values until negative value is entered.
Draw a flowchart to calculate & display the
factorial of a given number N
52. Pseudo code
School of Computer Engineering
The Pseudo code is neither an algorithm nor a program. It is an abstract form of a program. It consists of English
like statements which perform the specific operations. It employs programming-like statements to depict the
algorithm and no standard format (language independent) is followed. The statements are carried out in a order
& followings are the commonly used statements.
52
Statement Purpose General Format
Input Get Information INPUT: Name of variable
e.g. INPUT: user_name
Process Perform an atomic activity Variable arithmetic expression
e.g. x 2 or x x + 1 or a b * c
Decision Choose between different alternatives
IF (condition is met) then
statement(s)
ENDIF
IF (condition is met) THEN
statement(s)
ELSE
statements(s)
ENDIF
e.g.
IF (amount < 100) THEN
interestRate .06
ELSE
interest Rate .10
ENDIF
e.g.
IF (amount < 100) THEN
interestRate .06
ENDIF
53. Pseudo code cont…
School of Computer Engineering
53
Statement Purpose General Format
Repetition Perform a step multiple times
Output Display information OUTPUT: Name of variable OUTPUT: message
e.g. OUTPUT: user_name OUTPUT: „Credit Limit‟
limit
REPEAT
statement(s)
UNTIL (condition is met)
WHILE (condition is met)
statement(s)
ENDWHILE
e.g.
count 0
WHILE (count < 10)
ADD 1 to count
OUTPUT: count
ENDWHILE
OUTPUT: “The End”
e.g.
count 0
REPEAT
ADD 1 to count
OUTPUT: count
UNTIL (count < 10)
OUTPUT: “The End”
54. Pseudo code Guidelines
School of Computer Engineering
54
Guidelines Explanation
Write only one statement per line Each statement in pseudo code should express just one action for the computer. If the task list is properly
drawn, then in most cases each task will correspond to one line of pseudo code.
Capitalize initial keyword In the example above note the words: INPUT and OUTPUT. These are just a few of the keywords to use,
others include: IF, ELSE, REPEAT, WHILE, UNTIL, ENDIF
Indent to show hierarchy Each design structure uses a particular indentation pattern.
Sequence - Keep statements in sequence all starting in the same column
Selection - Indent statements that fall inside selection structure, but not the keywords that form the
selection
Loop - Indent statements that fall inside the loop but not keywords that form the loop
Task List
Read name, hours worked, rate of pay
gross = hours worked * rate of pay
Write name, hours worked, gross
Pseudo code
INPUT: name, hoursWorked, payRate
gross hoursWorked * payRate
OUTPUT: name, hoursWorked, gross
INPUT: name, grossPay, taxes
IF (taxes > 0)
net grossPay – taxes
ELSE
net grossPay
ENDIF
OUTPUT: name, net
55. Pseudo code Guidelines cont…
School of Computer Engineering
55
Guidelines Explanation
End multiline structures
Watch the IF/ELSE/ENDIF as constructed above, the ENDIF is in line with the IF. The same
applies for WHILE/ENDWHILE etc…
Keep statements language
independent
Resist the urge to write in whatever language you are most comfortable with, in the long
run you will save time. Remember you are describing a logic plan to develop a program,
you are not programming!
INPUT: name, grossPay, taxes
IF (taxes > 0)
net grossPay – taxes
ELSE
net grossPay
ENDIF
OUTPUT: name, net
56. Pseudo code Example
School of Computer Engineering
1. Problem - Design the pseudo code to add two numbers and display the average.
INPUT: x, y
sum x + y
average sum / 2
OUTPUT: ‘Average is:’ average
2. Problem - Design the pseudo code to calculate & display the area of a circle
INPUT: radius
area 3.14 * radius * radius
OUTPUT: ‘Area of the circle is ‘ area
2. Problem - Design the pseudo code to calculate & display the largest among 2 numbers
INPUT: num1, num2
max num1
IF (num2 > num 1) THEN
max num2
ENDIF
OUTPUT: ‘Largest among 2 numbers is’ max
56
57. Class Work (CW)
School of Computer Engineering
57
Write the pseudo code to subtract two
numbers and display the result
CW 1
Write the pseudo code to calculate &
display the area of a triangle
CW 2
Write the pseudo code to calculate &
display the area of a circle
CW 3
Write the pseudo code to calculate &
display the smallest among 2 numbers
CW 4
Write the pseudo code to calculate &
display the smallest of a set of given
positive data values
CW 6
CW 7
Write the pseudo code to add the
integers from 1 to 100 and to display
the sum.
CW 8
CW 9
Write the pseudo code to find sum of
given data values until negative value is
entered.
Write the pseudo code to test whether the
given integer value is prime or not
Write the pseudo code to calculate &
display the smallest among 3 numbers
CW 5
58. Number System
School of Computer Engineering
System Base Symbols
Used by
humans?
Used in
computers?
Decimal 10 0, 1, 2, … 9 Yes No
Binary 2 0, 1 No Yes
Octal 8 0, 1, 2, … 7 No No
Hexa-
decimal
16 0, 1, 2, … 9,
A, B, C, … F
No No
58
60. Binary to Decimal
School of Computer Engineering
Technique:
Multiply each bit by 2n,
where n is the “weight” of
the bit
The weight is the position
of the bit, starting from 0
on the right
Add the results
1010112 => 1 x 20 = 1
1 x 21 = 2
0 x 22 = 0
1 x 23 = 8
0 x 24 = 0
1 x 25 = 32
4310
Bit “0”
60
61. Decimal to Binary
School of Computer Engineering
Technique:
Continuously divide by
two to give a result and
keep track of the reminder
until the final result equals
to 0
12510 = ?2
2 125
62 1
2
31 0
2
15 1
2
7 1
2
3 1
2
1 1
2
0 1
12510 = 11111012
61
62. Octal to Decimal
School of Computer Engineering
62
7248 => 4 x 80 = 4
2 x 81 = 16
7 x 82 = 448
46810
Technique:
Multiply each bit by 8n,
where n is the “weight” of
the bit
The weight is the position
of the bit, starting from 0
on the right
Add the results
63. Hexadecimal to Decimal
School of Computer Engineering
63
ABC16 => C x 160 = 12 x 1 = 12
B x 161 = 11 x 16 = 176
A x 162 = 10 x 256 = 2560
274810
Technique:
Multiply each bit
by 16n, where n is
the “weight” of
the bit
The weight is the
position of the
bit, starting from
0 on the right
Add the results
64. Octal to Binary
School of Computer Engineering
64
Technique:
Convert each octal
digit to a 3-bit
equivalent binary
representation
7058 = ?2
7 0 5
111 000 101
7058 = 1110001012
65. Hexadecimal to Binary
School of Computer Engineering
65
Technique:
Convert each octal digit
to a 4-bit equivalent
binary representation
10AF16 = ?2
10AF16 = 00010000101011112
1 0 A F
0001 0000 1010 1111
67. Home Work (HW)
A company insures its drivers in the following cases:
If the driver is married.
If the driver is unmarried, male & above 30 years of age.
If the driver is unmarried, female & above 25 years of age.
In all other cases the driver is not insured. If the marital status, sex and age of
the driver are the inputs, write an algorithm (in step-form) to determine
whether the driver is to be insured or not.
If a five-digit number is input, write an algorithm (in step-form) and also
draw a flowchart to calculate the sum of its digits. For example if the number
that is input is 12391 then the output should be displayed as 16
If a five-digit number is input, write an algorithm (in step-form) and also
draw a flowchart to reverse the number. For example if the number that is
input is 12392 then the output should be displayed as 29321
67
School of Computer Engineering
68. Perform the following conversions
102 = ?8
190.5610 = ?8
15016 = ?8
290.A16 = ?2
70.58 = ?16
11110.112 = ?16
91010 = ?16
980.2310 = ?2
298010 = ?2
280210 = ?10
68
School of Computer Engineering
Home Work (HW)
69. Home Work (HW)
Draw a flowchart as per the following table. Input is gender, years of service &
qualifications
The marks obtained by a student in 5 different subjects are input through the
keyboard. The student gets a division as per the following rules:
Percentage above or equal to 60 - First division
Percentage between 50 and 59 - Second division
Percentage between 40 and 49 - Third division
Percentage less than 40 – Fail
Write an algorithm (in step-form) to calculate the division obtained by the student.
69
School of Computer Engineering