0% found this document useful (0 votes)
72 views

UNIT-1: Computer Definition

The document discusses computer systems and problem solving. It defines a computer as a programmable machine that allows users to store and process information. A computer system has two main components: hardware and software. Hardware includes physical parts like the CPU, keyboard, mouse, monitor, printers, and memory. Software includes system software like operating systems, and application software for specific tasks. An algorithm is a set of steps to solve a problem and must have clear inputs, outputs, and be finite, definite, and solvable.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

UNIT-1: Computer Definition

The document discusses computer systems and problem solving. It defines a computer as a programmable machine that allows users to store and process information. A computer system has two main components: hardware and software. Hardware includes physical parts like the CPU, keyboard, mouse, monitor, printers, and memory. Software includes system software like operating systems, and application software for specific tasks. An algorithm is a set of steps to solve a problem and must have clear inputs, outputs, and be finite, definite, and solvable.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

UNIT-1

1.1 INTRODUCTION
 Computer Systems: Software and Hardware Concepts and introduction to C
Computer Definition:
A computer is a programmable machine. It allows the user to store all sorts of information and then ‘process’ that
information, or data, or carry out actions with the information, such as calculating numbers or organizing words.
Computer Systems:
A computer is a system made of two major components: Hardware and Software

Hardware:
 The hardware are the parts of computer itself including the Central Processing Unit (CPU) and related microchips
and micro-circuitry, keyboards, monitors, case and drives (floppy, hard, CD, DVD, optical, tape, etc...).
 Other extra parts called peripheral components or devices include mouse, printers, modems, scanners, digital
cameras and cards (sound, colour, video) etc... Together they are often referred to as a personal computers or PCs.
 Central Processing Unit (CPU) - Though the term relates to a specific chip or the processor a CPU's performance is
determined by the rest of the computers circuitry and chips.
 Keyboard - The keyboard is used to type information into the computer or input information.
 Disk Drives - All disks need a drive to get information off - or read - and put information on the disk - or write. Each
drive is designed for a specific type of disk whether it is a CD, DVD, hard disk or floppy. Often the term 'disk' and
'drive' are used to describe the same thing but it helps to understand that the disk is the storage device which
contains computer files - or software - and the drive is the mechanism that runs the disk.
 Mouse - Most modern computers today are run using a mouse controlled pointer. Generally if the mouse has two
buttons the left one is used to select objects and text and the right one is used to access menus. If the mouse has one
button (Mac for instance) it controls all the activity and a mouse with a third buttons can be used by specific
software programs. One type of mouse has a round ball under the bottom of the mouse that rolls and turns two
wheels which control the direction of the pointer on the screen. Another type of mouse uses an optical system to
track the movement of the mouse.
 Monitors - This Visual Display Unit (VDU) shows information on the screen when you type. This is called
outputting information. When the computer needs more information it will display a message on the screen, usually
through a dialog box. Monitors come in many types and plus sizes from the simple monochrome (one colour) screen
to full color screens.
 Most desktop computers use a monitor with a cathode tube and most notebooks use a liquid crystal display (LCD)
monitor. To get the full benefit of today's software with full colour graphics and animation, computers need a color
monitor with a display or graphics card.

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 1


UNIT-1

 Printers - The printer takes the information on your screen and transfers it to paper or a hard copy. There are many
different types of printers with various levels of quality. The three basic types of printer are; dot matrix, inkjet, and
laser.
 Scanners- Scanners allow you to transfer pictures and photographs to your computer. A scanner 'scans' the image
from the top to the bottom, one line at a time and transfers it to the computer as a series of bits or a bitmap. You can
then take that image and use it in a paint program, send it out as a fax or print it.
 Memory - Memory can be very confusing but is usually one of the easiest pieces of hardware to add to your
computer. It is common to confuse chip memory with disk storage. An example of the difference between memory
and storage would be the difference between a table where the actual work is done (memory) and and a filing
cabinet where the finished product is stored (disk). To add a bit more confusion, the computer's hard disk can be
used as temporary memory when the program needs more than the chips can provide.
 Random Access Memory or RAM is the memory that the computer uses to temporarily store the information as it is
being processed. The more information being processed the more RAM the computer needs.

Basic hardware components:

Types of software:

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 2


UNIT-1

Relationship between system software and application software

The relationship between system and application software is shown in fig. In this figure, each circle represents an
interface point .The inner core is hard ware. The user is represented by the out layer. To work with the system, the
typical user uses some form of application software. The application software in turn interacts with the operating
system, which is a part of the system software layer. The system software provides the direct interaction with the
hard ware. The opening at the bottom of the figure is the path followed by the user who interacts directly with the
operating system when necessary.

System Software: It helps in running the computer hardware and the computer system. System software is a
collection of operating systems; devise drivers, servers, windowing systems and utilities. System software helps an
application programmer in abstracting away from hardware, memory and other internal complexities of a computer.
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:
operating system, system support, and system development.
The operating system provides services such as a user interface, file and database access, and interfaces to
communication systems such as Internet protocols. The primary purpose of this software is to keep the system
operating in an efficient manner while allowing the users access to the system.
System support software provides system utilities and other operating services. Examples of system utilities are
sort programs and disk format programs. Operating services consists of programs that provide performance statistics
for the operational staff and security monitors to protect the system and data.
The last system software category, 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: It enables the end users to accomplish certain specific tasks. Business software, databases and
educational software are some forms of application software. Different word processors, which are dedicated for
specialized tasks to be performed by the user, are other examples of application software.
Application software is broken in to two classes: general-purpose software and application – specific software.
General purpose software is purchased from a software developer and can be used for more than one application.

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 3


UNIT-1

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.
1.1 PROBLEM SOLVING
 Algorithm/Pseudo code
