You are on page 1of 29

PROGRAMMING & PROBLEM SOLVING [UNIT-1]

Computer: A computer is an electronic device that has the ability to store, retrieve, and process
data, and can be programmed with instructions that it remembers.

A computer system is made of two major components.

1. Hardware

2. Software.

Computer Hardware: The physical parts that make up a computer (the central processing unit,
input, output, and memory) are called hardware.

Computer Software: Programs that tell a computer what to do are called software. A set of
instructions that perform a particular task is called a program, software program, or software.

Software is classified as follows.

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.

An operating system is a program which connects the user and the electronic hardware in
a computer. It has a set of programs which supervise the activities of a computer and control the
operations of the hardware components such as CPU, main memory, disk drives, keyboard,
monitor printer and so on.

System support software provides system utilities and other operating services. Examples
of system utilities are sort programs and disk format programs. Operating services consist of
PROGRAMMING & PROBLEM SOLVING [UNIT-1]

programs that provide performance statistics for the operational staff and security monitors to
protect the system and data.

System development software includes the language translators that convert 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 is broken into two classes: general-purpose softwareand


application-specific software. General-purpose software is purchased from a software
developer and can be used for more than one application. Examples of general-purpose
software include word processors, database management systems, and computer-aided design
systems. They are labeled general purpose because they can solve a variety of user computing
problems.
Application-specific software can be used only for its intended purpose.A general ledger
system used by accountants and a material requirements planning system used by a
manufacturing organization are examples of application-specific software. They can be used
only for the task for which they were designed; they cannot be used for other generalized
tasks.

Block diagram of a computer

CPU

Arithmetic and
Logic unit (ALU)

Input Output
unit
Control Unit unit

Memory unit

1. Input unit:

An input device delivers the response of the user to the computer. Thus it creates man-to-
machine communication. In the other way this is the receiving section of the computer. Various
input devises are keyboard, mouse, optical mark reader(OMR), disks etc

2. Output unit:

An output device delivers the results of a program to the user. Thus it creates machine-to-man
communication. In other way it is shipping section of the computer. It takes information that has
been processed by the computer and place it on various output devices to make the information
available for use. Various output devises are computer screen, printer, plotter, disks.

3. Memory unit:
PROGRAMMING & PROBLEM SOLVING [UNIT-1]

This part of computer is used as storage for programs as well as data. It is measured in units of
bits, bytes and words. Memory is broadly divided into two types, Main memory and secondary
memory. Main memory is a semiconductor memory, used for temporary storage of programs and
data. Ex: RAM, ROM. The secondary memory is magnetic memory used for permanent storage of
programs and data, Ex: hard-disk, flash drive etc.

4. Control unit:

This component is responsible for controlling the movement of data and instructions in and
out of the memory and CPU. It decodes the fetched instruction and initiates the desired operation.
This activity is called instruction execution. Thus it sends command and control signals and finds the
sequence of instructions to be executed.

5. Arithmetic and Logic unit (ALU) :

During instruction execution, it receives the signal from the control unit and accordingly, it
performs calculations such as addition, subtraction, division and multiplication. It contains decision
mechanisms to find the logical relation between two variables.

6.Central Processing Unit:

Control unit, memory unit and ALU constitute central processing unit

PROGRAMMING LANGUAGES
Programming language can be defined as , the language used for expressing a set of instructions
that can be executed by the computer.

1. Machine level languages (MLLs)


i) The machine level languages came into existence soon after the advent of computers. These
languages are also called “first generation languages”
ii) These are the low-level languages directly used by the computer.
iii) The instructions of these languages consist of stings of binary digits 0 and 1.

These languages do not require translators because machine code will be directly understood
by the computer.
PROGRAMMING & PROBLEM SOLVING [UNIT-1]

Limitations:
1) These languages are machine dependent.
2) Lack of readability.
2. Assembly level languages (ALLs)
i) Programming in low-level languages is very difficult. Therefore middle-level languages are
developed. These are also called second generation languages.
ii) The instructions of these languages consists mnemonics. Mnemonics are the English like
abbreviations to represent the elementary operations of the computer.
Example:
To find the sum of two numbers a=12 and b=9, the code is as follows
LOAD A
ADD B
STORE C
iii) Assembly languages require translator programs, called assemblers. Assemblers convert
assembly language programs machine level languages.

Assembly Level Language Program

Assembler

Equivalent Machine Level


Language

1. High-level languages (HLLs)


i) To write complex programs in assembly languages is difficult. So high-level languages are
developed. These are often called third generation languages.
ii) In these programs single statements accomplish, that particular task.
Example:
C = A + B;
iii) Translator programs called compilers/Interpreter convert high-level language programs to
machine language.
iv) Compiler/interpreter is a system program, which converts a HLL program into its MLL
equivalent. A program written in a HLL is called a Source Program and the machine code
obtained after conversion is called Object Program.
PROGRAMMING & PROBLEM SOLVING [UNIT-1]

HLL

Compiler

Equivalent MLL

v) BASIC, PASCAL, COBOL, FORTRAN,C and C++ are the high level languages.
vi) HLL are more readable and easy to understand and modify if need be.
vii) HLL are portable i.e they are machine independent.

GENERATION OF COMPUTER LANGUAGES:


