Professional Documents
Culture Documents
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.
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.
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.
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.
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.
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.
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.
Assembler
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.
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.
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
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.
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]
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
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
>> 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
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.
• 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;
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.
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.
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
derived data types: Derived data types are derived from the collection of basic data
types.
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.
Ex: \a: Bell (alert); \t: horizontal tab; \n: new line
%c Character
%d Signed integer
%f Float values
%lf Double
%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
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.
• Subprograms
All the user-defined functions are defined in this section of the program.