An algorithm is a finite set of instructions to accomplish a particular task in a given problem. In other words, it is a
step by step procedure to solve a problem. There are time and space complexity associated with each algorithm.
Time complexity specifies the amount of time required by an algorithm to perform a desired task.
The space complexity specifies the amount of memory space required by the algorithm to perform the desired task.
Important criteria for algorithms are as follows: (Characteristics of Algorithms)
1. Input
2. Output
3. Finiteness
4. Definiteness
5. Solvable
1. Input:
For every program input is essential. Sometimes it is not needed. Input means the data supplied to the program.
Hence we have to supply zero or more data items externally to the algorithms.
2. Output:
The aim of the program is to get an output. Output is nothing but processed information. At least one output data
item should be produced by the given algorithm.
3. Finiteness:
A problem should be solved with in some number of steps. In all the cases the algorithm must reaches to the ending
statement in finite number of steps. This is called finiteness.
4. Definiteness:
Every instruction should be clear and understandable form. There should be no confusion in the algorithm.
5. Solvable:
A Step specified in an algorithm must be solvable and it it can be done by person with the help of a pencil and paper.
There should be a feasible solution.
In summary the criteria for algorithms can be represented as follows:

Criteria for algorithms

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 4


UNIT-1

Input Zero or more inputs


Output Atleast one
Finiteness N Number of steps
Definiteness Clear algorithm step
Solvable A carried out step

An algorithm can be written in English like sentences or in any standard representation. Sometimes algorithm
written in English like language is called pseudo code.

Some Examples are:


Ex1:
Suppose we want to find the average of three numbers, the algorithm is as follows:
Step 1 : start
Step 2 : Read three numbers x, y, z
Step 3 : Compute the sum of x, y, z
Step 4 : Divide the sum by by 3
Step 5 : Store the result in variable avg
Step 6 : Print the value of avg
Step 7 : stop
Ex2:
Suppose we want the average of ‘n’ inputted numbers, the algorithm is as follows:
Step 1 : start
Step 2 : Read the total number of values n
Step 3 : Assign the value 0 to count
Step 4 : Assign the value 0 to sum
Step 5 : if the count value is equal to value n go to step 10
Step 6 : Read the value x
Step 7 : Add the value x to sum and store it in sum variable
Step 8 : increment the value of count by 1
Step 9 : Goto step 5
Step 10 : compute sum by n and store the result in variable avg
Step 11 : Print the value of avg
Step 12 : Stop

The above algorithm terminates after some time, which explains the property of finiteness. Action of each step is
precisely defined. In this example each step requires simple operations such as reading, assigning comparing,
printing and so on. This explains the feature of definiteness and effectiveness. The total number of entries and their
values will be input for the algorithm and the average of the entered values will be the output. The above algorithm
is producing the correct result hence, it is solvable.

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 5


UNIT-1

Characteristics of Algorithms:

i. The instructions must be in a sequence


ii. The instructions must be simple
iii. There must be termination for an algorithm
iv. The repetition statements must have a termination; otherwise the program might be run into infinite loop.
v. The algorithm must be definitely solve the given problem

Advantages of Algorithms:

i. It provides a solution to a given problem. This solution can be easily implemented through a computer language.
ii. It is easy to identify the removal of logical errors in aprogram
iii. It is easy to find out the most efficient solution for given problem.
iv. It facilitates the program development by acting as a design document for the given solution.

Disadvantages of Algorithms:

i. In case of large algorithms the flow of the program control becomes defiifcult to track
ii. Algorithms lack visual representation of program constructs like flow charts.
 Flow chart
 Flowchart is a graphical representation of an algorithm.
 Flowchart is the diagrammatic representation of an algorithm. It is constructed with different types of symbols and
boxes. The operation to be performed is written inside the box. All symbols are connected by arrows among
themselves to indicate the flow of information and processing.
 The symbols used in flowcharts have been standardized by the American National Standards Institute. Arrows are
used on the connecting flow lines if the direction of flow is not clear. A flowchart should have one start and one or
more stop points and should be arranged so that the direction of processing is from top to bottom and from left to
right. Although flowlines can cross, the crossing flowlines are independent of each other. Whenever possible,
crossing of flowlines should be avoided, since it makes the flowchart difficult to read.
The Symbols used in Flow charts are:

Oval Terminal Start/Stop/Begin/End

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 6


UNIT-1

Parallellogram Input/Output Making the data


available(input) for
processing
Rectangle Process Any processing is performed.
Represents computations and
data manipulations
Diamond Decission Represents the branching to
alternative paths based on a
test

Circle On-page Connects two or more flows


Connector into one

Off-page Connects flows on different


connector pages

Directional Arrows Flow Represent direction


of processing

 Terminal symbol: The terminal symbol, which is oval shaped, represents start and end points of an algorithm. The
words START and STOP or END are usually placed as narratives within the start and end symbols, respectively.
Typically, there is only one starting point, but there may be more than one end points, one to indicate the end of
normal processing and others to indicate an exit resulting from one or more unrecoverable error conditions.
 Input/output symbol: Both input and output operations are represented by a parallelogram tilted to the right. The
nature of the operation to be performed is indicated by including the terms READ and INPUT to denote input
operations and the terms PRINT and WRITE to denote output operations.
 Processing symbol: The rectangular processing symbol is used to indicate operations involving arithmetic and data
manipulation. This symbol is also used to represent a collection of statements that perform computations.
 Decision symbol: The diamond-shaped decision symbol is used to indicate a point in the algorithm at which a
branch to one or more alternative paths is possible. The condition upon which each of the exit paths will be executed
is identified within the diamond shaped symbol. Also, flow lines leaving the corners of the diamond are labeled with
the decision results associated with each path.
 On-page connector: A small circle is used to indicate a connection between two points on the same page in a
flowchart. For complex programs containing numerous branches and loops, the number and direction of flow lines
can result in confusion. In such situations connectors are used to replace flow lines. Whenever possible, connectors
should be used instead of flow lines. Connectors are used in pairs, one indicating an exit from and the other an entry
to a point in a flowchart. The connector pairs are identically labeled.

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 7


UNIT-1

 Off-page connector: Off-page connectors are used to connect flowcharts that are too large to fit on one page. They