1. First generation language(1GL) : machine language
2. Second generation language(2GL) : assembly language
3. Third generation language (3GL) : high level languages
i) Fourth Generation Languages (4GL) HLL requires some proficiency in programming
skills and generally HLL are procedural in nature. These disadvantages led to develop
4GLs.

ii) Forth –generation languages are non procedural languages and have simple, english like syntax
rules. Generally these are used to access databases.
iii) The programmer does not have to give the details of the procedure in the program, but specify,
instead, what is wanted.

Example:
Assume that a programmer needs to display marks of a student , say ABHILASH, from the RESULTS
file. In procedural language, the programmer would have to write a series of instructions using
the following steps:
Step 1: get a record from RESULTS file
Step 2: if this record is of ABHILASH, display the marks
Step 3: if this is not the record of ABHILASH, go to step 1, until end of file.
In SQL, one of the 4GL, this can be written as
SELECT MARKS FROM RESULT WHERE NAME =’ ABHILASH’.
iv) Generally 4GLs are query languages or report generators or application generators.
4. Fifth Generation Languages (5GL)
Natural languages represent the next step in the development of programming languages belong to
the fifth generation languages. Natural language is similar to query language, with one
difference; it eliminates the need for the user to learn a specific syntax.

Creating and Running Programs


Computer hardware understands a program only if it is coded in its machine language.
PROGRAMMING & PROBLEM SOLVING [UNIT-1]

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, and (4) executing the program. These steps are seen in Figure

Assembler, Interpreter, Compiler, Loader, Linker


Assembler:
Assembler is software that converts a program written in assembly language into machine code.
There is usually one-to-one correspondence between simple assembly statements and machine
language instructions. Since the machine language is dependent on the processor architecture,
assembly language programs also defer for different architecture.

Interpreter:
Interpreter performs line by line execution of the source code. Interpreter reads source code line
by line, converts it into machine understandable form, executes the line, and then proceeds with
the next line. Some languages that uses interpreter are BASIC and PYTHON
Compiler:
A program written in high level language has to be converted to a language that computer can
understand i.e. binary form. Compiler is software that translates the program written in high level
language to machine language. The program written in high level language is called source code
and the compiled program is called object code.
The compilation process generally involves two parts. Breaking down the source code into small
pieces and constructing the object code. The compiler also reports syntax errors, if any in the
source program.

Source code Assembler Object code


Interpreter
Compiler
Linker:
PROGRAMMING & PROBLEM SOLVING [UNIT-1]

A linker is a program that link several object modules and libraries to form a single executable
program.

Loader:
Loaders are a part of operating system that brings an executable file residing on disk into memory
and starts it running.

Source Object
program code Linker
Compiler

Loader Executable
Memory code
PROGRAMMING & PROBLEM SOLVING [UNIT-1]

Algorithms and flow charts: short signed unsigned while do


for break continue switch case
Algorithm is a step by step process to do a
particular task. The steps must be clear and auto static extern goto return
definite. By following the steps one should get the struct union if else sizeof
final result. volatile default void const register

Flow chart is the pictorial representation of an enum


algorithm. Flow chart uses various geometrical • Keywords serve as basic building blocks for program
figures to represent the operations and arrows to statements.
show the direction of flow.
• Each keyword has a special meaning in C.
Following are the commonly used symbols in • All these keywords are in lower case letters. As
flowcharts. capital or upper case letters and lower case letters are
significant in C, all these keywords must be used in
Start/stop
lower cases.
• These key words names should not be used in
Input/output
function names or variable names or identifiers. Keep
this list with you for reference, whenever you write
Processing programs.
• No gap should be left in between the letters of a single
key word, while using.
Checking/decision making
• These key words are also called as reserved words or
vocabulary for the language.
Looping

Connector USER DEFINED WORDS


The words, which are defined by user, are user-defined
Arrows to represent direction
words. They can be for variable name function name so
of flow
on.

Rules of user defined words


CHARACTERS USED IN C 1. It must start with an alphabet.
2. Maximum number of characters should not
Aphabets: Uppercase letters A to Z
excess 8 (excess are ignored).
Lowercase letters a to z 3. Second character on wards alphabets, digits or
underscore’s can be used.
Numbers: 0 to 9
4. No special characters are allowed expect
Special characters: underscore.
5. It cannot be a reserved word.
+ - * / \ % | ~ ? ! , . ; : ‘ “ & # $ ^ <>
= ( ) [ ] { } _

KEY WORDS
int float char double long
3. unsignedint :It takes 2B of memory space that is 16 bits.
Valid Invalid There is no sign bit is sign bit. All 32 bits are magnitude bits.
Maximum value unsigned integer is 216 – 1 = 65535. Minimum
abhilash 23abc value of unsigned integer is 0.
sum _2cd
We cannot represent –ve values in unsigned character
product P.V.N. rao
abc123 Rama rao float

ab__12__cd ab,cd
hyderabad ab-cd
(first 8 are recognized) auto floatdouble long double
(4B) (8B) (10B)

