Sie sind auf Seite 1von 70

Website : www.aceec.ac.

in

ACE
Engineering College
Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
NAAC Accridated with ‘A’ Grade

Department of
Computer Science and Engineering

Programming for Problem Solving


I Year B.Tech. (I - Sem)

Prepared by :
Ms. Shubhangi Mahule

Assistant Professor (M.Tech)

in association with ACE Engineering Academy

ACE is the leading institute for coaching in ESE, GATE & PSUs
H O: Sree Sindhi Guru Sangat Sabha Association, # 4-1-1236/1/A, King Koti, Abids, Hyderabad-500001.
Ph: 040-23234418 / 19 / 20 / 21, 040 - 24750437

7 All India 1st Ranks in ESE


43 All India 1st Ranks in GATE
Unit 1 Programming for Problem Solving
Department of Computer Science and Engineering For Micro Notes by the
ACE ENGINEERING COLLEGE Student
(NBA Accredited B.Tech Courses: EEE, ECE, CSE)
(Affiliated to Jawaharlal Nehru Technological University, Hyderabad, Telangana)
Ankushapur (V), Ghatkesar (M), R.R.Dist - 501 301

UNIT – I

1. Introduction to Computers: 2: Computer System


3. Computing Environment 4. Computer Languages
5. Creating and running program 6. Compiler
7. Error in C 8. Algorithm and Flowchart
9. Introduction to C 10. Data types
11. Variable and Constant 12. C input output function
13. Operators 14. Type Conversion
15. Decision making and Control Statement 16. Other Statement
17. Storage Classes 18. Number System

Introduction to Computers:

Computer Systems:

• Computer is a machine made of Electronic devices (switch, capacitor,


transistor which known 0’s and 1’s or on and off) that enable user to enter
data (Input), process it (CPU), and store it in a computer memory.
• A computer is a system made of two major components:
• Hardware and software.
• Hardware is the physical equipment which can be touch and feel.
• Software is the collection of programs (instructions) that perform specific
task and allow the hardware to do its job.

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
3 Computer Science Engg

For Micro Notes by the


Student

Fig1: Different types of computer component


Computer Hardware

• The hardware component of the computer system consists of five parts:


• Input devices,
• Central processing unit (CPU)
• Primary storage,
• Output devices
• Auxiliary storage devices.

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
4 Computer Science Engg

Input device: For Micro Notes by the


Data or instruction are entered into the computer with the help of input devices. Student

Ex: keyboard, Mouse, Scanner.

Central processing unit (CPU) :


CPU is a computer brain use to perform calculation and other operation.

Output device :
The result given by the computer after processing data is called as output. the
output device shows or plays the result after the input has been proceed.
Ex: Monitor, Printer, Speaker are output devices .

Computer Memory:
Computer memory is any physical device capable of storing information
temporarily or permanently.

Fig2: Types of Memory

Primary Memory
• Primary memory is computer memory that a computer accesses directly.
• Primary memory is a volatile storage mechanism.
• It may be random access memory (RAM), cache memory or data buses.
• primarily associated with RAM.
• Primary memory is considered faster than secondary memory

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
5 Computer Science Engg

For Micro Notes by the


Student

Fig3: Ram

Secondary Storage and Disk


• Secondary storage devices are those devices whose memory is non
volatile, meaning, the stored data will be in computer even if the system is
turned off.
• Here are a few secondary storage. Hard Disk, CD RAM,DVD RAM, Pen Drive
• Secondary storage is also called auxiliary storage.
• Secondary storage is less expensive when compared to primary memory
like RAMs.
• The speed of the secondary storage is also lesser than that of primary
storage.
• Hence, the data which is less frequently accessed is kept in the secondary
storage.

Processor
• A processor is an integrated electronic circuit that performs the calculations

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
6 Computer Science Engg

that run a computer. For Micro Notes by the


Student
• A processor performs arithmetical, logical, input/output (I/O) and other
basic instructions that are passed from an operating system (OS).

Computer Software

• Computer software is divided in to two broad categories: system software


and application software.

System software:

• System software manages the computer resources .It provides the


interface between the hardware and the users.

• System software consists of programs that manage the hardware resources


of a computer and perform required information processing tasks.
• These programs are divided into three classes: the operating system,
system support, and system development.

• The operating system provides services such as a user interface, file and
database access, and interfaces to communication systems such as
Internet protocols.

• System support software provides system utilities and other operating


services. Examples of system utilities are sort programs and disk format
programs.

• The language translators converts user programs into machine language


for execution, debugging tools to ensure that the programs are error free
and computer –assisted software engineering (CASE) systems.

Application software
• Application software, on the other hand is directly responsible for helping
users solve their problems.

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
7 Computer Science Engg

For Micro Notes by the


Student
• Application software is classified into two classes:

• General-purpose software : is purchased from a software developer and


can be used for more than one application and application –specific
software.
Ex: MS-Office, Adobe Photoshop, Adobe Reader

• specific purpose software: use only for specific purpose.


Ex: Library software, Banking software, IRTC software.

Fig 4: The relationship between system and application software


Computing Environments:

• In the early days of computers, there was only one environment: the
main frame computer hidden in a central computing department. With
the advent of mini computers and personal computers, the environment
changed, resulting in computers on virtually every desktop.

1 Personal Computing Environment

• In 1971, Marcian E.Hoff, working for Intel, combined the basic elements of
the central processing unit into the microprocessor.
• The first computer on a chip was the Intel 4004. If we are using a personal
computer, all of the computer hardware components are tied together in
our personal computer (PC).

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
8 Computer Science Engg

For Micro Notes by the


Student

2 Time-Sharing Environment:

• Employees in large companies often work in time-sharing environment.


• Many users are connected to one or more computers.
• These computers may be minicomputers or central mainframes.
• The terminals they use are often nonprogrammable.
• In the time-sharing the output devices and auxiliary storage devices are
shared by all of the users.
• A typical college lab in which a minicomputer is shared is shared by many
students is shown in figure:

In the time-sharing environment, all computing must be done by the central


computer. The central computer has many duties: It must control the shared
resources; it must manage the shared data and printing and it must do the
computing.

3 Client/Server Environment

• In the client-server environment, the users’ micro computers or workstations


are called the client.
• The central computer, which may be a powerful microcomputer,
minicomputer, or central mainframe system, is known as the server.
• work is shared between the users’ computers and the central computer,
hence response time and monitor display are faster and the users are more

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
9 Computer Science Engg

productive as compare to Time sharing environment. For Micro Notes by the


Student

4 Distributed Computing

• Distributed Computing environment provides a seamless integration of


computing functions between different servers and client’s .
• The internet provides connectivity to different servers throughout the world.
• For example eBay uses several computers to provide its auction services.
This environment provides a reliable, scalable, and highly available
network.

Computer Languages:

• To write a program for a computer, we must use a computer language. Over


the years computer languages have evolved from machine languages to
natural languages.
1940’s Machine level Languages
1950’s Symbolic Languages
1960’s High-Level Languages

Machine Languages

• In the earliest days of computers, the only programming languages

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
10 Computer Science Engg

available were machine languages. Each computer has its own machine For Micro Notes by the
language, which is made of streams of 0’s and 1’s. Student

• Instructions in machine language must be in streams of 0’s and 1’s because


the internal circuits of a computer are made of switches transistors and
other electronic devices that can be in one of two states: off or on. The off
state is represented by 0 , the on state is represented by 1.

The only language understood by computer hardware is machine language.

Symbolic Languages:

• In early 1950’s Admiral Grace Hopper, A mathematician and naval officer


developed the concept of a special computer program that would
convert programs into machine language.

• The early programming languages simply mirror to the machine languages


using symbols of mnemonics to represent the various machine language
instructions because they used symbols, these languages were known as
symbolic languages.

• Computer does not understand symbolic language it must be translated


to the machine language.

• A special program called assembler translates symbolic code into


machine language. Because symbolic languages had to be assembled
into machine language they soon became known as assembly languages.

• Symbolic language uses symbols or mnemonics to represent the various,


machine language instructions.

High Level Languages:

• High level languages are portable to many different computers, allowing


the programmer to concentrate on the application problem at hand
rather than the intricacies of the computer.
• High-level languages are designed to relieve the programmer from the
details of the assembly language.

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
11 Computer Science Engg