represent an entry or an exit from, a point in a flowchart on a different page. As with on-page connectors, they
appear in identically labeled pairs.
Example:
Flow chart to evaluate area of triangle (sqrt(s(s-a)(s-b)(s-c)

Design Rules for the Flow charts:


i) It must start with begin start and end with stop symbol
ii) The flow of chart must be either from top to bottom or left to right
iii) Instruction specified in flow chart must be as few as possible
iv) Flow of control must be clearly indicated with appropriate arrows
v) Use of connectors should be as less as possible
vi) Process or Action box should have only one input and one output.
vii) Two arrows should not be intersected each other when they are cross

Advantages of Flow charts:


i) It helps us to understand the flow of the program in easier manner
ii) Developing the program based on the flow chart is easy
iii) It helps avoiding the Symantec errors
iv) A flow chart acts as documentation for the process or program flow.
v) They are very useful in small program design
Disadvantages of Flow charts:
i) For larger programs the flow chart may become very complex and confusing
ii) Modification of the flow chart is very difficult and sometimes it may lead to redraw the entire flow chart.
iii) Excessive connectors and large flow chart may confuse the programmers
iv) It takes more time to draw the flow charts compare to writing the code or algorithm.

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 8


UNIT-1

 System Development Life Cycle


The steps of the system development life cycle are:
1. Problem definition
2. Problem analysis
3. Algorithm design
4. Program development
5. Program verification
6. Documentation
7. Maintenance and modification
1. Problem definition: The first and most important step in problem solving is to define the problem. Before we begin
to formulate a solution, it is important that we have a clear understanding of the problem to be solved. The problem
statement should be carefully examined to determine what information is given and what needs to be found. The
initial description of the problem may be somewhat vague and imprecise. It may be necessary to reformulate the
original problem statement so that it is clear and concise.
2. Problem analysis: The second step is to analyze the problem and develop a mathematical formulation. This process
requires that we have a clear understanding of the underlying concepts and principles inherent to the problem. In this
step the information needed to solve the problem is assembled. This involves identifying what information is given
(input) and what results should be computed and displayed (output). The relationships between the input and output
variables are obtained. These relationships may be expressed in the form of mathematical equations. If it is found
that additional information is required to obtain a solution, we have to determine how this information is to be
obtained, and we have to state any modeling assumptions that are made.
3. Algorithm design: The third step is to formulate the algorithm. An algorithm is a series of step-by-step instructions
for solving a problem. It should be emphasized that the algorithm must be completed before the computer program
is actually written.
4. Program development: The fourth step in the problem solving process is conversion of the algorithm into the
desired programming language. The process of writing computer instructions in a programming language such as
Basic or C is called coding.
5. Program verification: The next step is program verification. Almost all programs contain errors or "bugs". The
term debugging is used to describe the process of correcting known errors in a program, such as syntax errors. After
a program has been debugged, it is tested. Testing is conducted to determine the reliability of the program, that is, to
determine whether the program does indeed perform its intended function.
Testing usually involves running the program using data for which the correct answers are known beforehand and
also using different data sets to test the different computational paths of a program. The main objective of testing is
to uncover unknown errors in the program.
The life cycle of a program developed normally ends with the fifth step. Once the program has been developed and
tested, the assignment is complete. For programs developed for real-world applications there are two additional steps
that have to be completed. These steps are program documentation, and maintenance and modification.

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 9


UNIT-1

6. Program documentation: Program documentation is an important step in program development, but one that is
neglected much too often. Documentation should be included so that the intended users can understand the program
and use it effectively.
Internal documentation of computer code is needed to facilitate understanding of the solution's logic. External
documentation consists of a user's manual and a technical reference manual. Some programmers prefer to write a
large part of the documentation before the program is actually coded. This can be helpful since it forces them to
decide on the details of the program before the program is actually written.
7. Maintenance and modification: The last step is maintenance and modification. Programs for real-world
applications will likely be used for a number of years. As a program is used, its limitations become apparent.
Typically, more errors are also discovered. A good program usually undergoes a number of revisions. Maintenance
involves correcting errors as they are discovered as well as making modifications to the program to incorporate
additional features. Software maintenance is a major component of the life cycle of a program and may account for
as much as 80 percent of the total cost of the program.
The above steps are summarized in figure bellow:

 Program Development steps/ Execution of C Program


Execution of C program involves the following steps.
1. Creating the program
2. Compiling the program
3. Linking the program
4. Executing the program

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 10


UNIT-1

Those steps can be pictorially represented with following flow chart.

Creating phase:

i. We can create a C program in any text editor or any any integrated environment. i.e., the code must be entered into
a file.
ii. The created program is called as source code.
iii. Once the editing or creating is over, we have to save the file. For Ex: “filename.c”
iv. The File must have an extentionof “.c” for every c program file.

Compiling phase:

i. The edited/created program must be compiled with the help of c compiler.


ii. Compiler translates the source code into object code(i.e. machine understandable code).
iii. If any syntax errors are found the errors will be dispalyed. We have to edit the program, save again and we have to
compile till the syntax errors are eliminated.

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 11


UNIT-1

iv. If there are no errors, the compiler generates the object code. Ex: “filename.obj”

Linking phase:

i. The object file has to be linked with system libraries.


ii. Linking is the process of putting all functins together with object code with the help of system libraries. For Ex:
printf funnction (available in stdio.h library).
iii. Now the object code is ready for the next phase i.e execution phase. And it will create a executable file with
extention of “.exe” For Ex: “filename.exe”

Execution phase:

i. In this phase the executable object code is loaded into the memory and the program execution begins.
ii. At the time of execution, we may get some logical errors or run time errors.
iii. If any logical errors are found, we have to re edit the program and proceed to all pahses again.
iv. If we gave any wrong data as the input, we have to re run (execute) the exe file to give the coorect data as input to
get the correct output.

 Computer Languages:
The operations of a computer are controlled by a set of instructions, which is called a program. In order to write a
program, you must use a computer language. They will be written to tell the computer to perform:
i) What operation to perform
ii) Where to locate data
iii) How to present results
iv) When to make certain decisions
Types of computer Languages:

Computer
Languages

Machine Assembly High level


Language language Languages
I Generation II Generation III Generation