MEMORY CONCEPTS AND DATATYPES 4. float : It takes 4B of memory space. First 26 bits are
integer part and next 6 bits are for fractional part. Decimal point
Data type is defined as the type of value that can be stored in a is assumed after 26th bit. It is not stored in memory
variable xxxxxxxx…..xxx ^ xxxxxxx→ 0 or 1 26
EXAMPLE 6
int a;
Variable ‘a’ is of type int we can store only integer
values in ‘a’ but not float. 5. double (long float): It takes 8B of memory space. First 52
float b; bits are integer part and next 12 bits are for fractional part.
Variable ‘b’ is of type float we can store only integer values in Decimal point is assumed after 52th bit. It is not stored in
‘a’ but not int. memory.
There are several data types in C language.
6.long double: It takes 10B of memory space. First 65 bits are
int integer part and next 15 bits are for fractional part. Decimal
point is assumed after 65th bit.

char

Int long unsigned


(2B) (4B) (2B)

1.int: It takes 2B of memory space that is 16 bits. First bit is signed char unsigned char
sign bit and remaining 15 are magnitude bits. (1B) (1B)
1010 1100 0101 0010
7. unsigned char: It takes 1B of memory space that is 8 bits.
sign magnitude There is no sign bit, all 8 are magnitude bits. Maximum value is
28 – 1 = 255.minimum value is 0. Range of ASCII values is from 0
If the sign bit is zero it is +ve magnitude. If the sign bit is 1 it is - to 255
ve magnitude.
8. signed char(or) char: It takes 1B of memory space that is 8
Maximum integer value is 215 – 1 = 32767. Minimum integer bits. There is one sign bit and 7 are magnitude bits are
value is -215 = -32768 magnitude bits.
Maximum value is 27 – 1 = 127.
Thus integer ranges from –32768 to 32767. Minimum value is –27 = -128
2. longint: To represent a value more than 32767 long is
used. It is an extension to Integer it takes 4B of memory space
that is 32 bits. First bit is sign bit and remaining 31 are
magnitude bits.
Maximum value of long integer value is 2 31 – 1
#include <stdio.h>
void main()
{
Type casting intx,y,sum;printf("Enter two numbers\n");
printf() Function scanf("%d%d",&x,&y);
printf() function is used to display a string or values of sum = x+y;
variables using the standard output device(monitor). It has printf("the sum of %d and %d is %d\n”,
x,y,sum);
the following forms }
S. Data Type Format Remark
printf(“control string”); No specifier
1. char %c Signed character
printf(“control string”,v1,v2,…vn);
2. int %d Signed integer in
where v1,v2 are variables whose values are to be decimal number system
3. unsigned %u Unsigned integer in
displayed on the screen. Control string is the format int decimal number system
4. long int %ld Signed long
string which represents the format specification
5. short int %hd Signed short
data type format specifier 6. unsigned %hu Unsigned short
short
int %d 7. float %f Signed single precision
float %f 8. double %lf Signed double precision
char %c float
9. string type %s string
string %s
Steps involved in solving a problem using
Examples computer.
1. printf(“%d”,x); to display integer value 1. Problem definition
2. printf(“%f”,a); to display float value 2. Problem analysis
3. printf(“the result is %d”,x); 3. Algorithm design
4. Coding
5. Testing and debugging.
scanf() Function 6. Documentation
scanf() function is used to read/input values of variables
using the standard input device(keyboard).
ALGORITHMS
It is an input function. It is call by address function. We
must send address to scanf but not variable. An algorithm is a step by step process to solve a given
It has the following form problem. It is a procedural solution to the problem on
hand.
scanf(“format string”,&v1,&v2,…&vn);
Where v1, v2,… are variables whose values are to be Characteristics of an algorithm
1. Each instruction in an algorithm should be precise
read from the keyboard. and unambiguous.
2. Each instruction should be executed in a finite
Examples amount of time.
1. scanf(“%d”,&x); to read integer value 3. The algorithm should work on all legitimate
2. scanf(“%f”,&a); to read float value inputs.
4. The required output shoud be obtained after the
3. scanf(“%c”,&ch); to read char value
algorithm ends.
4. scanf(“%s”,name); to read a string
5. scanf(“%d%f”,&x,&a); to read an integer value Examples:
and a float value. To find area of a rectangle.
Input: length and breath
Output: area
Algorithm
1. Read length
2. Read breath
// To find the sum of two numbers
3. Area=length * breath
4. Display area
5. Stop.

PSEUDOCODE:
Pseudo code is another way of representing
algorithm. As the name itself indicates, pseudo code
comprises plain English and higher level language
constructs to express conditional execution and repeated
execution. Pseudo code is influenced by higher level
languages.
Example: to generate prime numbers between two
numbers.
Read l,u
i=l
k=2
while(l<=u)
{
flag=1
K=2
while(k<=i/2)
{
r=i%k
if(r==0)
{
flag=0
}
K=k+1
}
if(flag==1)
display i
i=i+1
}
Operators

1. ‘=’ is Assignment operator ‘= = ’ is comparison


