Programming For Problem Solving - I UNIT - 2
Programming For Problem Solving - I UNIT - 2
Programming For Problem Solving - I UNIT - 2
in
ACE
Engineering College
Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
NAAC Accridated with ‘A’ Grade
Department of
Computer Science and Engineering
Prepared by :
Ms. Shubhangi Mahule
Assistant Professor (M.Tech)
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
UNIT – I
Introduction to Computers:
Computer Systems:
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
3 Computer Science Engg
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
4 Computer Science Engg
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.
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
Fig3: Ram
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
Computer Software
System software:
• The operating system provides services such as a user interface, file and
database access, and interfaces to communication systems such as
Internet protocols.
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
• 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.
• 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
2 Time-Sharing Environment:
3 Client/Server Environment
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
9 Computer Science Engg
4 Distributed Computing
Computer Languages:
Machine 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
Symbolic Languages:
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.
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:
Executing Programs:
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
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
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
PSEUDOCODE:
FLOW CHART:
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
16 Computer Science Engg
(Or)
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
start
Read A and B
value
C=A+B
Print C
Stop
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
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
19 Computer Science Engg
Reliability
Portability
Flexibility
Interactivity
Modularity
Efficiency and Effectiveness
Database systems
Graphics packages
Word processors
Spreadsheets
Operating system development
Compilers and Assemblers
Network drivers
Interpreters
Structure of C program
¾¾ Documentation section
¾¾ Link Section
¾¾ Definition Section
¾¾ Global declaration section
¾¾ Function prototype declaration section
¾¾ Main function
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
20 Computer Science Engg
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
21 Computer Science Engg
Identifiers
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:
DATA TYPES:
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
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
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”
#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
#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
#include <stdio.h>
int main( )
{
char str[100];
printf( “Enter a value :”);
gets( str );
printf( “\nYou entered: “);
puts( str );
return 0;
}
• 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
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
Types of C operators:
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
30 Computer Science Engg
programming.
• C Programming Supports 5 Arithmetic Operators.
• Arithmetic Operators are used for “Arithmetic Calculation“.
- Subtraction Operator 20 – 10 = 10
/ Division Operator 20 / 10 = 2
% Modulo Operator 20 % 6 = 2
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
31 Computer Science Engg
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
32 Computer Science Engg
Truth Table
P Q P&&Q P||Q !P
False False False false True
False True False True True
True False False True False
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
33 Computer Science Engg
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
34 Computer Science Engg
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
35 Computer Science Engg
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
36 Computer Science Engg
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
37 Computer Science Engg
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
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).
EXPRESSIONS:
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.
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.
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
{
statement block;
}
statement–x ;
Note: If test expression is false then only statement–x is executed.
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
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
43 Computer Science Engg
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;
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
44 Computer Science Engg
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
45 Computer Science Engg
WHILE STATEMENT:
while(test condition)
{
body of the loop
}
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
46 Computer Science Engg
do while is an exit controlled loop and its body is executed at least once.
do
{
body
}while(test condition);
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
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:
#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:
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
50 Computer Science Engg
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.
#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
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
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
52 Computer Science Engg
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
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
54 Computer Science Engg
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’.
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.
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
= (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)
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
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
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
Example: 1.6
(10101.101)2 = ( )10
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
(1001110101101.10101101)2 = (13AD.AD)16
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
60 Computer Science Engg
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
(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
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
(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
Example: 1.1
(2748)10 = ( )2
1.2
Example:
(1011011101)2 = ( )10
83 82 81 80
(1011011101) " ( 001 011 011 101) 2 " ( 1 3 3 5) 8
(or)
(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
0 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111
ex: (542.13)8 = ( )2
5 4 2 . 1 3
(101 100 010 . 001 011) 2
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
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
64 Computer Science Engg
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
Example:
(1A2.B5)16 = ( )2
Sol:
1 A 2 . B 5
e o
0001 1010 0010 . 1011 0101 2
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
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
162 161 16 0
1 2 E
1 # 162 + 2 # 161 + E # 16 0
256 + 32 + 14 = (302)10
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
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
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
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]
09. Explain various logical operators that are used in ‘C’. [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]
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]
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
68 Computer Science Engg
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]
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]
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
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]
29. What are the C operators? Explain their usage with suitable examples to
each of them. [Dec-16/10m]
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]
ACE Engineering College : Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG)
70 Computer Science Engg
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)