Procedure
Problem Oriented
Oriented Natural languages
Languages
languages V Generation
IV Generation
III Generation

1940’s Machine level Languages


1950’s Symbolic Languages/Assembly Languages
1960’s High-Level Languages

Machine Language:

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 12


UNIT-1

At the lowest level computer understands only 0 and 1. Programs expressed in terms of binary language are called
machine language. A computer’s programming language consists of strings of binary numbers (0’s and 1’s) and is
the only one language computer can understand. This language is the lowest level of computer language recognized
and used by the CPU. An instruction prepared in any machine language consists of 2 parts. The first part is the
command or opcode or operation code. The second part of the instruction is the operand/s or data and it tells the
processor where to find or store the data or other instructions that are manipulated. A short sample of machine
language to perform addition in the storage location 0166 will look like this
00010000 00000001 01100010
A machine language programmer has to know the binary code for each operation to be carried out. Machine
language programmers must also be familiar with the internal organization of the computer. A machine language
programmer must also keep track of all the addresser of main memory locations that are referred to in the program.
The machine language format is slow and tedious. We the human beings work on natural language and not on binary
language. Hence writing machine language program is difficult for the humans.

The off state can be represented by 0 and the on state can be represented by 1. Each 0 or 1 digit is referred to as a
bit.

 A bit is a single 0 or 1 in binary code.


 A byte is a sequence of 8 bits. Each byte could also be represented as a decimal number from 0 to
255.

Advantages:

i) Works very fast


ii) Directly deals with the Processors.

Disadvantages:

i) Different instruction sets are required for different hardware


ii) Machine language is that it is totally confusing and unreadable. Nobody want to memorize all these sequences of
0’s and 1’s.
Assembly language:
In the 1950’s, the first symbolic languages were created. The idea was to have each type of instruction represented
by some intuitive code word, and to allow data to be represented as decimal numbers. Here is an excerpt from a
symbolic language program:
LDA 9000
MOV B,A
MOV C,A
HLT

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 13


UNIT-1

An assembler is a special program used to translate symbolic code into machine code. Symbolic languages also
became known as assembly languages.

Assembly language Assembler Machine language

Assembler is used to convert assembly language into the machine language.


For example object program or machine language equivalent for the above assembly language is
01110 0100001100
01110011101
0111110000
1011100000
Advantages:
i. Assembly language programs are commonly used to write programs for electronic controls using microprocessors
e.g., compilers, operating systems, animation in computer graphics and so on.
ii. Assembly language is relatively easy for the human beings compared to machine language. Programs writing are
faster compared to machine language.
Disadvantages:
Assembly language programmer should know details of the architecture of the machine. Assembly language
programs are not portable.
Machine, symbolic and high level languages

High Level Language:


Assembly languages are easier to program than machine languages. However, there is still approximately a one to
one correspondence between assembly language instructions and machine language instructions, and there needs to
be a different assembly language for each type of computer. To avoid these problems, high-level languages were
developed.
Instructions which are written using English language with symbols and digits are called high level languages. The
high level language is closer to our natural language. The commonly used high level languages are FORTRAN,
BASIC, COBOL, PASCAL, PROLOG, C, C++ etc. The complete instruction set written in one of these languages is
called a high level language program or computer program or source program.
In order to execute the instructions, the source program is translated into binary form by a compiler or interpreter. A
compiler is also used to translate source program into an object program. An interpreter is a program which takes the
source program line by line and converts into machine code line by line. .
C language uses a compiler as its translator to translate or compile the complete C program. It is also necessary to
create an executable program to execute the instructions given in a source program by linking the input and output
devices with your program. A linker (another program) is used to link library routing and generate an executable
program from an object program. Compiler converts source program into object program in terms of stages called

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 14


UNIT-1

passes. Normally, most of the compilers use two passes to convert source program into the machine language
program.
Translators used:
Compiler & Interpreter
Compiler:
Compilers convert the program instructions from human understandable form to the machine understandable form
and the translated program instruction is called object code. Compiler is nothing but a language translator used to
translate the entire program of the high level language into machine language. Every programming language
requires its own compiler to translate the program. For example, the programming language PASCAL requires
PASCAL compiler and C uses C compiler.

High level language Compiler Machine language

Interpreter:

Interpreters also convert the source program to machine language instruction but execute each line as it is entered.
The translation of the source program takes place for every run and is slower than the compiled code. An interpreter
must accompany the object code to run a program. Programming languages BASIC and LISP use interpreters.

High level language Interpretor Machine language

GW BASIC is an interpreter used to convert basic program into object program.

Three benefits of high-level languages:


1) More powerful instructions; each may translate to several machine language instructions.
2) Portable to many different computers.
3) Simpler to understand and use.

Procedure Oriented Languages:

High level languages designed to solve general purpose problems and they are called procedural languages. They are
designed to express logic and procedure of a problem.

Ex: C, Fortran, Pascal, COBOL etc

Problem oriented languages:

They are used to solve specific problems and they are known as fourth generation languages. Generally these
languages uses WYSIWYG( What You See Is What You Get) environment

For ex: Querry languages like SQL, Visual C++, Visual BASIC etc.,

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 15


UNIT-1

Natural Languages:

Natural Languages behave like an expert to solve problems and tend to use artificial intelligence. These languages
are called Fifth Generation Languages and still they are in developmental stage.

Ex: LISP and Prolog are having the characteristics and features of such languages.

 Creating and Running programs:


Once we load the Operating system into the memory, the computer is ready to receive the program. The program
must be entered into a file. The file name can consist of letters, digits and special characters, followed by a dot and a
letter c.
Ex: hello.c program.c ex1.c
The file is created with the help of a text editor, we use tc.exe. double click the icon TC on the desktop. TURBOC
editor will be opened. If the file is existed before, it can be loaded. If it does not exist, the file has to be created.
After typing the instructions save it with file extension .c.
When editing is over, the file is saved on disk which can be referred later by its name. the program that is entered
into the file is known as the source program.
Compiling the program: to compile the program press ALT + F9
Now the source program instructions are transformed into a form suitable for execution by the computer. If
mistakes in syntax or semantics of the language are discovered, they are listed out and the compilation process ends
right there. The errors should be corrected in the source program with the help of the editor and the compilation is
done again.
Linking the program: Linking is the process of putting together other program files and functions that are required
by the program. The compiled and linked program is called the executable object code
Executing the program: to execute the program press CTRL+F9
would load the executable object code into the computer memory and execute the instructions.
Sometimes the program may request for some data to be entered through the keyboard. When there is wrong with
the program logic or data, then it is necessary to correct the source program or data. In case the source program is
modified the entire process of compiling, linking and executing the program should be repeated.
1.2 BASICS OF C
INTRODUCTION TO C:
C has emerged as the most widely used programming language for software development. C language supports the
powerful low level features like pointer, memory allocation, bit manipulations etc. The features of C language make
it possible to see the language for system programming like the development of compiler, interpreter, operating
system, system utilities etc.
History of C:
C was originally developed in the 1970’s by Dennis Ritchie at Bell Telephone Laboratories, Inc. It is an
outgrowth of two earlier languages, called BCPL and B, which were also developed at Bell Laboratories.

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 16


UNIT-1

Why C is Popular?
C language became popular because of the following reasons:
1. C is a robust language, which consists of number of built-in functions and operators can be used to write any
complex program
2. C compiler combines the capabilities of assembler and
3. Programs written in c are executed fast compared to other languages.
4. C language is highly portable
5. C language is well suited for structured programming.
6. C is a simple language and easy to learn.
ADVANTAGES/FEATURES OF C:
C language has become the language of choice of two decades among system programmers and application
programmers. Reason for its popularity can be summarized as follows:
1. Powerful and flexibility:
The power and popular UNIX as was written in C. The complier and interpreter for FORTAN, PASCAL, LISP, and
BASIC are written in C.
2. Portability:
C program written in one system can be run on other system with little modifications.
3. Efficiency:
The program written in C language is highly efficient like assembly language in speed and memory management.
4. Programmer oriented:
It has the flexible control structure and gives access to hardware and enables to manipulate individual bits of
memory.
5. Modularity:
C program can be modularizing for step wise refinement. The complex program can be modularize into simple
programs.
 C program structure:
C is a structured programing language. A C program consists of one or more functions. Each function is a collection
of statements, which perform a specific task in the program.
A C program in general, consists of various sections which can be elaborated as follows.

Documentaion Section /* comments of a program */  multi line comments


// Single line comments  Single line comments
Header Files Section #include <stdio.h> /* #include is a preprocessor directive through which we can
include header files */
#include <math.h>

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 17


UNIT-1

….
….
…..
Definition Section # define pi 3.1415 /* Here we can define some symbolic constants
Global variable Declaration int var1, var2 ….. var n;
Section
main Function Section main()
{ {
Declaration section int x=10;
float a = 5.25;
Executable statements printf(“The value of x is =%d, the value of a= %f”,x,a);
}
}
Sub program Section add (int x, int y)
Or {
User Defined Function Section --------
---------
}

Documentation Section:

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 18


UNIT-1

The Documentation Section consists of a set of comment lines giving the name of the program, the author and other
details, which the programmer would like to use later.
To enhance the readability of the program, programmers can provide comments about the program in this section.
They will not be executed and ignored by the compiler at compilation time. The comments are two types.
They are
1. Single line comments
The text should be started with //
Ex:
// This program caluculates the area of a triangle
2. Multi line comments
The text should be included in /* and */
For ex:
/* This program caluculates the area of a traingle */
Header files Section:
i. A C program depends on the header files to a great extent.
ii. A header file contains the information required by the compiler when call to the library functions are required.
iii. Every header file should have an extention “.h”.
iv. These must be included with the pre-procerssor directive # and a keyword include.
Syntax: #include<header file>
#include<stdio.h>
Definition section:
It is used to assign symbolic names to constants and define macros. The preprocessor directive #define is used for
this purpose.
Ex: # define pi 3.1415
In the above example pi is the symbolic constant and its value is 3.1415 and it is fixed throuh ou the execution of the
program.
Note:
i) We should not use equal operator to assign a value to the symbolic constant.
ii) We should not use statement terminator (;) at the end of declaration.
Global Variable Declaration section:
i. In this section, we can declare the varibles and they are availble in all the user defined functions if any, along with
the main() function.
ii. These variables are declared before main() function only.
main() Program Section:
i. The main() functionis the starting point of execution of C program.
ii. In every program we must use the main() function.
iii. The main function may or may not have arguments or parameters associated with it.

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 19


UNIT-1

iv. The program execution starts with open brace ({) and terminate with end brace (}).
v. The main() function consists of declarations and executable statements.
vi. The statements are executs sequentially.
vii. Every statement must be terminated with statement terminator semicolumn (;).
User defined function section:
i. C language provides the facility for the user to define their own functions.
ii. These user defined functions are defined after the main() function.
iii. This sectionis optional in C programs.
Ex:
/* To find the area of and circumference of a circle */
#include<stdio.h>
#define PI 3.1415 /* defining the symbolic constant */
main()
{
int r;
float a, c;
clrscr(); /* Clears the screen */
printf(“\nEnter radius :\n”);
scanf(“%d”,&r);
a = 2 * PI * r;
printf(“Area = %f \n”,a);
printf(“Circuference = %f \n”);
getch(); /* waits for the user to enter any character from keyboard */
}
C Character Set:
The characters that can be used to form words, numbers and expressions are called as the C character set which are
grouped into the following categories.
1. Letters.
2. Digits.
3. Special characters.
4. White spaces.
The compiler ignores the white spaces unless they are a part of string constant.
C TOKENS:
In the passage of a text individual words and punctuation marks are called tokens. In C, the smallest individual units
are called as c Tokens. C has six types of tokens that are used to write the c programs.

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 20


UNIT-1

 Keywords: Keywords are preserved words that have special meaning in C language. The meaning has already been
described. These meaning cannot be changed. There are total 32 keywords in C language.

 Identifiers, basic data types and sizes.