Arithmetic Operators: operator
1.Arithmetic Operators + - * / %
2. 7 is stored in ‘a’ and result Value of ‘a’ is compared
2.Assignment operator = += *= %= /= is 7. to 7 and result is true or
3.Relational Operators ><>= <= == != false.
4.Logical Operators && || !
5.Increment and ++ -- 3. Operator behind ‘=’ is copy Operator behind ‘= =’ is
Decrement Operators that is RHS is copied to compare that is RHS is
6.Conditional operator ? : LHS variable compared to LHS variable
(Ternary operator) Results are true or false.
7.Bitwise operators >><<& | ^ ~
4. a = b and b = a are a = = b and b = = a are
8.Comma operator ,
different same
1. Arithmetic Operators are of two types.
Unary and binary.
Logical Operators:
– unary
1. These are also called Boolean operators.
+, -, *, /, % are binary.
2. There are three logical operators
2. These will perform arithmetic operations.
3. These can operate only on built in data types. && || !
4. Precedence of these operators 3. A logical operator is used to compare or evaluate
logical and relational expressions ie these are used in
- (unary minus)
combing one or more conditions.
* / %
4. && and || are binory operators and !is a unary
+ -
operator.
5. mod operator (%) works on integers only.
5. truth table for &&
true && true is true. If any expression is false it is
Relational Operators: false.
1. There are six relational operators. 6. truth table for ||
<><= >= == != false || false is false. If any expression is true it is
2. == and != are also called equality operators true.
3. Relational operators are used to compare arithmetic,
7. ! operator is called negation operator. It negates ie
logical and character expressions.
true to false and false to true.
4. Each operator compares its left hand side with the
right hand side. The whole expression involving the
relational operator then produces a logical value. A Example for relational and logical Operators.
logical value is either true or false. It evaluates to zero void main()
if the condition is false and one if true. {
// To demonstrate relational operators int x=10,y=5,p,q;
#include <stdio.h> p= x>9;
void main() q = x>3 && y!= 3;
{ printf(“p=%d q=%d \n“,p,q);
int a=20,b=10; q= (x==10) || (y==10);
printf("a= %d b=%d\n",a,b); printf(“q=%d\n”,q);
printf(" value of expression (a>b) is %d\n",a>b ); q= (x==10) && (y==10);
printf(" value of expression (a<b) is %d\n",a<b ); printf(“q=%d\n”,q);
printf(" value of expression (a==b)is %d\n",a==b); }
} OUTPUT:
p=1 q= 1
OUTPUT: q=1
value of expression (a>b) is 1 q=0
value of expression (a<b) is 0
value of expression (a==b)is 0
What is the difference between a=7 and a==7 Increment and decrement operators:
a=7 a==7 1. ++ and- - are called increment and decrement operators.
2. Increment operator add one and decrement operator printf("%d %d %d\n",a,b,c);
subtract one. Output
3. These will operate only on variables. i.e. 5++ is not valid. c= ++a + ++b;
4. If the operator appears after the variable, it is called printf("%d %d %d\n",a,b,c); 1 1 1
postfix, then the value used is the value before it is c= ++a - b--; 2 0 2
updated. printf("%d %d %d\n",a,b,c); 3 1 4
5. If the operator appears in front of the variable, it is called } 4 0 3
prefix operator, then the value used is updated value.
6. Example x = a++ means Program 4
x = a; main()
a= a+1; { Output
int x=1,y;
x = ++a means y=x++ + --x + ++x + ++x + x++; 10 4
a = a+1; printf("%d %d",y,x);
x=a; }
7. These are extensively used in for and while loops. Conditional operator:
8. When increment and decrement operators are not in an The conditional operator contains a condition followed by
expression postfix and prefix have same meaning; two statements or values. If the condition is true exp2 is
executed otherwise exp3 is executed. The conditional
Program 1 operator is also called ternary operator because there are
void main() three arguments.
{
int i=1,y,x=1; Output Syntax:
i++; 2 (exp1) ? (exp2) : (exp3);
printf("%d\n",i); 3 Example 1
++i; 2 2 // to find the largest of two numbers
printf("%d\n",i); 3 2 #include <stdio.h>
3 void main()
y=++x;
printf("%d %d\n",x,y); {
y = x++; int x,y,big;
printf("%d %d\n",x,y); printf("Enter two numbers\n");
printf("%d\n",x); scanf("%d%d",&x,&y);
} big = (x>y) ? x : y;
printf("big =%d\n",big);
output }
Program 2 Example 2
void main() 6 6 4 4 3 1
//to find absolute value of a number
{ 1 1 1 1 1 1
#include <stdio.h>
int i=1;
printf("%d %d %d %d %d %d \n",i,++i,i++,++i,++i,i++);
void main()
i=1; {
printf("%d %d %d %d %d\n",i,++i, i-- ,--i, i++); int x,absx;
} printf("Enter a number\n");
scanf("%d",&x);
Program 3 absx= (x>0) ? x : -x;
void main() printf("%d\n",absx);
{ }
inta,b,c; Example 3
a=b=c=0;
// to display the result
c = a++ + ++b;
#include <stdio.h>
printf("%d %d %d\n",a,b,c);
void main()
c= ++a - --b; {
int mark;
printf("Enter your marks\n"); }
scanf("%d",&mark); Example 4
printf("you secured %s mark", mark>=60 ? // to find the largest of 3 numbers
"FIRST CLASS" :"NOT A FIRST CLASS"); #include <stdio.h>
void main() 2 ++ -- -(unary) ! ~ Right to Left
{ * &sizeof
intx,y,z,big; 3 * / % Left to right
printf("Enter three numbers\n"); 4 + - Left to right
scanf("%d%d%d",&x,&y,&z); 5 <<>> Left to right
big = (x>y)? (x>z ? x:z) : (y>z ?y :z); 6 <<= >>= Left to right
printf("big =%d\n",big); 7 == != Left to right
} 8 & Left to right
9 ^ Left to right
Comma operator: 10 | Left to right
Comma operator is used to separate two or more 11 && Left to right
expressions. The comma operator has the lowest priority 12 || Left to right
among all the operators.
13 ? : R to L
Ex: a=2,b=4,c=a+b;
14 = += -= *= R to L
15 , (comma Left to right
Example 1
operator)
void main()
{
1. a = b = c = d = 0;
intx,i,y;
Right to left evaluation for assignment operators. ‘0’ is
x=(i=2,++i);
stored in c , b , a. This is called multiple assignment i.e.,
y=(2,20);
same value stored in different variables.
printf("%d %d",x,y);
This equivalent to a = 0; b = 0; c = 0;
}
Example 2 2. a% = 3 + 4 * 2;
// to swap two numbers by using comma operator a = a % ( 3 + 4 * 2)
#include <stdio.h> = 20 % (3+8)
void main() = 20%11
{ =9
intx,y,temp;
x=20,y=10; 3. if a = 4, b = 5, c = 6 evaluate a += b += c+=7;
temp=x,x=y,y=temp;
 a += b += c+= 7
printf("%d %d",x,y);
}
 a += b += 13
a += 18
Operator precedence and associativity  22 a b c
The order in which various operators are 22 18 13
performed is known as hierarchy of operation or
operation precedence.
Someofthe operators of the same level
precedence are evaluated from left to right or right
BITWISE OPERATORS
Use of bitwise operators is to modify the individual bits
to left. This is called associativity.
rather than the number.

Precedence operator associativity


order Operator What it does
1 ( ) [ ] -> Left to Right
& bitwise AND;
Compares two bits and generates a 1 result if
both bits are 1, otherwise it returns 0. ^ bitwise exclusive OR;
Compares two bits and generates a 1 result if the
| bitwise inclusive OR; bits are complementary, otherwise it returns 0.
Compares two bits and generates a 1 result if
either or both bits are 1, otherwise it returns 0. ~ bitwise complement;
Inverts each bit. ~ is used to create destructors. x^y= 11000 =24

>> bitwise shift right; // program to illustrates >> and << operators
Moves the bits to the right, discards the far right #include <stdio.h>
bit and assigns the left most bit to 0. #include <stdlib.h>
void main()
<< bitwise shift left; {
Moves the bits to the left, it discards the far left bit int x=16,y=16;
and assigns the right most bit to 0. char a[30],b[30],c[30],d[30],e[30];
itoa(x,a,2);
printf("x = %8s = %d\n",a,x);
Both operands in a bitwise expression must be of an itoa(y,b,2);
integral type. printf("y = %8s = %d\n",b,y);
_________________________________________________
E1 E2E1 & E2 E1 ^ E2 E1 | E2
itoa(x>>1,c,2);
_________________________________________________
0 0 0 0 0
printf("x>>1 = %8s = %d\n",c,x>>1);
1 0 0 1 1 itoa(y<<1,d,2);
0 1 0 1 1 printf("y<<1 = %8s = %d\n",d,y<<1);
1 1 1 0 1 }
_________________________________________________ output
// program to illustrates |,& and ^ operators x = 10000 =16
y = 10000 =16
#include <stdio.h> x>>1= 1000 = 8
#include <stdlib.h> y<<1= 100000 =32
void main() //Program 3
{ void main()
int x=20,y=12; {
char a[30],b[30],c[30],d[30],e[30];
int a=12,b=25;
itoa(x,a,2);
printf("%d\n",a&b);
printf("x = %8s = %d\n",a,x);
itoa(y,b,2); printf("%d\n",a|b);
printf("y = %8s = %d\n",b,y); printf("%d\n",a^b);
itoa(x|y,c,2); printf("%d\n",a>>2);
printf("x|y = %8s = %d\n",c,x|y); printf("%d\n",b<<2);
itoa(x&y,d,2); }
printf("x&y = %8s = %d\n",d,x&y); Output
itoa(x^y,e,2); 8
printf("x^y = %8s = %d\n",e,x^y); 29
}
21
output
3
x= 10100 = 20
y= 1100 =12 100
x|y= 11100 =28
x&y= 100 =4
TEST YOUR UNDERSTANDING – I
1. Write a program to print CBIT by using printf function.
2. Write a program to accept an integer number, float number, character and to display
3. Write a program to find sum of any two numbers.
4. Write a program to find area and perimeter of a circle.
5. Write a program to find components of a force.
6. Write a program to find the last digit of number.
7. Write a program to convert an integer number to years, months and days
8. Write a program to find sum of digits of a given three-digit integer number.
9. Write a program to find simple interest and compound interest
Theory questions
1. Write about machine languages, assembly languages, high level languages
2. Draw block diagram of a computer and explain different parts
3. Define algorithm, flow chart, program
4. Write various symbols used in flow chart
5. What is operating system
6. Explain basic structure of c program
7. Explain various input and output functions in C
8. List out various operators in C and explain in detail
9. What is the difference between precedence and associativity of operators?
10. List out the relational and logical operators in C language
11. What are unary, binary and ternary operators in C ?
12. List out basic data types in C language
13. Explain how the following data are represented inside a computer
a) integer b) floating point number c) characters
14. What do you understand by structured program?
15. What is the difference between procedural and object-oriented languages?
16. What is type casting?
17. Write C expressions for the following
a) Maximum of two variables m and n (by using if else)
b) Maximum of two variables m and n (by using ternary operator)
c) Maximum of three variables a, b and c (by using ternary operator)
d) Whether given integer is even or odd
e) Variable x lies between 20 and 30
f) Point p(x,y) is in first quadrant
g) Point is origin
h) Given character is vowel
i) Given character is lowercase alphabet
j) Given character is digit
OBJECTIVE TYPE QUESTIONS
1. What is a compiler?
A compiler reads the entire program and converts it to the object code.
2. Name any five versions of C compilers.
Turboc ,boroland c, ansi c,C99,win45