• High level languages share one thing with symbolic languages; they must For Micro Notes by the
be converted into machine language. Student
• The process of converting them is known as compilation.

The first widely used high-level languages, FORTRAN (FORmula TRANslation) was
created by John Backus and an IBM team in 1957;it is still widely used today in
scientific and engineering applications. After FORTRAN was COBOL (Common
Business-Oriented Language). Admiral Hopper was played a key role in the
development of the COBOL Business language. C is a high-level language used
for system software and new application code.

Creating and Running Programs:

Computer hardware understands a program only if it is coded in its machine


language. It is the job of the programmer to write and test the program .There
are four steps in this process:

1. Writing and Editing the program


2. Compiling the program
3. Linking the program with the required library modules
4. Executing the program.

Writing and Editing Programs


• The software used to write programs is known as a text editor.
• A text editor helps user to enter, change, and store character data.

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
12 Computer Science Engg

• The main difference between text processing and program writing For Micro Notes by the
is that programs are written using lines of code, while most text Student
processing is done with character and lines.
• After writing a program, we save our file to disk. This file will be input to
the compiler; it is known as a source file.

Compiling Programs:
• The code in a source file stored on the disk must be translated into
machine language; this is the job of the compiler.
• The ‘c’ compiler is two separate programs. The preprocessor and the
translator.
• The code generated after compilation is called object code.

The preprocessor reads the source code and prepares it for the translator.
While preparing the code, it scans for special instructions known as
preprocessor commands.

After the preprocessor has prepared the code for compilation, the
translator convert the program into machine language and generate the
object code that is,not executable because it does not have the required
C and other functions included.
Linking Programs:

• A C program is made up of many functions.


• Function can be user defined or predefined
• Predefined function, such as input/output function and mathematical
library functions that exist elsewhere and must be attached to our
program.
• The linker assembles all of these functions code to object code and
then generate final executable program.

Executing Programs:

• Once program has been linked, it is ready for execution.


• To execute a program we use an operating system command, such
as run,
• OS use the program called loader to load program from secondary
memory into primary memory to execute the program

In a typical program execution, it reads data for processing, either from the user

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
13 Computer Science Engg

or from a file. After the program processes the data, it prepares the output. For Micro Notes by the
At output can be to the user’s monitor or to a file. When the program has Student

finished its job, it tells the operating system, which then removes the program
from memory.

Interpreter Vs Compiler
Interpreter Compiler
Translates program one statement Scans the entire program and
at a time. translates it as a whole into
machine code.
It takes less amount of time to It takes large amount of time to
analyze the source code but the analyze the source code but
overall execution time is slower. the overall execution time is
comparatively faster.
No intermediate object code is Generates intermediate object
generated, hence are memory code which further requires linking,
efficient. hence requires more memory.
Continues translating the program It generates the error message only
until the first error is met, in which after scanning the whole program.
case it stops. Hence debugging is Hence debugging is comparatively
easy. hard.
Programming language like Programming language like C,
Python, Ruby use interpreters. C++ use compilers.

Errors in C
• Error is an illegal operation performed by the user which results in abnormal
working of the program.
• Programming errors often remain undetected until the program is
compiled or executed.
• Some of the errors restrict the program from getting compiled or executed.
Thus errors should be removed before compiling and executing.
Types of Error
1. Syntax errors:
• Errors that occur when you violate the rules of writing C syntax are known
as syntax errors.
• This compiler error indicates something that must be fixed before the
code can be compiled.
• All these errors are detected by compiler and thus are known as compile-
time errors.

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
14 Computer Science Engg

• Most frequent syntax errors are: For Micro Notes by the


Student
Missing Parenthesis ( })
Printing the value of variable without declaring it
Missing semicolon like this:

2. Run-time Errors :
• Errors which occur during program execution(run-time) after successful
compilation are called run-time errors.
• One of the most common run-time error is division by zero also known as
Division error.
• These types of error are hard to find as the compiler doesn’t point to the
line at which the error occurs.

3. Logical Errors :
• On compilation and execution of a program, desired output is not
obtained when certain input values are given.
• These types of errors which provide incorrect output but appears to be
error free are called logical errors.
• These are one of the most common errors done by beginners of
programming.

ALGORITHM:
• An algorithm was developed by an Arab mathematician Abdullah
Muhammad bin Musa al-Khwarizmi.
• It is step-by-step approach to solve a given problem.
• Input: There are more quantities that are supplied.
• Output: At least one quantity is produced
• Definiteness: Each instruction of the algorithm should be clear and
unambiguous.
• Finiteness: The process should be terminated after a finite number of steps.
• Effectiveness: Every instruction must be basic enough to be carried out
theoretically or by using paper and pencil

Example: Algorithm/pseudo code to add two numbers

Step 1: Start
Step 2: Read the two numbers in to a,b
Step 3: c=a+b

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
15 Computer Science Engg

For Micro Notes by the


Student
Step 4: write/print c
Step 5: Stop.

Write an algorithm for converting temperature from degree Celsius to Fahrenheit

2. Write an algorithm for calculating Kinetic Energy. (E=/2 mv2 )

PSEUDOCODE:

• Pseudo code is an English like language.that helps programmers


develop algorithms.
• Pseudo code is a nonprogrammable.
• it is convenient and user friendly .

FLOW CHART:

A Flow chart is a Graphical representation of an Algorithm or a portion of an


Algorithm. Flow charts are drawn using certain special purpose

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
16 Computer Science Engg

For Micro Notes by the


Student
symbols such as Rectangles, Diamonds, Ovals and small circles. These symbols
are connected by arrows called flow lines.

(Or)

The diagrammatic representation of way to solve the given problem is


called flow chart.

The following are the most common symbols used in drawing flowcharts:

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
17 Computer Science Engg

For Micro Notes by the


Student
Flowchart for Adding two numbers.

start

Read A and B
value

C=A+B

Print C

Stop

1 Draw a flowchart for converting temperature from degree Celsius to Fahrenheit

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
18 Computer Science Engg

3. Draw a flowchart for calculating kinetic Energy. (E=1/2 mv2 ) For Micro Notes by the
Student

INTRODUCTION TO ‘C’ LANGUAGE:

• C language facilitates a very efficient approach to the development


and implementation of computer programs. The History of C started
in 1972 at the Bell Laboratories, USA where Dennis M. Ritchie proposed
this language.
• In 1983 the American National Standards Institute (ANSI) established
committee whose goal was to produce “an unambiguous and
machine independent definition of the language C “while still
retaining its spirit.
• C is the programming language most frequently associated with
UNIX. Since the 1970s, the bulk of the UNIX operating system and its
applications have been written in C.
• Because the C language does not directly rely on any specific
hardware architecture, UNIX was one of the first portable operating
systems. It appears that there will be yet another ANSI C standard
officially dated 1999 or in the early 2000 years; it is currently known as
“C9X.”

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
19 Computer Science Engg

Features of C programming language: For Micro Notes by the


Student

™™ Reliability
™™ Portability
™™ Flexibility
™™ Interactivity
™™ Modularity
™™ Efficiency and Effectiveness

Uses of C programming language:

The C programming language is used for developing system applications that


forms a major portion of operating systems such as Windows, UNIX and Linux.
Below are some examples of C being used.

™™ Database systems
™™ Graphics packages
™™ Word processors
™™ Spreadsheets
™™ Operating system development
™™ Compilers and Assemblers
™™ Network drivers
™™ Interpreters

Structure of C program

• Structure of C program is defined by set of rules called protocol, to be


followed by programmer while writing C program.
• All C programs are having sections/parts which are mentioned below.

¾¾ Documentation section
¾¾ Link Section
¾¾ Definition Section
¾¾ Global declaration section
¾¾ Function prototype declaration section
¾¾ Main function

User defined function definition section

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
20 Computer Science Engg

For Micro Notes by the


S.No Sections Description
Student
We can give comments about the
program, creation or modified date,
author name etc in this section. The
characters or words or anything which
are given between “/*” and “*/”,
1 Documentation section
won’t be considered by Ccompiler
for compilation process. These will
beignored by C compiler during
compilation.Example : /* comment line1
comment line2 comment3 */
Header files that are required to execute
2 Link Section
a C program are included in this section
In this section, variables are defined and
3 Definition Section
values areset to these variables.
Global variables are defined in this
section. When a variable is to be used
Global declaration
4 throughout the program, can be defined
section
in this section.