Identifier: An identifier is used for any variable, function, data definition, etc. In the programming language C, an
identifier is a combination of alphanumeric characters, the first being a letter of the alphabet or an underline, and the
remaining being any letter of the alphabet, any numeric digit, or the underline.
Rules for an Identifier
 An Identifier can only have alphanumeric characters ( a-z , A-Z , 0-9 ) and
Underscore (_).
 The first character of an identifier can only contain alphabet ( a-z , A-Z ) or
Underscore (_).
 Identifiers are also case sensitive in C. For example name and Name are two different identifier in C.
 Keywords are not allowed to be used as Identifiers.

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 21


UNIT-1

 No special characters, such as semicolon, period, whitespaces, slash or comma are permitted to be used in or as
Identifier.
Examples of legal and illegal identifiers follow, first some legal identifiers:
float _number;
float a;
int this_is_a_very_detailed_name_for_an_identifier;
The following are illegal (it's your job to recognize why):
float :e;
float for;
float 9PI;
float .3.14;
float 7g;

Basic data types and sizes:


 C data types are defined as the data storage format that a variable can store a data to perform a specific operation.
 Data types are used to define a variable before to use in a program.
 Size of variable, constant and array are determined by data types.

C – Data types:
There are four data types in C language. They are,
S.no Types Data Types
1 Basic data types int, char, float, double
2 Enumeration data type enum
3 Derived data type pointer, array, structure, union
4 Void data type void

1. Basic data types in C:


1.1. Integer data type:
 Integer data type allows a variable to store numeric values.
 “int” keyword is used to refer integer data type.
 The storage size of int data type is 2 or 4 or 8 byte.
 It varies depend upon the processor in the CPU that we use. If we are using 16 bit processor, 2 byte (16 bit) of
memory will be allocated for int data type.
 Like wise, 4 byte (32 bit) of memory for 32 bit processor and 8 byte (64 bit) of memory for 64 bit processor is
allocated for int datatype.
 int (2 byte) can store values from -32,768 to +32,767
 int (4 byte) can store values from -2,147,483,648 to +2,147,483,647.
 If you want to use the integer value that crosses the above limit, you can go for “long int” and “long long int” for
which the limits are very high.

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 22


UNIT-1

Note:
 We can’t store decimal values using int data type.
 If we use int data type to store decimal values, decimal values will be truncated and we will get only whole number.
 In this case, float data type can be used to store decimal values in a variable.
1.2. Character data type:
 Character data type allows a variable to store only one character.
 Storage size of character data type is 1. We can store only one character using character data type.
 “char” keyword is used to refer character data type.
 For example, ‘A’ can be stored using char datatype. You can’t store more than one character using char data type.
 Please refer C – Strings topic to know how to store more than one characters in a variable.

1.3. Floating point data type:


 Floating point data type consists of 2 types. They are,
 Float
 double
1. float:
 Float data type allows a variable to store decimal values.
 Storage size of float data type is 4. This also varies depend upon the processor in the CPU as “int” data type.
 We can use up-to 6 digits after decimal using float data type.
 For example, 10.456789 can be stored in a variable using float data type.
2. double:
 Double data type is also same as float data type which allows up-to 10 digits after decimal.
 The range for double datatype is from 1E–37 to 1E+37.
sizeof() function in C:
 sizeof() function is used to find the memory space allocated for each C data types.

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 23


UNIT-1

Modifiers in C:
 The amount of memory space to be allocated for a variable is derived by modifiers.
 Modifiers are prefixed with basic data types to modify (either increase or decrease) the amount of storage space
allocated to a variable.
 For example, storage space for int data type is 4 byte for 32 bit processor. We can increase the range by using long
int which is 8 byte. We can decrease the range by using short int which is 2 byte.
 There are 5 modifiers available in C language. They are,
a. short
b. long
c. signed
d. unsigned
e. long long
 Below table gives the detail about the storage size of each C basic data type in 16 bit processor.
Please keep in mind that storage size and range for int and float datatype will vary depend on the CPU processor
(8,16, 32 and 64 bit)

S.No C Data types storage Size Range


1 char 1 –127 to 127
2 int 2 –32,767 to 32,767
3 float 4 1E–37 to 1E+37 with six digits of precision
4 double 8 1E–37 to 1E+37 with ten digits of precision
5 long double 10 1E–37 to 1E+37 with ten digits of precision
6 long int 4 –2,147,483,647 to 2,147,483,647
7 short int 2 –32,767 to 32,767
8 unsigned short int 2 0 to 65,535
9 signed short int 2 –32,767 to 32,767
10 long long int 8 –(2power(63) –1) to 2(power)63 –1
11 signed long int 4 –2,147,483,647 to 2,147,483,647
12 unsigned long int 4 0 to 4,294,967,295
13 unsigned long long int 8 2(power)64 –1

2. Enumeration data type in C:


 Enumeration data type consists of named integer constants as a list.
 It start with 0 (zero) by default and value is incremented by 1 for the sequential identifiers in the list.
 Enum syntax in C:
enum identifier [optional{ enumerator-list }];
 Enum example in C:
enum month { Jan, Feb, Mar }; or
/* Jan, Feb and Mar variables will be assigned to 0, 1 and 2 respectively by default */
enum month { Jan = 1, Feb, Mar };
/* Feb and Mar variables will be assigned to 2 and 3 respectively by default */

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 24


UNIT-1

enum month { Jan = 20, Feb, Mar };


/* Jan is assigned to 20. Feb and Mar variables will be assigned to 21 and 22 respectively by default */
 The above enum functionality can also be implemented by “#define” preprocessor directive as given below. Above
enum example is same as given below.
#define Jan 20;
#define Feb 21;
#define Mar 22;

3. Derived data type in C:


 Array, pointer, structure and union are called derived data type in C language.
 To know more about derived data types, please visit “C – Array“ , “C – Pointer” , “C – Structure” and “C – Union”
topics in this tutorial.
4. Void data type in C:
 Void is an empty data type that has no value.
 This can be used in functions and pointers.
 Please visit “C – Function” topic to know how to use void data type in function with simple call by value and call by
reference example programs.
 Constants and Variables
Constants: Constants in C refer to fixed values that do not change during the execution of a program.

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 25


UNIT-1

Integer Constants
An integer constant refers to a sequence of digits, There are three types integers, namely, decimal, octal, and hexa
decimal.
Decimal Constant
Eg: 123,-321 etc.,
Note: Embedded spaces, commas and non-digit characters are not permitted between digits.
Eg: 1) 15 750 2)$1000
Octal Constant
An octal integer constant consists of any combination of digits from the set 0 through 7, with a leading 0.
Eg: 1) 037 2) 0435
Hexadecimal Constant
A sequence of digits preceded by 0x or 0X is considered as hexadecimal integer. They may also include alphabets A
through F or a through f.
Eg: 1) 0X2 2) 0x9F 3) 0Xbcd
Program for representation of integer constants on a 16-bit computer.
/*Integer numbers on a 16-bit machine*/
main()
{
printf(“Integer values\n\n”);
printf(“%d%d%d\n”,32767,32767+1,32767+10);
printf(“\n”);
printf(“Long integer values\n\n”);
printf(“%ld%ld%ld\n”,32767L,32767L+1L,32767L+10L);
}
OUTPUT
Integer values
32767 -32768 -32759
Long integer values
32767 32768 32777