3. Give the character set of C language.


Letters, Digits, White spaces, Special Characters.

4. What is an identifier?
Identifiers are names of variables, functions and arrays. They are user defined names, consisting
of letters and digits, with the letter as the first character.

5. What are the rules to be followed for forming identifiers?


It must start with an alphabet. It should not be a reserved word. Should not start with digit.
Second character onwards alphabets,digits or underscore can be used.

6. What are the key words in C?


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

7. What is the difference between a constant and variable?


A constant is a fixed value whose value cannot be changed during program's execution or once
the value is defined. It is also known as literal. For example, 5, 20, 'a', 'Radius', 5.2, "Welcome
back", etc. A constant can be defined in two ways, like #define pre-processor and
by const keyword. The constants can be different data types, such as integer constants, floating
constants, character constants, string constants and enumeration constants. Let's understand
briefly about them:

• Integer constant
An integer constant is a whole number and can be large without including any decimal points.
For example, 0, 1, 2, 123, 5767etc.
• Float constant
The float constants are the part of an integer constant that containing a decimal point, fractional
form and exponential form.
Here are some example of floating-point constants:
0.5, 35.05, 2.3e6 etc.
• Character Constants
It is a single character constant enclosed within a single quotation mark (like 'a', 'A'), called a
character constants. There are some valid constants as: 'g', 'D', ' ', '#'.
• String Constant
It is the character set of string constants that are enclosed in a double quote. The character may
be letters, numbers, special symbols and some blank space. Furthermore, a string constant
contains zero, one or more continuous character in double quotation marks. For example, "Hello
Friends", "Computer", "5987", " " , "A".
Variable: It is a variable that stores data type value in a program. The value of a variable can
change depending on the conditions.
int a = 5; float radius = 5.2;