Function prototype gives many


information about a declaration section
5 Function Prototype
function like return type, parameter
names used
Every C program is started from main
function and this function contains
6 Main function
two major sections called declaration
section and executable section.
User can define their own functions in
User defined function
7 this section which perform particular
section
task as per the user requirement.

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
21 Computer Science Engg

Tokens in C For Micro Notes by the


Student

• A C program consists of various tokens and a token is a keyword, an


identifier, a constant, a string literal, or a symbol.

Identifiers

In C language identifiers are the names given to variables, constants, functions


and user-define data. This identifier is defined against a set of rules.

Rules for an Identifier

• An Identifier can only have alphanumeric characters (a-z, A-Z, 0-9)


and underscore (_).
• identifier should not start with a number and other special characters
(except underscore).
• Identifiers are also case sensitive in C. For example name and Name
are two different identifier in C.
• Keywords are not allowed to be used as Identifiers.
• No special characters are permitted, such (, , : ,. . %, #, ! )
• No space is allowed in between the Identifiers.
• Maximum number of characters allowed for an identifier is 63(31).

Keywords:
auto double int struct
break else long switch
case enum register typedef
char extern return union
continue for signed void
do if static while
default goto sizeof volatile
const float short unsigned

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
22 Computer Science Engg

Character set: In C language characters are grouped into the following For Micro Notes by the
Student
categories:

Letters (all alphabets a to z & A to Z).


Digits (all digits 0 to 9).
Special characters(such as colon:, semicolon ;, period. , underscore _,
ampersand & etc).

DATA TYPES:

Data types in c refer to an extensive system used for declaring variables or


functions of different types. The type of a variable determines how much space
it occupies in storage and how the bit pattern stored is interpreted.
The types in C can be classified as follows –
Basic Types: They are arithmetic types and are further classified into: (a) integer
types and (b) floating-point types

Primary Data type

Character Integer Float Boolean

Signed unsigned float


char
Int Int
Signed double
char Short Short Int
Int
Unsigned long double
Long Int Long Int
char

Enumerated types: They are again arithmetic types and they are used to define
variables that can only assign certain discrete integer values throughout the
program

The type void: The type specifier void indicates that no value is available.

Derived types : They include (a) Pointer types, (b) Array types, (c) Structure

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
23 Computer Science Engg

types, (d) Union types and (e) Function types. For Micro Notes by the
Student
storage
S.No C Data types Range
Size
1 char 1 –127 to 127
2 int 2 –32,767 to 32,767
1E–37 to 1E+37 with six digits of
3 float 4
precision
1E–37 to 1E+37 with ten digits of
4 double 8
precision
1E–37 to 1E+37 with ten digits of
5 long double 10
precision
6 long int 4 –2,147,483,647 to 2,147,483,647
7 short int 2 –32,767 to 32,767
unsigned 0 to 65,535
8 2
short int
signed short –32,767 to 32,767
9 2
int
10 long long int 8 –(2power(63) –1) to 2(power)63 –1
signed long –2,147,483,647 to 2,147,483,647
11 4
int
unsigned long 0 to 4,294,967,295
12 4
int
unsigned long 2(power)64 –1
13 8
long int

Void data type in C:


• Void is an empty data type that has no value.
• This can be used in functions and pointers.

Derived data type in C:


Array, pointer, structure and union are called derived data type in C language.

C – Variable
• C variable is a named location in a memory where a program can
manipulate the data. This location is used to hold the value of the
variable.
• The value of the C variable may get change in the program.
• C variable might be belonging to any of the data type like int, float,
char etc.

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
24 Computer Science Engg

Rules for naming C variable: For Micro Notes by the


Student
• Variable name must begin with letter or underscore.
• Variables are case sensitive
• They can be constructed with digits, letters.
• No special symbols are allowed other than underscore.
examples : sum, height, _value are some

Declaring & initializing C variable:


• Variables should be declared in the C program before to use.
• Memory space is not allocated for a variable while declaration. It
happens only on variable definition.
• Variable initialization means assigning a value to the variable.
S.No Type Syntax Example
data_type variable_ int x, y, z; char a;
1 Variable declaration
name; float ch;
data_type variable_ int x = 50, y = 30; char
name =value; flag = ‘x’,ch=’l’;
2 Variable initialization
float f=45.7;

There are three types of variables in C program They are,


Local variable
Global variable
Local variable in C:
• The scope of local variables will be within the function only.
• These variables are declared within the function and can’t be
accessed outside the function.
Global variable in C:
• The scope of global variables will be throughout the program. These
variables can be accessed from anywhere in the program.
• This variable is defined outside the main function. So that, this variable
is visible to main function and all other sub functions.

C – Constants
• C Constants are also like normal variables. But, only difference is, their
values cannot be modified by the program once they are defined.
• Constants refer to fixed values. They are also called as literals
• Constants may be belonging to any of the data type.

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
25 Computer Science Engg

Syntax: const data_type variable_name; (or) const data_type *variable_name; For Micro Notes by the
Student

Types of C constant:
S.no Constant type data type Example
1 int 53, 762, -478 etc
Integer constants
unsigned int 5000u, 1000U etc
long int 4,83,647
long long int
2,147,483,680
2 Real or Floating point float 10.456789
constants doule 600.1234568
3 Octal constant int 13 /* starts with 0 */
4 H e x a d e c i m a l int 0×90 /* starts with 0x */
constant
5 character constants char ‘A’ , ‘B’, ‘C’
6 string constants char “ABCD” , “Hai”

1. Example program using const keyword in C:

#include <stdio.h>
void main()

{
const int height = 100; /*int constant*/
const float number = 3.14;
const char letter = ‘A’; /*char constant*/
const char letter_sequence[10] = “ABC”; /*string constant*/
const char backslash_char = ‘\?’;
printf(“value of letter : %c \n”, letter );
printf(“value of letter_sequence : %s \n”, letter_sequence);
printf(“value of backslash_char : %c \n”, backslash_char);
}

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
26 Computer Science Engg

C Input and Output Functions For Micro Notes by the


Student
Input : In any programming language input means to feed some data into
program. This can be given in the form of file or from command line.

Output : In any programming language output means to display some


data on screen, printer or in any file.

• C programming treats all the devices as files. So devices such as the


display are addressed in the same way as files and the following three
files are automatically opened when a program executes to provide
access to the keyboard and screen.
Standard File File Pointer Device
Standard input stdin Keyboard
Standard output stdout Screen
Standard error stderr Your screen

The getchar() and putchar() Functions

• The int getchar(void) function reads the next available character


from the screen and returns it as an integer. This function reads only
single character at a time.

• The int putchar(int c) function puts the passed character on the


screen and returns the same character. This function puts only single
character at a time.

#include <stdio.h>
int main( )
{
int c;
printf( “Enter a value :”);
c = getchar( );
printf( “\nYou entered: “);
putchar( c );
return 0;
}

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
27 Computer Science Engg

The gets() and puts() Functions For Micro Notes by the


Student
• The char *gets(char *s) function reads a line from stdin into the buffer
pointed to by s until either a terminating newline or EOF (End of File).
• The int puts(const char *s) function writes the string ‘s’ and ‘a’ trailing
newline to stdout.

#include <stdio.h>
int main( )
{
char str[100];
printf( “Enter a value :”);
gets( str );
printf( “\nYou entered: “);
puts( str );
return 0;
}

The scanf() and printf() Functions

• The int scanf(const char *format, ...) function reads the input from the
standard input stream stdin and scans that input according to the
formatprovided.

• The int printf(const char *format, ...) function writes the output to the
standard output stream stdout and produces the output according
to the format provided.

The format can be a simple constant string, but you can specify %s, %d, %c, %f,
etc., to print or read strings, integer, character or float respectively.

#include <stdio.h>
main( )
{
char str[100]; int i;
printf( “Enter a value :”);
scanf(“%s %d”, str, &i);
printf( “\nYou entered: %s %d “, str, i);
}

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
28 Computer Science Engg

1. Write a Program for Adding two numbers. For Micro Notes by the
Student

2. Write a Program for converting temperature from degree Celsius to Fahrenheit

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
29 Computer Science Engg

3. Write a Program for calculating Kinetic Energy. For Micro Notes by the
Student


C – Operators

• The symbols which are used to perform logical and mathematical


operations in a C program are called C operators.

• These C operators join individual constants and variables to form


expressions.

• Operators, functions, constants and variables are combined together


to form expressions.
Consider the expression A + B * 5. Where +, * are operators, A, B are variables
(operands), 5 is constant and A + B * 5 is an expression.

Types of C operators:

C language offers many types of operators. They are,


™™ Arithmetic operators
™™ Relational operators
™™ Assignment operators
™™ Logical operators
™™ Bit wise operators
™™ Conditional operators (ternary operators)
™™ Increment/decrement operators
™™ Special operators

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
30 Computer Science Engg

(a). Arithmetic operators For Micro Notes by the


• Arithmetic operators are used to perform arithmetic operations in c Student

programming.
• C Programming Supports 5 Arithmetic Operators.
• Arithmetic Operators are used for “Arithmetic Calculation“.

Operator Meaning Example


+ Addition Operator 10 + 20 = 30

- Subtraction Operator 20 – 10 = 10

* Multiplication Operator 20 * 10 = 200

/ Division Operator 20 / 10 = 2

% Modulo Operator 20 % 6 = 2

Write a program to understand the floating point arithmetic operations.

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
31 Computer Science Engg

Relational operators: For Micro Notes by the


Student
• Relational operators in c programming is used for specifying the
relation between two operands such as greater than, less than and
equals.
• In C Programming we can compare the value stored between two
variables and depending on the result we can follow different blocks
using Relational Operator in C.
• Relational operators are used to compare, logical, arithmetic
and character expression and each relational operator takes two
operands.
• Each operator compares their left side with their right side.It evaluates
to 0 if the condition is false and 1 if it is true.

Expression Interpretation Value


Operator Meaning

< Less than a<b FALSE 0


> Greater than a>b TRUE 1
Less than or equal
<= a<=b FALSE 0
to
Greater than or
>= a>=b TRUE 1
equal to
== Equal to a==b FALSE 0
!= Not equal to a!=b TRUE 1

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
32 Computer Science Engg

c). Logical Operators: For Micro Notes by the