Real Constants
Certain quantities that vary continuously, such as distances, heights etc., are represented by numbers containing
functional parts like 17.548.Such numbers are called real (or floating point)constants.
Eg:0.0083,-0.75 etc.,
A real number may also be expressed in exponential or scientific notation.
Eg:215.65 may be written as 2.1565e2
Single Character Constants

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 26


UNIT-1

A single character constants contains a single character enclosed within a pair of single quote marks.
Eg: ’5’
‘X’
‘;’
String Constants
A string constant is a sequence of characters enclosed in double quotes. The characters may be letters, numbers,
special characters and blank space.
Eg:”Hello!”
“1987”
“?….!”

Rules for Constructing String constants:


1. A string constant may consist of any combination of digits, letters, escaped sequences and spaces enclosed in double
quotes.
2. Every string constant ends up with a NULL character which is automatically assigned (before the closing double
quotation mark) by the compiler.
Backslash Character Constants
C supports special backslash character constants that are used in output functions.
These character combinations are known as escape sequences.

Variables:
Definition:
A variable is a data name that may be used to store a data value. A variable may take different values at different
times of execution and may be chosen by the programmer in a meaningful way. It may consist of letters, digits and
underscore character.
Eg: 1) Average

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 27


UNIT-1

2) Height
Rules for defining variables
 They must begin with a letter. Some systems permit underscore as the first character.
 ANSI standard recognizes a length of 31 characters. However, the length should not
be normally more than eight characters.
 Uppercase and lowercase are significant.
 The variable name should not
 Expressions and Operators

Like in any other programming language, in C, there are a number of arithmetic relational and logical operators we
can use to write expressions that are made up of simpler basic types.

Operators are the symbol which operates on value or a variable. For example: + is a operator to perform addition. C
programming language has wide range of operators to perform various operations. For better understanding of
operators, these operators can be classified as:

1. Arithmetic Operators

Operator Meaning of Operator

+ addition or unary plus


- subtraction or  unary minus
* multiplication
/ division
% remainder after division( modulo division)

Example of working of arithmetic operators


/* Program to demonstrate the working of arithmetic operators in C. */
#include <stdio.h>
int main(){
int a=9,b=4,c;
c=a+b;
printf("a+b=%d\n",c);
c=a-b;
printf("a-b=%d\n",c);
c=a*b;
printf("a*b=%d\n",c);
c=a/b;
printf("a/b=%d\n",c);

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 28


UNIT-1

c=a%b;
printf("Remainder when a divided by b=%d\n",c);
return 0;
}
a+b=13
a-b=5
a*b=36
a/b=2
Remainder when a divided by b=1
Explanation:
Here, the operators +, - and * performed normally as you expected. In normal calculation, 9/4 equals to 2.25. But,
the output is 2 in this program. It is because, a and b are both integers. So, the output is also integer and the compiler
neglects the term after decimal point and shows answer 2 instead of 2.25. And, finally a%b is 1,i.e. ,when a=9 is
divided by b=4, remainder is 1.
Suppose a=5.0, b=2.0, c=5 and d=2
In C programming,
a/b=2.5
a/d=2.5
c/b=2.5
c/d=2
Note: % operator can only be used with integers.
2. Increment and Decrement Operators
In C, ++ and -- are called increment and decrement operators respectively. Both of these operators are unary
operators, i.e, used on single operand. ++ adds 1 to operand and -- subtracts 1 to operand respectively. For example:
Let a=5 and b=10
a++; //a becomes 6
a--; //a becomes 5
++a; //a becomes 6
--a; //a becomes 5
Difference between ++ and -- operator as postfix and prefix
When i++ is used as prefix(like: ++var), ++var will increment the value of var and then return it but, if ++ is used as
postfix(like: var++), operator will return the value of operand first and then only increment it. This can be
demonstrated by an example:
#include <stdio.h>
int main(){
int c=2,d=2;
printf("%d\n",c++); //this statement displays 2 then, only c incremented by 1 to 3.

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 29


UNIT-1

printf("%d",++c); //this statement increments 1 to c then, only c is displayed.


return 0;
}
Output
2
4
3. Assignment Operators

Operator Example Same as

= a=b a=b
+= a+=b a=a+b
-= a-=b a=a-b
*= a*=b a=a*b
/= a/=b a=a/b
%= a%=b a=a%b

The most common assignment operator is =. This operator assigns the value in right side to the left side. For
example:
var=5 //5 is assigned to var
a=c; //value of c is assigned to a
5=c; // Error! 5 is a constant.
4. Relational Operators

Operator Meaning of Operator Example

== Equal to 5==3 returns false (0)