8. What are the various data types available in C?


Fundamental or scalar or primitive or basic data types: int, char, float,double

derived data types: arrays, functions, pointers, structures, unions.

9. What is a derived data type?


Derived data types are derived from the collection of basic data types.

10. Explain the qualifiers with examples.


Short, long, signed, unsigned identifiers may precede some of basic data types to specify the
number of bits for representing the respective type of data in memory. they are known as
qualifiers or modifiers.

Example:

Long int, short int, unsigned int, signed int, unsigned long int, unsigned short int, signed long int,
signed short int, signed char, unsigned char, long double.

11. How is character constant specified in C?


It should be within single quote.

12. Differentiate between character constant and string constant.


String constant should be within double quote.

13. What are escape sequence?


\0, \a, \b, \t, \n, \v, \f, \r.

14. What are white space characters available in C?


In C, blank and some of the non-graphic characters are known as white space characters. They
are blank, new line, horizontal tab, vertical tab, carriage return and form feed.

15. How are comments introduced in C?


A comment explains briefly what a program does. In C programs, the characters between /* and
*/ are treated as comments and are ignored by the compiler.

16. What is the significance of semicolon in C?


It is a statement terminator.

17. What are C tokens? Give examples?


A compiler identifies and splits a program into a number of tokens. A token may be a single
character or a group of characters which has a specific meaning. Various C tokens are identifiers,
keywords, constants, operators, separators.

18. Explain the structure of C language.


19. Write a program that does no operation.
20. Name various types of operators in C
21. Differentiate between binary and unary operator.
22. What is an expression?
Any valid combination of constants , variables and operators constitute an expression.

23. Is there any exponential operator in c?


24. What is a numeric expression?
Arithmetic expression is called numeric expression

25. Discuss pre and post increment operators.