• Logical operators are used to combine expressions containing relation Student

operators. To compare more than one condition then we need to use


logical operators. In C, there are 3 logical operators:

S.no Operators Name Example Description


It returns true when
1 && Logical AND (x>5)&&(y<5) both conditions are
true
It returns true when
2 || Logical OR (x>=10)||(y>=10) at-least one of the
condition is true
It reverses the state
of the operand
“((x>5) && (y<5))”

3 ! Logical NOT !((x>5)&&(y<5))


If “((x>5) && (y<5))”
is true, logical NOT
operator makes it
false

Truth Table
P Q P&&Q P||Q !P
False False False false True
False True False True True
True False False True False

True True True True False

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
33 Computer Science Engg

d )Assignment Operators: For Micro Notes by the


• An Assignment operator is used to form an assignment expression, Student

which assigns the value to an identifier. The most commonly used


assignment operator is = . Assignment expressions that make use of
this operator are written in the form
Identifier=expression;

Operator Description Example


Simple assignment operator, C = A + B will assign
= Assigns values from right side value of A + B into C
operands to left side operand
Add AND assignment operator, C += A is equivalent to C
It adds right operand to the left = C + A
operand and assign the result t o
+=
left operand

Subtract AND assignment C -= A is equivalent to C


operator, It subtracts right = C - A
-= operand from the left operand
and assign the result to left
operand
Multiply AND assignment C *= A is equivalent to C
operator, It multiplies right = C * A
operand with the left operand
*=
and assign the
result to left operand

Divide AND assignment operator, C /= A is equivalent to C


It divides left operand with the = C / A
/= right operand and assign the
result to left operand

Modulus AND assignment C %= A is equivalent to C


operator, It takes modulus using = C % A
%=
two operands and assign the
result to left operand

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
34 Computer Science Engg

For Micro Notes by the


Left shift AND assignment C <<= 2 is same as C=C<<2
Student
operator of the right shift of c by
<<=
2) (It assigns the result of the
right shift of c by 2)
Right shift AND assignment C >>= 2 is same as C = C
operator >> 2(It assigns the result of
>>=
the right shift of c by 2)

Bitwise AND assignment operator C &= B is same as C = C &


B(It assigns the result o f
&= the Bitwise AND between B
& C)

bitwise exclusive OR and C ^= B is same as C = C ^B


assignment operator (It assigns the result of
^= the Bitwise Exclusive OR
between B & C)

bitwise inclusive OR and C |= B is same as C = C |B


assignment operator (It assigns the result of
|= the Bitwise Inclusive OR
between B & C)

(e) Bitwise Operators:

• Bitwise operators are special types of operators that are used in


programming the processor. In processor, mathematical operations
like: addition, subtraction, addition and division are done using the
bitwise operators which makes processing faster and saves power.

Operators Meaning of operators


& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
35 Computer Science Engg

Write a C program to understand Bitwise operator. For Micro Notes by the


Student

Increment and Decrement operators:


• The increment (++) and decrement (--) operator are unary operators
because they operate on a single operand. the increment operator
increment the value of the variable by 1,while decrement operator
decrement the value of the variable by 1.
Ex. ++x is equivalent to x=x+1
--x is equivalent to x=x-1
• These operator should be used only with variables, They can not be
used with constant or expressions.
Ex. The expression ++5 or ++(X+Y+Z) are invalid.
• These operators are of two types
1. Prefix-Increment/decrement Operator is written before the operand.
Ex. ++X or --X
2. Postfix-Increment/decrement Operator is written after the operand.
Ex. X++ or X--

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
36 Computer Science Engg

Prefix-Increment/decrement Operator For Micro Notes by the


Student
• Here first the value of the variable is Incremented or decremented
then the new value is used in the operation
• Let us take a variable X=3
The statement Y=++X; Means first increment the value of X by 1,then
assign the value of X to Y. This single statement is equivalent to these
two statements
X= X+1;
Y=X;
Now value of X=4 and Y=4
The statement Y=--X; Means first decrement the value of X by 1,then
assign the value of X to Y. This single statement is equivalent to these
two statements
X= X-1;
Y=X;
Now value of X=3 and Y=3

1.Write a C program to implement Prefix-Increment/decrement Operator


#include<stdio.h>
Int main(void)
{
Int x=8;
Printf(“x=%d”,x);
Printf(“x=%d”,++x);
Printf(“x=%d”,x);
Printf(“x=%d”,--x);
Printf(“x=%d”,x);
Return (0);
}
Output:
x=8 x=9 x=9 x=8 x=8
Postfix-Increment/decrement Operator
Here first the value of the variable is used in the operation and then value
Incremented or decremented is performed.
let us take a variable X=3
The statement Y=X++; Means first the value of X is assigned to Y and then X is
incremented. This single statement is equivalent to these two statements
Y=X;
X= X+1;

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
37 Computer Science Engg

Now value of X=4 and Y=3 For Micro Notes by the


Student

The statement Y=X--; Means first the value of X is assigned to Y and then X is
incremented. This single statement is equivalent to these two statements
Y=X;
X= X-1;
Now value of X=3 and Y=4

1.Write a C program to implement Postfix-Increment/decrement Operator

(g). Conditional Operator (?:):


The conditional expression can be used as shorthand for some if-else statements.
It is a ternary operator. This operator consists of two symbols: the
question mark (?) and the colon (:).
The general syntax of the conditional operator is:
Identifier = (test expression)? Expression1: Expression2;

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
38 Computer Science Engg

This is an expression, not a statement, so it represents a value. The For Micro Notes by the
Student
operator works by evaluating test expression (condition). If it is true (non-zero),
it evaluates and returns expression1 (Block1). Otherwise, it evaluates and
returns expression2 (Block2).

1.Write a program for Conditional operator.

(i) Special Operators:


S. no Operators Description
1 & This is used to get the address of the
variable.
Example: &a will give address of
variable a.
2 * This is used as pointer to a variable.
Example: * a where, * is pointer to
the variable a.
3 Size of () This gives the size of the variable.
Example: size of (char) will give us 1.

EXPRESSIONS:

• An expression is a sequence of operands and operators that reduces


to a single value.
• Expression can be simple or complex.
• An operator is a syntactical token that requires an action be taken.
• An operand is an object on which an operation is performed.

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
39 Computer Science Engg

A simple expression contains only one operator. E.g: 2 + 3 is a simple For Micro Notes by the
Student
expression whose value is 5.A complex expression contains more that one
operator. E.g: 2 + 3 * 2.

To evaluate a complex expression we reduce it to a series of simple


expressions. In this first we will evaluate the simple expression 3 * 2 (6)and
then the expression 2 + 6,giving a result of 8.

The order in which the operators in a complex expression are evaluated


is determined by a set of priorities known as precedence, the higher
the precedence ,the earlier the expression containing the operator is
evaluated.

The following table shows the precedence and Associativity of operators:

TYPE CONVERSION:
In an expression that involves two different data types , such as multiplying an
integer and a floating point number to perform these evaluations ,one of the
types must be converted.
We have two types of conversions:
1.Implicit Type Conversion 2.Explicit Type Conversion

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
40 Computer Science Engg

IMPLICIT TYPE CONVERSION: When the types of the two operands in a binary For Micro Notes by the
Student
expression are different automatically converts one type to another .This is
known as implicit type conversion.

EXPLICIT TYPE CONVERSION: Explicit type conversion uses the unary cast
operator ,to convert data from one type to another. To cast data from one
type to another ,we specify the new type in parentheses before the value we
want converted.

For example, to convert an integer ,a , to a float, we code the expression like


(float) a

1. write a program to check implicit and explicit type conversion

DECICSION MAKING OR CONTROL STATEMENTS:

IF AND SWITCH STATEMENTS:


We have a number of situations where we may have to change the order
of execution of statements based on certain conditions or repeat a group of
statements until certain specified conditions are met.

The if statement is a two way decision statement and is used in conjunction with
an expression. It takes the following form
If the test expression is true then the statement block after if is executed
otherwise it is not executed

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
41 Computer Science Engg

Simple if: For Micro Notes by the


if (test expression) Student

{
statement block;
}
statement–x ;
Note: If test expression is false then only statement–x is executed.

The if –else statement:


If your have another set of statement to be executed if condition is false then
if-else is used
if (test expression)
{
statement block1;
}
else
{
statement block2;
}
statement –x ;

Write Algorithm, Program and draw a flowchat for Roots of quadratic equation.

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
42 Computer Science Engg

For Micro Notes by the


Student

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
43 Computer Science Engg

Nesting of if..else statement : For Micro Notes by the


Student
If more than one if else statement with in if else
if(text cond1)
{
if (test expression2
{
statement block1;
}
else
{
statement block 2;
}
}
else
{
statement block2;
}
statement-x ;

if else ladder:
• The nesting of if-else depends upon the conditions with which we
have to deal.
• This statement is similar to switch statement.

if(condition1)
statement1;
else if(condition2)
statement 2;
else if(condition3)
statement n;
else
default statement.
statement-x;

THE SWITCH STATEMENT:


• switch statement used when user have to choose from more than two
option/choices (means multiple option)

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
44 Computer Science Engg

switch(expression) For Micro Notes by the


Student
{
case value-1:
block-1
break;
case value-2:
block-2
break;
--------
--------
default:
default block;
}
statement–x;

1.Write a Program for Arithmetic operations using switch case.

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
45 Computer Science Engg

LOOPING or ITERATION or REPETITION STATEMENTS: For Micro Notes by the


Student
• Some times we require a set of statements to be executed repeatedly until
a condition is met.
• We have two types of looping structures. One in which condition is tested
• before entering the statement block called entry control. The other in which
condition is checked at exit called exit controlled loop.
• First initialization is done once, then condition is evaluated and if it is true
then body of loop is executed. After execution of body the control goes to
• increment/ decrement part then condition is once again evaluated and if
true body is executed once again. This goes on until test condition becomes
false.

WHILE STATEMENT:
while(test condition)
{
body of the loop
}

1. write a program which describe while loop

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
46 Computer Science Engg

DO WHILE STATEMENT: For Micro Notes by the


The while loop does not allow body to be executed if test condition is false. The Student

do while is an exit controlled loop and its body is executed at least once.

do
{
body
}while(test condition);

write a program which describe do- while loop

THE FOR LOOP:


It is also an entry control loop that provides a more concise structure
for(initialization; test control; increment/decrement)
{
body of loop
}

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
47 Computer Science Engg

1 Write a program which describe for loop For Micro Notes by the
Student

2. Write a program for printing a multiplication table for given number using all
loops(while, do-while, for loop)

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
48 Computer Science Engg

OTHER STATEMENTS: For Micro Notes by the


Student

BREAK STATEMENT:
• This is a simple statement. It only makes sense if it occurs in the body of
a switch, do, while or for.
• When it is executed the control of flow jumps to the statement
immediately following the body of the statement containing the break.
• Its use is widespread in switch statements, where it is more or less
essential to get the control.

CONTINUE STATEMENT:

• This statement meaning is “Skip the following statements and do the


next iteration”.
• Executing a continue starts the next iteration of the loops like do, while
or for statement immediately.
• The use of continue is largely restricted to the top of loops, where a
decision has to be made whether or not to execute the rest of the
body of the loop.

Example it ensures that division by zero (which gives undefined behavior)


doesn’t happen.

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

int i;
for(i = -10; i < 10; i++)
{
if(i == 0)
Continue;
printf(“%f\n”, 15.0/i);

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
49 Computer Science Engg

GOTO STATEMENT :
For Micro Notes by the
• In C, it is used to escape from multiple nested loops Student
• You will need a label when you use goto statement.

goto L1;
/* whatever you like here */
L1: /* anything else */

#include <stdio.h>
int main()
{
int sum=0,;
for(int i = 0; i<=10; i++){
sum = sum+i;
if(i==5){
goto addition;
}
}
addition:
printf(“%d”, sum);
}

Output:

C Pointers: stdin, stdout, stderr


Linux is built being able to run instructions from the command line using switches
to create the output.One of the ways to make use of this is by using the three
special file descriptors - stdin, stdout and stderr
stdin
Generally standard input, referred to as “stdin”, comes from the keyboard.
When you type stuff or data, you’re typing it on stdin (a standard input terminal).
A standard input device, which is usually the keyboard, but Linux also allows you
take standard input from a file.
stdout
Standard output, as created at process creating time, goes to the console,
your terminal or an X terminal. Exactly where output is sent clearly depends on
where the process originated.Our console or terminal should be the device that
is accepting the output. Running the command:

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
50 Computer Science Engg

stderr For Micro Notes by the


The final component in this dialog of file descriptors is standard error.Every Student

command could send it’s output to one of two places: a) it could be valid
output or b) it could be an error message.It does the same with the errors as it
does with the standard output; it sends them directly to your terminal screen.

Command Line arguments


• The most important function of C is main() function.
• It is mostly defined with a return type of int and without parameters :int
main() { /* ... */ }
• We can also give command-line arguments in C .
• Command-line arguments are given after the name of the program in
command-line shell of Operating Systems.
• To pass command line arguments, we typically define main() with two
arguments : first argument is the number of command line arguments
and second is list of command-line arguments.

int main(int argc, char *argv[]) { /* ... */ }


or
int main(int argc, char **argv) { /* ... */ }

• argc (Argument Count) is int and stores number of command-line


arguments passed by the user including the name of the program.
So if we pass a value to a program, value of argc would be 2 (one for
argument and one for program name)
• The value of argc should be non negative.
• argv(Argument Vector) is array of character pointers listing all the
arguments.
• Argv[0] is the name of the program , After that till argv[argc-1] every
element is command -line arguments.

#include <stdio.h>
int main(int argc, char** argv)
{
printf(“You have entered %d \n” ,argc);
printf(“ arguments:\n”);
for (int i = 0; i < argc; ++i)
printf(“%s\n”, argv[i]);
return 0;
}

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
51 Computer Science Engg