> Greater than 5>3 returns true (1)
< Less than 5<3 returns false (0)
!= Not equal to 5!=3 returns true(1)
>= Greater than or equal to 5>=3 returns true (1)
<= Less than or equal to 5<=3 return false (0)

Relational operators checks relationship between two operands. If the relation is true, it returns value 1 and if the
relation is false, it returns value 0. For example:
a>b
Here, > is a relational operator. If a is greater than b, a>b returns 1 if not then, it returns 0.

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 30


UNIT-1

Relational operators are used in decision making and loops in C programming.


5. Logical Operators

Operator Meaning of Operator Example

Logial If c=5 and d=2 then,((c==5) && (d>5))


&& AND  returns false.
Logical If c=5 and d=2 then, ((c==5) || (d>5))
|| OR returns true.
Logical
! NOT If c=5 then, !(c==5) returns false.

Logical operators are used to combine expressions containing relation operators. In C, there are 3 logical operators:
Explanation
For expression, ((c==5) && (d>5)) to be true, both c==5 and d>5 should be true but, (d>5) is false in the given
example. So, the expression is false. For expression ((c==5) || (d>5)) to be true, either the expression should be true.
Since, (c==5) is true. So, the expression is true. Since, expression (c==5) is true, !(c==5) is false.
6. Conditional Operators
Conditional operator takes three operands and consists of two symbols ? and : . Conditional operators are used for
decision making in C. For example:
c=(c>0)?10:-10;
If c is greater than 0, value of c will be 10 but, if c is less than 0, value of c will be -10.
7. Bitwise Operators
A bitwise operator works on each bit of data. Bitwise operators are used in bit level programming.

Operators Meaning of operators

& Bitwise AND


| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right
Bitwise operator is advance topic in programming. Learn more about bitwise operator in C programming.
8. Special Operators
Comma Operator
Comma operators are used to link related expressions together. For example:
int a,c=5,d;

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 31


UNIT-1

The sizeof operator


It is a unary operator which is used in finding the size of data type, constant, arrays, structure etc. For example:
#include <stdio.h>
int main(){
int a;
float b;
double c;
char d;
printf("Size of int=%d bytes\n",sizeof(a));
printf("Size of float=%d bytes\n",sizeof(b));
printf("Size of double=%d bytes\n",sizeof(c));
printf("Size of char=%d byte\n",sizeof(d));
return 0;
}
Output
Size of int=4 bytes
Size of float=4 bytes
Size of double=8 bytes
Size of char=1 byte

READING A CHARACTER:
getchar( ): Reading a single character can be done by using the function getchar( );
varname = getchar( );
Ex: Where varname is a valid C identifier. When this statement is encountered, the computer waits until a key is
pressed and then assigns this character as a value to getchar function. Since getchar() is used on the right hand side
of an assignment statement, the character is assign to the variable.
putchar (var-name)
char ch; ch=getchar( ); putchar(ch); putchar(‘\n’);
is used to write character on the terminal.Where var-name is a character var or char constant. putchar (‘\n’); would
cause the cursor on the screen to move to the beginning of the next line. char c= ‘a’;
putchar(c);
scanf( ) Function :
Input data can be entered into the computer from a standard input device by means of the C library function
scanf().This function can be used to enter any combination of numerical values, single characters and strings.
scanf (control_string, &arg1, &arg2,……&argn);

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 32


UNIT-1

Where control string refers to a string containing certain required formatting information and arg1, arg2,…. argn are
arguments that represent the individual input data items.
%c single character
%d decimal integer
%e floating point value
%f floating point value
%s string
%x Hexa decimal
%o Octal integer
%u unsigned decimal integer
Each variable name must be preceded by an ampersand (&). The arguments are actually pointers, which indicate
where the data items are stored in the computer memory.
int a;
float b;
char c;
scanf(“%d”, &a); scanf(“%f”,&b); scanf(“%c”,&c);
scanf(“%d%f%c”,&a,&b,&c);
char name[20]
scanf(“%s”, name);
printf( ) Function:
Output data can be written from the computer on to a standard output device using the library printf().This function
can be used to output any combination of numerical values, single characters and strings.
printf (control-string, arg1, arg2,……..);
Where control string refers to a string that contains formatting information, and arg1, arg2 are arguments that
represent individual output data items.
%c single character
%d decimal integer
%e floating point value
%f floating point value
%s string
%x Hexa decimal
%o Octal
int x;
x=20; output printf (“%d”,x); 20
printf (“x=%d”,x); x=20
printf(“value of x=%d”,x) value of x=20
int a,b; a=10; b=20;

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 33


UNIT-1

printf(“%d %d”,a,b); 10 20
printf(“a=%d b=%d”,a,b); a=10 b=20
Assume i=integer; d=double; c=character pow() is a library function for exponential
abs(i) returns positive value of the given integer
ceil(d) returns the nearest integer greater than the number
cos(d) returns cosine of the given value
exp(d) returns e to the power d
fabs(d) returns positive value of the given integer
floor(d) returns the nearest integer less than the number
log(d) returns the logarithm of the given value
tolower(c) returns the lowercase character of the given character
toupper(c) returns the upper case character of the given character to
ascii(c) returns the ASCII value of the given character
sqrt(d) returns the square root of the given number

Creating and running programs:


demo.c
#include<stdio.h>
main()
{
printf("Hello World!\n");
}
For compilation: gcc demo.c (gcc programname.c)
For execution: ./a.out or ./demo
Write a program to read two integer values and to print the results of various arithmetic operations on them.
Program:
#include <stdio.h>
void main()
{
int a, b;
printf("Enter two integers : ");
scanf("%d %d", &a, &b);
printf("%d + %d = %d\n", a, b, a + b);
printf("%d - %d = %d\n", a, b, a - b);
printf("%d * %d = %d\n", a, b, a * b);
printf("%d / %d = %d\n", a, b, a / b);
printf("%d %% %d = %d\n", a, b, a % b);
}
Output:

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 34


UNIT-1

Enter two integers :


20
30
20+30=50
20-30=-10
20*30=600
20/30=0
20%30=20

PROGRAMMING FOR PROBLEM SOLVING-CSE Page 35

You might also like