26. What is a relational expression?
27. Differentiate between relational and logical expressions?
28. What is the difference between & and &&
29. What is the difference between = and ==
30. Classify bitwise operators.
31. Explain compound assignment expression?
32. What is a ternary operator in c?
33. What is type conversion(type casting) in c?
34. What is the result of each of the following (float)5/3 (float)1 +1/3
35. What is precedence and associativity of operators?
36. “C is rich in operators “. Justify the statement.
37. Find the values of following expressions
a=b++ + ++b; where b=10
a*= b= c=5 ; where a = 20
x=(y*=2) + (z=a=4); given y=5;
a *= b /= c -= d+= 6*2
38. What is purpose of including stdio.h in a program?
39. How is stdio.h file included in a program?
40. What is the syntax for scanf() function?
41. What is a conversion specifier?
42. What is a conversion character?
43. What is a header file. Name any 5 header files?
44. What is the purpose of math.h file?
45. What is the propose of main() function?
46. How many times the function main() can be used in a program?
47. What is the suppression character what is its effect?
48. How would the number 9876 appear if printed with the following conversion specifications
1) %0d 2) %3d 3) %4d 4) %10d
49. Distinguish between source code, object code and executable code.
UNIT-1(Short answer questions)
1. What is a compiler? Name any five versions of C compilers?
A compiler reads the entire program and converts it to the object code. Turbo c,
boroland c, gcc, ansi c, C99, win45

2. Give the character set of C language.


Letters, Digits, White spaces, Special Characters.

3. What is an identifier?
Identifiers are names of variables, functions and arrays. They are user defined names,
consisting of letters and digits, with the letter as the first character.

4. What are the rules to be followed for forming identifiers?


It must start with an alphabet. It should not be a reserved word. Should not start with
digit. Second character onwards alphabets, digits or underscore can be used.

5. What are the key words in C?


Keywords are predefined, reserved words in C language and each of which is associated
with specific features. These words help us to use the functionality of C language. They
have special meaning to the compilers.

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

6. What is the difference between a constant and variable?


A constant is a fixed value whose value cannot be changed during program's execution
or once the value is defined. It is also known as literal. For example, 5, ‘a', 5.2, "Welcome
back", etc.
A constant can be defined in two ways, like #define pre-processor and by const keyword.
The constants can be different data types, such as integer constants, floating constants,
character constants, string constants and enumeration constants.
Variable:
It is a variable that stores data type value in a program. The value of a variable can
change depending on the conditions.
int a = 5; float radius = 5.2;

7. Explain about integer constant.


Integer constant
An integer constant is a whole number and can be large without including any decimal
points. For example, 0, 1, 2, 123, 5767etc.
8. Explain about float constant.
Float constant
The float constants are the part of an integer constant that containing a decimal point,
fractional form and exponential form.
Here are some example of floating-point constants:
0.5, 35.05, 2.3e6 etc.

9. Explain about character constant.


Character Constants
It is a single character constant enclosed within a single quotation mark (like 'a', 'A'),
called a character constants. There are some valid constants as: 'g', 'D', ' ', '#'.

10. Explain about string constant.


String Constant
It is the character set of string constants that are enclosed in a double quote. The
character may be letters, numbers, special symbols and some blank space. Furthermore,
a string constant contains zero, one or more continuous character in double quotation
marks. For example, "Hello Friends", "Computer", "5987", " " , "A".

11. What are the various data types available in C?


Fundamental or scalar or primitive or basic data types: int, char, float, double

derived data types: Derived data types are derived from the collection of basic data
types.

Ex: arrays, functions, pointers, structures, unions.

12. Explain the qualifiers with examples.


Short, long, signed, unsigned identifiers may precede some of basic data types to specify
the number of bits for representing the respective type of data in memory. they are known
as qualifiers or modifiers.

Example:

Long int, short int, unsigned int, signed int, unsigned long int, unsigned short int,
signed long int, signed short int, signed char, unsigned char, long double.

13. Differentiate between character constant and string constant.


String constant should be within double quote. Character constant should be with in
single quote.

14. What are escape sequence?


\0, \a, \b, \t, \n, \v, \f, \r.

Ex: \a: Bell (alert); \t: horizontal tab; \n: new line

15. What are white space characters available in C?


In C, blank and some of the non-graphic characters are known as white space
characters. They are blank, new line, horizontal tab, vertical tab, carriage return and
form feed.

16. How are comments introduced in C?


A comment explains briefly what a program does. In C programs, the characters
between /* and */ are treated as comments and are ignored by the compiler.

17. What is the significance of semicolon in C?


It is a statement terminator.

18. What are C tokens? Give examples?


A compiler identifies and splits a program into a number of tokens. A token may be a
single character or a group of characters which has a specific meaning. Various C
tokens are identifiers, keywords, constants, operators, separators.

19. Name various types of operators in C


Arithmetic operators, Relational operators, Logical operators, Increment and
Decrement operators, Bit wise operators, Ternary operator, Assignment operators,
Comma operator
20. Differentiate between binary and unary operator.
Operators that operate on two operands are known as binary operators. Operators that
operate on one operand are known as unary operators.
x+y here + is a Binary Plus Operator which is adding values of x and y .
++a is a unary increment Operator which increases a value by one.

21. What is an expression?


Any valid combination of constants, variables and operators constitute an expression.

22. Is there any exponential operator in c?


The C language lacks an exponentiation operator, which would raise a base value
to a certain power. For example: 2^8 , which in some programming languages
is an expression to raise 2 to the 8th power. Instead, C uses the pow() function. ...
In the expression 2^8 , 2 is the base and 8 is the exponent.
23. What is a numeric expression?
Arithmetic expression is called numeric expression

24. What is the difference between & and &&?