Output: For Micro Notes by the


Student

Storage Classes
• Storage classes of c will provide following information to compiler.
• storage area of variable.
• Scope of variable i.e in which block the variable is visible.
• Lifetime of the variable i.e how long the variable will be there in active
mode.
• Default value of the variable if it is not initialized.

Depending on the behaviour and storage area storage classes are classified
into 2 types.
1. Automatic Storage class
2. Static storage class

Automatic Storage class:


• This storage class variables are created automatically and destroyed
automatically.
• Automatic storage class variables will be stored in stack area of data
segment or CPU register.
• Under automatic storage class we are having 2 types of storage class
specifies
1. Auto
2. Register

Static storage class:


• Static storage class variable are created only ones and throughout
the program it will be there in active mode only.
• Static storage class variables are stored in static area of data segment.
• Under static storage class we have 2 types of storage class specifiers
1. Satic
2. Extern

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
52 Computer Science Engg

Code Properties: For Micro Notes by the


Type Scope Life Default Value Student
Auto Block Block Garbage Value
Static Block Program 0
Extern Program Program or All functions 0
Register Block Block Garbage Value

Note:
By default any kind of variable storage class specifier is auto.
In C- Programming language there are 4 – types of scope are available i.e.
body, function, program and file scope.
Int a;
Short int a;
Signed short int a;
Auto signed short int a;
| | | | |
| | | | |
Storage Sign Size Type Variable

Register Variables:
It is a special kind of variable which stores in CPU register.
The advantage of register variables are faster than remaining variables.

Limitations:
When we are working with register variable we can’t create ‘n’ number of register
variables. i.e., depending upon CPU capacity 4.6 are maximum variables.
In TC.3.0 we can’t access the address of register variables.
Pointer or pointer related concepts are not applied to register variables.

Examples:
void main()
{
register int a=10;
++a;
printf(“\n value of a:%d”,a);
printf(“Enter a value:”);
scanf(“%d”,&a);
--a;
printf(“value of a:%d”,a);
} Output: Error

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
53 Computer Science Engg

1.write a Program for storage Classes For Micro Notes by the


Student

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
54 Computer Science Engg

Number System For Micro Notes by the


Student
The number systems are used to quantify the magnitude of something. One
way of quantifying the magnitude of something is by proportional values.
This is called analog representation. The other way of representation of any
quantify is numerical (Digital).

The number systems are called position weighted systems, since the weight
of each digit depends on its relative position within the number.
Base (or) radix of system:
The base (or) radix of number system is defined as the number of different
symbols (Digits (or) characters) used in that number system.
Key points:
• If the Base of the number system is ‘r’, the no. of different symbols used in
the system is ‘r’ (the different symbols are ‘0 to r–1’).
• The largest value of digits in base ‘r’ system is ‘r–1’.

General Binary Decimal Octal Hexadecimal


Base r 2 10 8 16
Symbols 0, 1, 2, 0, 1 0, 1, 2, 0, 1, 0, 1, 2, 3, 4, 5,
..., (r–1) ...,9 ..... 7 6, 7, 8, 9, A,
B, C, D, E, F
Max (r–1) 1 9 7 F(15)
digit

Types of Number systems:


1. Binary number system:
The base value of binary number system is ‘2’. (i.e. r = 2).
(i) The different symbols used in Binary number system are 0 to r–1. (i.e. 0
to 1)Those are 0, 1.
(ii) The maximum digit in binary system
= r–1 = 2–1 = 1
2. Octal Number system:
The base value of octal number system is ‘8’.
(i.e. r = 8)
(i) The number of different symbols used in octal system are 0 to r–1.
(i.e. 0 to 7) Those are 0,1,2,3,4,5,6,7.
(ii) The maximum digit in octal number system
= r–1 = 8–1= 7.

3. Decimal number system:


The base value of Decimal number system is ‘10’. (i.e. r = 10)

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
55 Computer Science Engg

(i) The number of different symbols used in decimal system are 0 to r – 1. For Micro Notes by the
Student
(i.e. 0 to 9) Those are 0,1,2,3,4,5,6,7,8,9.

(ii) The maximum digit in Decimal system


= r–1 = 10–1 = 9.

4. Hexadecimal number system:


The base value of Hexadecimal Number system is ‘16’. (i.e. r = 16)
(i) The number of different symbols used in
Hexadecimal number system are 0 to r–1.
(i.e. 0 to 15)
Those are 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F.
(ii) The maximum digit in Hexadecimal system
= r–1 = 16–1 = 15(F).
Key points:
Any positional number system can be expressed as sum of products of place
value and digit value. For base ‘r’ system,
......... r3 r2 r1 r0 . r–1 r–2 r–3 ........
↑ Decimal point
The place (or) weights of different digits in mixed number systems is

(i) Decimal number systems


.........103102101100.10–110–210–3...........

(ii) Binary number system


......23222120.2–12–22–3........

(iii) Octal number system


.....83828180.8–18–28–3.......

(iv) Hexa decimal number system


.........163162161160.16–116–216–3...........

Examples:

01. (234.14)10=
102 101 10 0 10 -1 10 -2
Sol:
2 3 4 . 1 4
=(2×102)+(3×101)+(4×100)+(1×10–1)+(4×10–2)

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
56 Computer Science Engg

02. (765.43)8 = For Micro Notes by the


8 8 8
2 1
8 8
0 -1 -2 Student
Sol:
7 6 5 . 4 3

= (7×82)+(6×81)+(5×80)+(4×8–1)+(3×8–2)

03. (1011.110)2 =
23 22 21 20 2 -1 2 -2 2 -3
Sol:
1 0 1 1 . 1 1 0

=(1×23)+(0×22)+(1×21)+(1×20)+(1×2–1)+(1×2–2)
+(0×2–3)

04. (AB4.CD)16 =

162 161 16 0 16 -1 16 -2
Sol:
A B 4 . C D

= (A×162)+(B×161)+(4×160)+(C×16–1)+(D×16–2)

=(10×162)+(11×161)+(4×160)+(12×16–1)+(13×16–2)

Number systems conversion:


Any number system with base ‘r’ to decimal number system
Any number with base ‘r’ may be converted to their decimal equivalents
by the positional weights method.
In this method, each digit of the number with base ‘r’ is multiplied by its
position weight and the product terms are added to obtain the decimal
number.
If the number is D3D2D1D0.D–1D–2 with base r, then its decimal equivalent is

r3 r2 r1 r 0 r -1 r -2
D3 D2 D1 D 0 . D -1 D -2

Example: 1.1
=(D3×r3)+(D2×r2)+(D1×r1)+(D0×r0)+(D–1×r–1)+ (D–2×r–2)
(10101)2 = ( )10

Sol: Positional weights

2 4 23 22 21 2 0

1 0 1 0 1

= (1×24)+(0×23)+(1×22)+(0×21)+(1×20)
= 16 + 4 + 1 = (21)10

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
57 Computer Science Engg

Example: 1.2 For Micro Notes by the


Student
(11011.101)2 = ( )10

2 4 23 22 21 2 0 . 2 -1 2 -2 2 -3
Sol: Positional weights
1 1 0 1 1 . 1 0 1

=(1×24)+(1×23)+(0×22)+(1×21)+(1×20)

+(1×2–1)+(0×2–2)+(1×2–3)
1 1
= 16 + 8 + 0 + 2 + 1 + 2 + 0 + 8

= (27.625)10

Example: 1.3
(653.12)8 = ( )10

82 81 80 8 -1 8 -2
Sol: Positional weights
6 5 3 . 1 2

=(6×82)+(5×81)+(3×80)+(1×8–1)+(2×8–2)
= 384 + 40 + 3 + 0.125 + 0.03125
= (427.15625)10

Example: 1.4
(A2B.4C)16 = ( )10

Sol:
162 161 16 0 16 -1 16 -2
Positional weights
A 2 B . 4 C

=(A×162)+(2×161)+(B×160)+(4×16–1)+(C×16–2)
=(10×162)+(2×161)+(11×160)+ b 4 # 16
1
l + b12 # 1
256
l
= 2560 + 32 + 11 + 0.25 + 0.046875
= (2603.296875)10