A Bitwise And operator is represented as ‘&’ and a logical operator is represented as
‘&&’.
#include<stdio.h>
int main()
{
int a = 8;
int b = 4;
printf("a & b = %d\n", a & b);
printf("a && b = %d\n", a && b);
return 0;
}
Output: a & b= 0
a:1000
b:0100
a&b:0000
a && b=1(The expression a && b will return 1 if both a and b is non-zero, and 0 otherwise.)
25. What is the difference between = and ==
= ==
Let a=7 Consider a==7
1. ‘=’ is Assignment operator ‘= = ’ is comparison operator

2. 7 is stored in ‘a’ and result is 7. Value of ‘a’ is compared to 7 and


result is true or false.

3. Operator behind ‘=’ is copy that Operator behind ‘= =’ is compare


is RHS is copied to LHS variable that is RHS is compared to LHS
variable
Results are true or false.

4. a = b and b = a are different a = = b and b = = a are


same

26. What is type conversion (type casting) in c?


Conversion of one data type to some other data type is known as type casting. That is
Conversion of int to float, float to int, int to char so on is called type casting.
Implicit typecasting: Type casting is done by system.
int a = 10.8; /* a value stored as 10*/
Explicit typecasting:
There are some scenarios in which we may have to force type conversion which is
called Explicit typecasting.
Syntax:
(type-name) expression

(float)5/3 /* answer is 1.666667*/


(float)1 +1/3 /* answer is 1.000000)/
27. Find the values of following expressions
i)a=b++ + ++b; where b=10 /*answer is a=22*/
ii)a*= b= c=5 ; where a = 20 /*answer is a=100*/
iii)x=(y*=2) + (z=a=4); given y=5; /*answer is x=14*/
iv) int a=20, b=50, c=10, d=1;
a *= b /= c -= d+= 2*2; /*answer is a=200*/
28. What is purpose of including stdio.h in a program? How is stdio.h file included in a
program?
stdio. h is known to contain the input and output operations like "printf" or "scanf" etc.
"h" extension means the header file .we can include it with the pre-processed C directive
#include .
29. What is the syntax for scanf() function?
scanf (“format string”, &arg1, &arg2, …..);
30. What is the syntax for printf() function?

printf(“format string”, arg1, arg2, …..);


31. What is a format specifier?
The format specifiers are used in C for input and output purposes. Using this concept
the compiler can understand that what type of data is in a variable during taking input
using the scanf() function and printing using printf() function.

%c Character

%d Signed integer

%f Float values

%lf Double

%Lf Long double

%lld Long long

%s String

%u Unsigned int
32. What is a header file. Name any 5 header files?
A header file is a file with extension . h which contains C function declarations and
macro definitions to be shared between several source files.
• #include<stdio. h> (Standard input-output header)
• #include<string. h> (String header)
• #include<conio. h> (Console input-output header)
• #include<stdlib. h> (Standard library header)
• #include<math. h> (Math header )
33. What is the purpose of math.h file?
“math.h” header file supports all the mathematical related functions in C language.
cos(),sin(),tan(),pow(),sqrt(),cosh(),sinh(),tanh(),log(),log10(),trunc()etc.
34. What is the propose of main() function?

Every C-programs needs to have the main function. Each main function contains 2 parts.
A declaration part and an Execution part. The declaration part is the part where all the
variables are declared. The execution part begins with the curly brackets and ends with
the curly close bracket. Both the declaration and execution part are inside the curly
braces.

35. How many times the function main() can be used in a program?
Whenever we compile and run the program the compiler searches for the main()
function in the program first and then start executing the instructions that are written in
it one by one. So we can have one and only one main() function in a program.

36. How would the number 9876 appear if printed with the following conversion
specifications?
i)%0d
ii) %2d
iii) %4d
iv) %7d
v) %10d

37. What is operating system? Give four examples.


An operating system is a program which connects the user and the electronic hardware
in a computer. It has a set of programs which supervise the activities of a computer and
control the operations of the hardware components such as CPU, main memory, disk
drives, keyboard, monitor printer and so on.
Five of the most common operating systems are Microsoft Windows,
Apple macOS, Linux, Android and Apple's iOS.
38. Explain the structure of C language.
There are six main sections to a basic c program.

The six sections are, Documentation, Link section, Definition section, Global declaration
section, main function, subprograms (sub functions)

• Documentation
The documentation section is the part of the program where the programmer gives the details
associated with the program. He usually gives the name of the program, the details of the author and
other details like the time of coding and description. It gives anyone reading the code the overview of
the code. /*documentation section */

• Link
This part of the code is used to declare all the header files that will be used in the program. This leads
to the compiler being told to link the header files to the system libraries. Example #include<stdio.h>

• Definition
In this section, we define different constants. The keyword define is used in this part.

#define PI=3.14
39. Global Declarations
This part of the code is the part where the global variables are declared. The user-defined functions
are also declared in this part of the code.

Ex: function prototype : int area(int );, int a=10;

40. Main function


Every C-programs needs to have the main function. Each main function contains 2 parts. A declaration
part and an Execution part. The declaration part is the part where all the variables are declared. The
execution part begins with the curly brackets and ends with the curly close bracket. Both the
declaration and execution part are inside the curly braces.

• Subprograms
All the user-defined functions are defined in this section of the program.

Basic Structure of C program

You might also like