Example: 1.5
(2134.13)5 = ( )10

53 52 51 5 0 5 -1 5 -2
Sol: Positional weights
2 1 3 4 . 1 3

=(2×53)+(1×52)+(3×51)+(4×50)+(1×5–1)+ (3×5–2)

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
58 Computer Science Engg

= 250 + 25 + 15 + 4 + 0.2 + 0.12 For Micro Notes by the


Student
= (294.32)10

Example: 1.6
(10101.101)2 = ( )10

Sol: Integer: (10101)2


Positional weights 24 23 22 21 20
Binary number 1 0 1 0 1
= ]1 # 2 4g + ^0 # 23h + ]1 # 22g + ^0 # 21h + ]1 # 20g = 16+4+1 = (21)10
Fraction: (.101)2
Positional weights 2–1 2–2 2–3
= ^1 # 2-1h + ^0 # 2-2h + ^1 # 2-3h
= 0.625

Mixed: (10101.101)2
(10101)2 = (21)10
(.101)2 = (.625)10
(10101.101)2 = (21.625)10

Example: 1.15
(1010101010.10010110)2 = ( )8

Sol: (1010101010)2
Step 1: Write the given binary number
Step 2: Group three bits

001 010 101 010 = (1252)
8
. . . .
1 2 5 2

(.10010110)2

.100 101 100 = (.454)
. . . 8
4 5 4

(1010101010.10010110)2 = (1252.454)8

Example: 1.16
(1010101010.10010110)2 = (1252.454)8
(1001110101101.10101101) 2
= ( )16

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
59 Computer Science Engg

Sol: (1001110101101)2
For Micro Notes by the
Student
0001 0011 1010 1101 = (13AD)
16
. . . .
1 3 A D

(.10101101)2

.1010 1101 = (.AD)16


. .
A D

(1001110101101.10101101)2 = (13AD.AD)16

Decimal to number system with base-r conversion:


There are two methods to convert a decimal number to a number system
with base-r
1. Double-dabble method
2. Indirect method

01. Double-dabble method:


In this method, the decimal integer number is converted to the integer
number with base-r by successive division by r, and the decimal fraction
is converted to fraction by successive multiplication by r

Integer number: (successive Division-by-r method)


The given decimal integer number is successively divided by r till the quotient
is zero. The last remainder is MSB. The remainders read from bottom to top
give the equivalent integer number with base-r.

Fraction Number: (successive multiplication-by-r method)


The given decimal fraction and the subsequent decimal fractions are
successively multiplied by r, till the fractional part of the product is 0 (or) till the
desired accuracy is obtained.
The first integer obtained is MSB. Thus, the integers read from top to bottom
give the equivalent fraction of a number system with base-r

Key Point: Any decimal number can be converted to an equivalent number in


any other base (r) system by repeated division-by-r for integers and repeated
multiplication-by-r for fractions.

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
60 Computer Science Engg

Example: 1.1 For Micro Notes by the


Student
(161.875)10 = ( )2

Sol: Conversion of integer


Successive division by ‘2’

2 161
2 80 - 1
2 40 - 0
2 20 - 0
2 10 - 0
25 -0
2 2 -1
1 -0

(161)10 = (10100001)2
Conversion of fraction
Successive multiplication by ‘2’
0.875 × 2 = 1.75 1
0.75 × 2 = 1.5 1
0.5 × 2 = 1.0 1
(0.875)10 = (.111)2
(161.875)10 = (10100001.111)2

Example: 1.2
(382.40625)10 = ( )8

Sol: Conversion of integer


Successive division by ‘8’
8 382
8 47 - 6
5-7

(382)10 = (576)8
Conversion of fraction
Successive multiplication by ‘8’
0.40625 × 8 = 3.25 3
0.25 × 8 = 2.0 2
(0.40625)10 = (.32)8
(382.40625)10 = (576.32)8

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
61 Computer Science Engg

Example: 1.3 For Micro Notes by the


Student
(2748.8671875)10 = ( )16

Sol: Conversion of integer


Successive division by ‘16’
16 2748
16 171 - 12
10 - 11
10 11 12

(2748) 10 = (A B C) 16

Conversion of fraction
Successive multiplication by ‘16’
0.8671875 × 16 = 13.875 13(D)
0.875 × 16 = 14.0 14(E)
(0.8671875)10 = (.DE)16

(2748.8671875)10=(ABC.DE)16

Example: 1.4
(456.24)10 = ( )6

Sol: Conversion of integer


Successive division by ‘6’
6 456
6 76 - 0
6 12 - 4
2-0

(456)10 = (2040)6
Conversion of fraction
Successive multiplication by ‘6’
0.24 × 6 = 1.44 1
0.44 × 6 = 2.64 2
0.64 × 6 = 3.84 3
0.84 × 6 = 5.04 5
0.04 × 6 = 0.24 0
0.24 × 6 = 1.44 1
Here the fraction 12350 is getting repeated so, we can stop multiplying
here
(0.24)10 = (0.12350)6
(456.24)10 = (2040.12350)6

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
62 Computer Science Engg

02. Indirect method: For Micro Notes by the


Student

Example: 1.1
(2748)10 = ( )2

Sol: (a) Decimal → Hexadecimal → Binary


(2748) 10 " 16 2748 " (A B C) 16
-
16 171 12 ( 1010 1011 1100) 2
10 - 11

(or)

(b) Decimal → Octal → Binary


(2748)10 → (5274)8 → (101 010 111 100) 2

1.2
Example:
(1011011101)2 = ( )10

Sol:(a) Binary → Octal → Decimal

83 82 81 80
(1011011101) " ( 001 011 011 101) 2 " ( 1 3 3 5) 8

(1335) 8 = (1# 8 3) + (3 # 8 2) + ^3 # 81h + ^5 # 8 0h


= (733)10

(or)

(b) Binary → Hexadecimal → Decimal

(1011011101) 2 " ( 0010 1101 1101) 2 " a 2 D D k16


162 161 16 0

(2DD)16 = 2 # 16 2 + 13 # 161 + 13 # 16 0
= 512 + 208 + 13
= (733)10

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
63 Computer Science Engg

Octal to Binary Conversion: For Micro Notes by the


Student

Octal digit Binary equivalent

0 000

1 001
2 010
3 011
4 100
5 101
6 110
7 111

Replace each octal digit by its 3-bit binary equivalent

ex: (542.13)8 = ( )2

5 4 2 . 1 3

(101 100 010 . 001 011) 2

Binary to octal conversion:


Step 1: Divide the binary number into 3-bits each
Step 2: Replace each 3-bits group by its octal equivalent

Example:
(1011101011.11010)2 = ( )8

Sol:
0 0 1011101011.110 10 0
- -
for 3 - bit grouping for 3 - bit grouping
pad with zeros pad with zeros

001 011 101 011. 110 100


1 3 5 3 . 6 4

(1353.64)8

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
64 Computer Science Engg

Hexadecimal to Binary conversion: For Micro Notes by the


Student

Hexadecimal Binary
digit equivalent
0 0000

1 0001

2 0010

3 0011

4 0100

5 0101

6 0110

7 0111

8 1000

9 1001

10=A 1010

11=B 1011

12=C 1100

13=D 1101

14=E 1110

15=F 1111

Replace each hexadecimal digit by its 4-bit binary equivalent

Example:
(1A2.B5)16 = ( )2

Sol:
1 A 2 . B 5
e o
0001 1010 0010 . 1011 0101 2

Binary to Hexadecimal conversion


Step-1: Divide the binary number into 4-bits each
Step-2: Replace each 4-bits group by its hexadecimal equivalent

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
65 Computer Science Engg

Example:
For Micro Notes by the
]1011101011.11010g2 = ^ h16 Student

Sol:
0 0 10 1110 1011 . 1101 0 0 0 0
2 E B . D 0

→ For 4-bit grouping pad with zeros


]2EB.D0g16

Hexadecimal to octal conversion:


Method-1:
Step-1: Convert each Hexadecimal digit to its 4-bit binary equivalent
Step-2: Group the binary equivalent of hexadecimal number into 3-bits each.
Step-3: Replace each group by its octal equivalent

Example:
]ACE.ECEg16 = ^ h8

Sol:

]A C E . E C E g 16
binary
equivalent ^101 0 11 00 1 110 . 111 0 11 00 1 110 h 2
0
]5 3 1 6 . 7 3 1 6 g8

Method-2:
Hexadecimal → decimal → octal

Example:
]12Eg16 = ^ h8

Sol: step-1: hexadecimal convert to decimal

162 161 16 0

1 2 E

1 # 162 + 2 # 161 + E # 16 0
256 + 32 + 14 = (302)10

Step-2: Decimal to octal


ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
66 Computer Science Engg

8 302
For Micro Notes by the
8 37 - 6
Student
4-5
(456)8

Octal to Hexadecimal conversion:


Method-1:
Step-1: Convert each octal digit to its 3-bit binary equivalent
step-2: Group the binary equivalent of octal number into 4-bits each
step-3: Replace each group by its hexadecimal equivalent

Example:
]532.67g8 = ^ h16

Sol:
]5 3 2 . 6 7g8
Binary
equivalent 101 011 010 . 110 111
0
^0001 0101 1010 . 1101 1100 h2
0
]1 5 A . D C g16

Method-2:
Octal → Decimal → Hexadecimal

Example:
(125)8 = ( )16

Sol: step- 1: octal convert to decimal

82 81 8 0

1 2 5

1 # 82 + 2 # 81 + 5 # 80
64 + 16 + 5
(85)10
step-2: Decimal convert to Hexadecimal

16 85
5-5

(55)16

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
67 Computer Science Engg

Previous year JNTUH Questions: For Micro Notes by the


2&3 Marks: Student

01. Which statement is multi way selection statement? Why? [May-18/2m]

02. What is meant by time sharing environment? [May-18/3m]

03. Evaluate the value of the following when x = 3.45 floor (x * 100+0.5)/100
[May-18/2m]
04. Distinguish between variables and constants. Give example. [May-
18/2m]

05. Explain the difference between break, goto and continue statements.
[May-18/3m]

06. What are the storage classes in C? [May-18/2m]

07. What are flowchart symbols? Explain. [Apr-18/2m]

08. Explain the syntax of case statement with example. [Apr-18/3m]

09. Explain various logical operators that are used in ‘C’. [June-17/2m]

10. Differentiate between break and continue statement with an example.


[June-17/3m]

11. Explain about auto storage class. [June-17/2m]

12. What is the size of the double data type? Which conversion specifier is
used? [June-17/2m]

13. Write program in C to interchange the two values without using third vari-
able. [June-17/3m]

14. Explain rules of variables. [Dec-17/2m]



15. Give the symbols of flow chart. [Dec-17/3m]

16. Give the decimal equivalent of 7AC(16). [Sep-17/2m]

17. What is ternary operator in C? Give example expression for its use.
[Sep-17/3m]

18. Define the term algorithm and how it is different from a flow chart.
[Dec-16/2m]

19. Distinguish between if and switch statements. [Dec-16/3m]

20. Explain auto and register storage classes. [Dec-16/2m]

21. What is meant by type qualifier? [Dec-16/3m]

22. Describe type casting with example. [June-15/2m]

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
68 Computer Science Engg

For Micro Notes by the


23. Distinguish between while and do-while statement in C. [June-15/3m] Student

24. What is the difference between compiler and interpreter? [Dec-15/2m]

25. Give the list of bit wise operators of C. [Dec-15/3m]

26. What is the use of type qualifier? [Dec-15/2m]




5 Marks Questions:

01. Write an algorithm to find LCM of two numbers. [May-18/5m]

02. Describe the process of program development. [May-18/5m]

03. What are the three differences between the conversion codes for input
formatting and output formatting? Explain them with examples.
[May-18/5m]

04. What is the need of explicit type conversion in C? How to cast the data?
[May-18/5m]

05. State and explain various identifiers in C program. And also discuss about
operator precedence in expression evaluation with a suitable example.
[May-18/10m]

06. Write and explain the structure of C program. [May-18/5m]

07. Write a program to print prime numbers up to a given number ‘n.


[May-18/5m]

08. Write a C program to generate Fibonacci series. [Apr-18/5m]

09. What are operators in C? Explain with example. [Apr-18/5m]

10. Explain about decision making and branching. [Apr-18/5m]

11. Describe the structure of a C program. [Apr-18/5m]

12. Draw the flowchart to find the roots of the given equation ax2+bx+c=0.
[June-17/5m]

13. Explain the relational operators with an example. [June-17/5m]

14. Explain the different types of data types used in ‘C’ language. [Jun-17/5m]

15. What is a programming language? Briefly explain the classification of pro-


gramming languages. [June-17/5m]

16. Write and explain the steps in writing a ‘C’ program. [June-17/5m]

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
69 Computer Science Engg

17. Discuss about the various bitwise operators supported by Language ‘C’. For Micro Notes by the
[June-17/5m] Student

18. Write ‘C’ program to print the Fibonacci sequence. [June-17/5m]

19. In what way a do-while loop differs from while loop, Explain. [June-17/5m]

20. Explain the different types of bitwise operators are used in C. [Dec-17/5m]

21. Write an algorithm to find the greatest number among the three given
numbers. [Dec-17/5m]

22. Write a program to swap the two numbers without using a temporary vari-
able. [Dec-17/5m]

23. Explain the different types of operators are used in C. [Dec-17/5m]

24. Why is C language known as middle-level language? [Sep-17/5m]

25. Draw a flow chart to find average of 10 numbers. [Sep-17/5m]

26. What is associativity? Illustrate its application in expression evaluation.


[Sep-17/5m]

27. Which statement is a multi way selection statement? Why? [Sep-17/5m]

28. Write a C program to find factorial of a given number N by using ‘while’


loop and do-while’ loop. [Dec-16/10m]

29. What are the C operators? Explain their usage with suitable examples to
each of them. [Dec-16/10m]

30. Explain with example switch and for statements. [June-15/5m]

31. Write a C Program to print prime numbers between 1 and 100.


[June-15/5m]

32. Write a C Program to print prime numbers between 1 and 100.



[June-15/5m]
33. Explain the bitwise operator with suitable examples. [June-15/5m]

34. Write a Program in C to read a set of n single digits and converts them into
single decimal integer. For example the program should convert the set of
5 digits [1, 2, 3, 4, 5] to integer 12345. [June-15/5m]

35. What is a flowchart? Discuss various symbols used in flowchart. Illustrate with
an example. [Dec-15/5m]

36. Explain the steps in program development. [Dec-15/5m]

37. Describe the concept of operator precedence. When is associativity ap-

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
70 Computer Science Engg

plied in expression evaluation? [Dec-15/5m] For Micro Notes by the


Student
38. Write a program to print prime numbers up to a given number ‘n’.
[Dec-
15/5m]

Topic wise Questions asked in JNTUH Examination

Introduction to computer:
Computing Environment
Computer languages - [2m/ June-16]
Different between compiler & interpreter - [2m/ Dec-15]
Creating & running program & program Development - [Dec-15/5m]

C Language:
Data types used in C language - [May-17/5m]
Program structure
Identifiers, Precedence & Associating with expressions- [J-14/10m, Jun-15/5m,
Dec-15/5m]
Variables & constants - [J-14/2m]
Operators- [Dec-16/10m- Jan-15/5m- Conditional operator (Dec-14/Jan-
15/2m]
Algorithm & Flow chart-[Dec-16]
Type casting/type conversion (Jun-15/2m]
Selection/Decision making statement (Jan-15/5m)- switch with example-[10m/
Jun-16] Different between if & switch [Dec-16/3m]
Repetition statement (loops) (Jun-14/ June-15/5m)
Different-break, continue [Jun-15/5m] June-16/2m] May-17/3m
goto (June-14/3m)
Flow chart with symbol (Dec-15/5m)
Root of quadratic equation [May-17/5m]
Factorial using while [Dec-16/10m]
Convert 1,2,3,4,5 Into whole number 12345 [Jan-15 /5m]
Type qualifier (June-16/3m). [Dec-16.3m]
Precaution taken while constructing statement in C language. [Dec-14] [Jan-
15/2M]
What is Programming Language? Briefly explain the classification of Program-
ming Language [May-17/5m]

ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)

Das könnte Ihnen auch gefallen