Introduction To Computers: Unit 1
Introduction To Computers: Unit 1
Introduction To Computers: Unit 1
PD PD
!
W
W
O
O
N
N
y
i
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr e tr re
ar
.
.
ac ac
k e r- s o ft w k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
that tells the computer what to do and how to perform the given task scales are devised in such a manner that is suitable alignment of one
of the user is known as computer software. The main objective of scale against the other .Multiplication and division etc may be formed
software is to enhance the performance capability of hardware. by using Slide Rule.
Generally software is deigned to perform only one task or a few
similar task. Example:-Windows XP, MS Word, MS Excel etc.
Firmware
The program stored in ROM is called firmware and hardware is
used to write a program in the ROM. Many computer manufacturer
company make essential software, required to start up of the computer
and available in read only memory chips.
3. Discuss briefly about the history of computing and computers.
[Pokahara University 2008]
Ans: The development of personal computer has taken a long period of time
to extend the use of computer at present days. There were not the
advanced technologies in the ancient days. So, it passed through a
several phases to become this phase at present days. The development
of computer from ancient days to present days is as follows:
A. Calculating Devices
a. Abacus
Abacus is a counting machine, which consists of beads divided
into two parts, which are movable on the rods or wires of two parts.
The upper part is called heaven and each bead has a value equal to 5.
The lower part is called earth and each bead has a value equal to 1.
While calculating, the beads are brought near to the mid-bar. Its exact
origin is unknown. It may have originated in china, Egypt or Greece.
The abacus is still used in some parts of the world today.
b. Napier’s Bone
In 1614, a Scottish mathematician John Napier first published
the table of logarithms, i.e. the tabular arrangement of number in such
a way that we could do multiplication and other calculations.
In 1617, Napier also developed a set of numbered rods for
multiplication, division and extraction of roots. These rods were made
up of strips of bones on which numbers were carved and painted,
which is often called Napier bones.
c. Slide Rule
William oughtred, an English mathematician invented slide
rule in 1620 and was based on the principle of logarithms. It consists
of two movable marked scales, one of which slides over the other. The
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Hungarian mathematician, John Von Neumann gave two ideas:
to repeat certain instructions. So, she got the idea of loop in the
program. To honor her contribution, the programming language used • Binary number system (instead of decimal number system)
in American defense was named as ‘ADA’. can be used in building computers.
f. Hollerith’s Tabulator • Computer instruction as well as the data being manipulated
In 1887, an American statistician, Herman Hollerith could be stored internally in the machine (Store Program
constructed a tabulating machine to compute the statistics of 1890 US Concept)
census. He used the punched cards to store data, with the holes
representing data values. This machine can read 200 punched cards
per minute. In 1896, he established the Tabulating Machine Company
to make and sell his invention. Later in 1923, this firm merged with
other companies to form the IBM Corporation, which is the most
popular and biggest company in computer manufacturing.
C. Electromechanical Computers
a. Mark–I
In 1937, Harvard professor, Howard H. Aiken, constructed an
electromechanical computer named mark-I, which was the first
machine that could perform according to pre-programmed instruction
automatically.
It was 51 ft. long, 8 ft. height and 3ft. wide using 18,000
vacuum tube with 7 lakhs 50 thousands parts and weighed approx. 32
tons. Being a huge machine, it takes more time in calculation.
b. Atanasoff Berry Computer (ABC)
In 1938, John Atanasoff and his student Clifford Berry
designed Atanasoff Berry Computer (ABC) for solving systems of
simultaneous equation. It had a memory consisting 45 vacuum tubes.
D. Electronic Computers
a. Electronic Numerical Integrator and Calculator (ENIAC)
In 1946, John Presper Eckert and John W. Mauchly developed
ENIAC. It was the first electronic computers.This machine was built
to meet the needs of US armed forces to use in world war II. It was
300 times faster than any other device of these days. It was huge in
size with 18,000 vacuum tubes, produced more heat as it consumed
180 kilowatts of power and needed to be cooling system.
b. Electronic Discrete Variable Automatic Computer
(EDVAC)
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
E. Fifth Generation Computer(Present-Beyond): AI
b. They were more reliable and accurate than the first
The features of fifth generation computers are:
generation computer.
c. Punched cards and magnetic tape were used for input (a) The development of the fifth generation of computer
while punched cards and paper constituted the output. system is characterized mainly by the acceptable of
d. They had faster and larger primary and secondary parallel processing.
storage in comparison to first generation computers.
e. They required less electricity and emitted less heat.
f. They were much easier to program and use than first
generation computer. Hence, they had wider commercial
use.
C. Third Generation Computer (1965-1975): Integrated
Circuits
The main features of third generation computers are:
(a) They were much smaller than second generation
computer. So, it requires smaller space.
(b) They had faster primary and auxiliary memory as
compared to second generation computers.
(c) They consumed much less power than second
generation computers.
(d) The operating speed of computer was nanoseconds.
(e) They were reliable, better performance and had larger
storage capacity.
(f) The size of computer became much smaller.
(g) They were used for both scientific and commercial applications.
D. Fourth Generation Computer (1975-present):
Microprocessor
The main features of fourth generation computers are:
(a) The computers were smaller, cheaper, highly reliable and
accurate.
(b) The operating speed was in Picoseconds. (c)
They were used for general purposes.
(d) Magnetic disks became common source of internal
storage.
(e) Micro computers were introduced.
(f) Application software for microcomputer became more
popular.
(g) Further refinement of input/output devices took place.
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
computer, is a semiconductor device consisting of electronic
The operating speed was increased up to the microsecond range (10-6
logic circuits manufactured by using VLSI technique. Then in 1971,
sec).They were more reliable and accurate than first generation computers.
American Intel Corporation developed first microprocessor
They had larger, faster primary and secondary storage as compared to first
generation computer. There was an overall reduction in system and operating
costs.
Second generation computers were used in payroll, inventory control,
marketing and production planning. Examples:-IBM 7090, IBM 7094I
IBM 7094II, CDC 1604, IBM 1620, IBM 1401, Honey well 400 etc
c. Third generation computer (1965 – 1975) : ICs
The third generation computers replaced transistors with “Integrated
Circuits (IC)” popularly known as chips.IC was developed by Jack St. Clair
Kilby and Robert Noyce in 1958.
Initially,SSI and MSI could replace transistors, and later on, the
semiconductor memories (i.e. RAM and ROM) replaced the magnetic core
memory as primary storage medium. The magnetic disk was able to replace
magnetic tape as secondary storage medium, keyboards and video display
terminals were introduced. Cache memory was also introduced.
Multiprocessing, multiprogramming, multi-user system were introduced. The
concept of virtual memory was introduced.
The operating speed was increased to nanoseconds, 10-9 sec.
Less manpower was required at assembly stage. They were smaller in size but
had high memory capacity.ANSI FORTRAN and ANSI COBOL and time
sharing operating system was used.PL/1, PASCAL, BASIC were notable high
level language of the 3rd generation. Examples:-IBM system/360, ICL 1900,
IBM 370 series, Honeywell
2200 series, CDC 7600, STAR-100 etc
d. Fourth generation computer (1975-Present) :
Microprocessor
The fourth generation computer used LSI (Large Scale Integration)
and VLSI (Very Large Scale Integration) circuits for internal operations.
A single silicon chip of LSI or VLSI has thousands of ICs built on it.
This chip reduced the physical size of the computer and increased their
power, efficiency and reliability. The speed of operation is measured in
pico-seconds (10-12 sec). Because of the VLSI technology, it becomes possible
to build many computing functions on a single chip.
The microprocessor, which is the main component of fourth generation
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
C
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
B
Symbol of transistor
E
IC (Integrated Circuit)
An Integrated Circuit (IC), also called a microchip, is an
electronic circuit consisting of a large number of electronic
components like transistors, resistors and capacitors placed on a single
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Introduction to Computers …13 2. Explain classification of computers based on size and capabilities.
[Pokhara University 2005, Semester-Spring]
1.2 Classification of Computer Ans: Types of computers on the basis of size are:
1. Differentiate between analog and digital computer. Also, draw block a) Super computer
diagram of digital computer. Super computers are the most powerful,largest, fastest and a
[Pokhara University 2009, Semester-fall] single problem oriented machine.Super computers are multi-user,
Ans: Differences between Analog and Digital Computer: multiprocessor very large machine.These computers are capable of
handling huge amounts of calculations that are beyond human
Analog Computer Digital Computer
a. Analog computer is one a. Digital computer works with
which is designed to work on binary digits (0 and 1) or it is a
numerical data represented machine that operates on data
by physical quantity (i.e. expressed in discontinuous
rotation or displacement or signals.
temperature).
b. It is specific or not versatile. b. It is general or versatile
machine.
c. The accuracy of this c. The accuracy of this computer
computer is very low is very high.
d. It has low storage capacity. d. It has high storage capacity.
e. It is costly. e. It is low in cost.
f. Have fewer functions. f. Have large number of functions
g. It is used only in scientific, g. It is mostly used for statistical
industrial and medical fields. data, numerical problems of
business and sciences.
h. Example of analog device is h. Examples: IBM PC, IBM
speedometer of an compatible etc.
automobile.
i. The symbolic representation i. The symbolic representation of
of analog computer is digital computer is
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
14 … A Complete Refresh Manual of C Programming (DEC). Minicomputers are commonly used as servers in network
environment and hundreds of personal computers can be connected to
capabilities. They can perform billions of instructions per second the network with a minicomputer acting as server like mainframes,
(BIPS). Some of the today’s super computers have the computing minicomputers are used as web servers. They have been widely used
capability equal to that of 40,000 microcomputers. A Japanese
as data processing, engineering analysis, industrial application
supercomputer has calculated the value of PI ( ) to 16 million decimal etc.Examples: AS 400, PRIME Series.PDP-11, VAX 7500, HCL,
places. These computers cost in 15 – 20 million-dollar range (most
MAGNUM etc.
expensive). They need large space and temperature controlled room
Super computers are typically used for number crunching
including scientific application, weather forecasting, simulations,
creating computer (animated) graphics, analysis of geological data
(e.g. in petrochemical prospecting), structural analysis, computational
fluid dynamics, electron designing, nuclear energy research and
metrology.
Examples: CRAY–1, CYBER 205, CDC–Star 100, NEC 500,
PARAM and ANURAG etc.
Among them, PARAM and ANURAG are supercomputers
produced by India and are exported to many European countries.
b) Mainframe Computer
Mainframe computer is a large, multi-user, general purpose
computer system and supports wide range of input and output
terminals with a large backing storage capacity. It supports large
number of terminals up to 100. It works on time sharing basis. It
requires an area of 1000 sq. ft.
Mainframes use proprietary operating systems, most of which
are based on UNIX, and a growing number on Linux. Mainframe
computers are generally used in big organization or government for
large scale data processing, credit card processing, bank account
management, generating life-like animation etc.
Example - IBM 4300 series, ICL 39 series, CDC 6600, IBM
1401 and CYBER 170 etc.
c) Mini Computer
Mini Computers are similar to the mainframe computers but
they are general purpose computer which are smaller, less powerful,
and have lower cost than mainframe computers but bigger than micro
computers. So they are called mid-range computer. They can support
50 terminals (i.e. 50 persons can work at the same time with single
minicomputer). They require area of 100 sq. ft. The first minicomputer
was introduced in the mid-1960s by Digital Equipment Corporation
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
places of decimal as it is necessary. Digital computers are mostly used in the measure heart function, temperature and other signs of patients.
preparation of reports, results, tabulation and graphic representations. Thus, Then these measurements are converted into digital signal and
they help in mathematics, engineering, business, accounting etc. So, fed to the digital computer that monitors the patient’s vital signs.
they are versatile in nature. Most of the computers used in Nepal are
digital.Examples of digital devices are calculators,digital watches, and digital
thermometers etc.Examples of digital computer are: IBM PC, IBM
compatible etc.
The symbolic representation of digital computer is
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
CU
Memory
Primary Memory
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Secondary Memory
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
that can be directly accessed by the CPU which constantly interacts
(ii) CU (Control Unit)
with it, retrieves data stored there in, goes through instructions and
It is a component of a CPU which controls all the tasks of
execute them as per the requirement.
computer. It also controls all other devices like input, output devices
Previously William tubes, delay lines or rotating magnetic
and storage connected to the CPU. It maintains order and directs
drums were used as primary memory which was later replaced by
functions of the entire system. It fetches instruction from the memory
magnetic core memory.
and interprets the program instruction to know what tasks are
performed and sends suitable control signals to other components to
perform further necessary steps to execute instruction.
Functions of CU are:
• The computer accepts information in the form of programs and
data through input devices and stores it in memory.
• Information stored in the memory is fetched, under program
control, into an arithmetic and logic unit where it is processed.
• Processed information leaves the computer through output
devices.
• All activities inside the computer are directed by control unit.
(iii) Register
Register is a special purpose temporary storage location in the
CPU that is designed for storing data temporarily during the program
execution. They are quite differing from the locations in main
memory, although they can similar in structure. Register quickly
accept, store and transfer data and instruction that they are used
immediately. The number of registers differs from processor to
processor. The various types of registers available in the market from
previous days to today’s are 8-bit, 16-bit, and 32-bit and 64-bit
registers.
C. Memory
Memory is essential component of a digital computer system. It is the
place where computer stores program and data that help the program
in carrying out its operations.
There are two types of memory and these are:
(a) Primary Memory
The primary memory is a fast, compact and lighter memory
which stores programs along with data that are required to execute
user's program. When we launch our program, it is loaded into and run
main memory. If we enter new data, it is also stored in the main
memory. It is a temporary memory and consists of some chips either
on motherboard or on a small circuit board attached to the
motherboard of a computer system. Primary memory is the memory
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y f. Primary memory is not used f. Secondary memory is used as
y
bu
bu
to
to
as backup storage.
ww
ww
om
om
backup storage for present and
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
future use.
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
b. Primary memory is faster, b. Secondary memory is slower
compact and lighter. and heavier.
c. Primary memory consumes c. Secondary memory consumes
less power. more power.
d. There is no rotating part in d. There is rotating part or tape
primary memory. in secondary memory.
e. Primary memory is e. Secondary memory is cheaper
expensive than secondary than primary memory.
memory.
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
The language closer to what machines can understand.
Hence, also, known as machine oriented language. This is also
processor dependent likewise machine language. Assembly
language is an example of this. In this language each machine
language instruction is assigned a code. So, instead of having
to remember a string of 0's and
1's, programmer would only need to remember short codes like
ADD, MOV, SUB, MUL etc.
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Types of high level language are — natural language. They will use in coming generation computers to
communicate humans with computers i.e. they will use in the areas of
Procedure Oriented Language : 3GL
artificial intelligence and expert system.
The high–level languages, which require step-by-step sequence
Two popular natural languages are LISP (List processor) and
of instructions to solve a variety of problems (general problems), are Prolog (programming logic). Instructions written in such languages
called procedural-oriented languages or third generation languages (or often suffer from ambiguities.
General purpose programming languages). They are designed to help
the programmer in order to express the logic, the procedure, of a
problem in the form of a program. Examples of procedural languages
are BASIC, FORTRAN, C, C++, COBOL, FORTRAN etc.
Advantages
(a) Machine independent.
(b) Programmer is clear about the logic of the program. So he can
make any modification in the program.
(c) Requires less time to write.
(d) Easy to understand / learn.
Disadvantages
(a) Takes more time to execute the program
(b) Programmer should have the in-depth knowledge of the
programming language.
Problem Oriented Language : 4GL
Problem–oriented languages or fourth generation languages are
high–level language designed to solve specific problems or develop
specific applications by enabling the user to describe what he wants
rather than step by step procedures, i.e. it simply describes the
necessary input/output requirements. Sometimes these languages are
called non-procedural languages, as the programmer need not provide
the logic to perform a task. They are mostly database-oriented
language. Examples of 4 GL are: dBase III, Oracle, FoxPro, SQL
(Structured query language), QBE (Query By Example) etc.
Advantages
a. Machine independent.
b. Programmer need not provide the logic to perform a task.
c. Define report on the screen and form layouts including format
to be printed
d. Define input data and validation check
Disadvantage
a. Requires more memory as compared to 3 GL.
D. Natural Language : 5GL
Languages used for everyday communication are known as
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
language.
[T.U. 2069 Ashad, Pokhara Uiversity 2010, Semester-
Fall] Ans: Differences between low level and high level language
Low level Language High Level Languages
(a) It is machine dependent (a) It is machine independent
language. language.
(b) It is hard to learn. (b) It is easy to learn and
understand.
(c) Requires more time to write (c) Requires less time to write.
the program.
(d) It is older program. (d) It is newer program.
(e) Debugging is more (d) Debugging is easier
complex
(f) Uses assembler to (e) Programs are translated
generate object code using compiler or
interpreter to generate
object code
5. Differentiate between 3GLs & 4GLs.
Third-generation Fourth-generation Languages
Ans: Differences between 3GL and 4GL:
Language
1. Intended for use by 1. May be used by a non-
professional programmers. programming end user as well
as a professional programmer.
2. Requires specification of how 2. Requires specification of what
to perform tasks. task is to be performed
(system determines how to
perform the task.)
3. All alternatives must be 3. Default alternatives are built
specified. in; an end user need not
specify these alternatives.
4. Require large number of 4. Require far fewer instructions.
procedural instructions.
5. Code may be difficult to read, 5. Code is easy to understand
understand and maintain. and maintain because of
English-like commands.
6. Language developed for batch 6. Language developed
operation. primarily for on-line use.
7. Can be difficult to learn. 7. Easy to learn.
8. Difficult to debug. 8. Easy to debug.
9. Typically file-oriented. 9. Typically database-oriented.
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
high- referred and has to language. com rpreter, helps to translate the source code to object code and gives error
The level as a convert A pute messages after translations.
program langua source into program, r or B. Interpreting Process
written in ge is program machine called inte Interpreting is a program that converts each high level program
statement in to machine code just before the program statement is to
Step1: Write programs in any editor or compiler(e.g. C source code
be executed. Translation and execution occur immediately one
have extension .C),compile the programs using compiler for
statement at a time.
checking errors.
Step2: If there is no any type of errors arised,then the compiler Program Interpreter Executible
produces object file(i.e. .obj file). Statement Statement
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
words, break a complex program into smaller, more manageable
(e) Programming testing and debugging
At this stage, we are ready to have next programming step, modules that can be reviewed and inspected before moving to the next
testing. We must first verify that our program has been written out and level of detail. Step-wise refinement is a powerful paradigm for
transcribed correctly and does what it is supposed to do. Computer developing a complex program from a simple program by adding
programs do not work properly the first time. The programmer might features incrementally.
have made logical errors in sequencing or perhaps errors were made in
entertaining the code. The process of correcting such error is called
debugging. The elimination of errors is lengthy and expensive process.
Care should be taken to see that all the errors are corrected. Once all
the errors have been made and corrected, the programmer is ready to
use program.
(f) Documentation
Documentation is an important part of software development.
This should be carried out simultaneously with design. Documentation
is used to keep track of the design process and implementation and
testing. There are two type of documentation, they are:
i. System documentation: System documentation includes
functional description, introductory manuals, reference
manuals, installation manuals, user manuals and so forth.
ii. Program documentation: The program documentation
includes a phase of program development documents. It should
include:
* Statement of problem
* Glossary of i/o values.
* Description of each module of program
* Error message produced by program
* Test data to be used in program modification.
10. Briefly explain what do you understand by stepwise refinement of
the program?
Ans: The concept of “Stepwise refinement” means to take an object and
move it from a general perspective to a precise level of detail and this
cannot be done in one jump but in steps. The number of steps needed
to decompose an object into sufficient detail is ultimately based on the
inherent nature of the object. “Stepwise refinement” of program
represents a "divide and conquer" approach to design. In simple
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
One simple way to find the location of the error is to use print IF Condition?
statement to display the values of the variables. Once the location of
the error is found, the error is corrected and debugging statement may Statements
be removed.
12. What is control structure? Explain different types of control
structure. b) Two-way branching: IF … ELSE statement
Ans: The order in which program instructions are performed by the c
(
computer must be carefully controlled and programming language A e
contains features that allow the order of instruction execution to be )
controlled. They are called control structures. i.e., program control
structures are used to regulate the order in which program statements S
are executed. e
They fall into three categories. [T.U. 2062 Baisakh] q
(A) Sequence u
(B) Selection e
n
(C) Repetitions (Iterations)
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Syntax:
IF <Condition> THEN
Statement1
ELSE
Statement 2
END IF Flowchart:
Yes
No
Condition?
Normally the statements are executed sequentially as written in Statement -1 Statement -2
the program. In other words, when all the operations specified in
particular statements are executed, the statement appearing on the next
line of the program is taken up for execution. This is also known as Fig. Flowchart of IF .... THEN.. ELSE Statement
the normal flow of control.
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
CASE case1
Statement 2 Do Statement1 Y
ELSE CASE case2 Case1 Statement1
tatement 3 Do Statement2
…. N
ENDIF …. Y
CASE Default: Case2 Statement1
Example: Do StatementN
END SELECT N .
IF A>B AND A>C THEN .
.
PRINT”A IS GREATER” .
Example: N
ELSE IF B>A AND B>C THEN SELECT choice StatementN
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
y esday” PRINT
P
CASE 4
” “Thursday” PRINT
R
CASE 5
“Friday”
I
CASE 6
P PRINT “Saturday”
N
CASE 7
R
T
N
“
T
T
“
e
W
s
e
d
d
a
n
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
CASE DEFAULT:
PRINT “Wrong choice”
END SELECT
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Initialize
Flowchart for WHILE -- LOOP
Example: true
A=1
Condition? Statement Increment
WHILE A<=10
PRINT A
A=A+1 False
END WHILE Example:
(b) REPEAT – UNTIL <condition> OR DO –WHILE <condition> FOR A =1 TO 10 STEP 1
statement PRINT A
-for trailing decisions (at the end of the loop) END FOR
-condition must be false to stay in the loop
(Used in Pascal) 13. What is an algorithm? What are the rules to write algorithm. Write
Syntax: REPEAT down its advantages.
Statements Ans: Algorithm
UNTIL(Condition) [Pokhara University 2004, 2005, 2009 & 2006, 2010]
The general syntax of DO…WHILE statements is Algorithm can be defined as a sequence of instructions designed in a
Syntax: DO manner that, if the instructions are executed in the specified sequence,
Statements the desired results will be obtained. It is named after its Arabian
WHILE (Condition) inventor Musa Al-Khowarizmi of Baghdad (Iraq) means receipe, A
sequence of instruction must posses the following characteristics:
Flowchart: [Pokhara University 2004, 2008]
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Step 6 : Is A less than 100? program documentation, which is needed for various purposes.
If yes, repeat step 3 onwards d. Efficient Coding: The flowcharts act as a guide or blueprint
If no, go to step 7 during the systems analysis and program development phase.
Step 7 : Print sum e. Proper Debugging: The flowchart helps in debugging process.
Step 8 : Stop
18. Design an algorithm to generate nth member of the Fibonacci
sequence.
Ans: An algorithm to generate nth number of Fibonacci sequence is:
Step1: start
Step2: Scan the number ‘n’ upto which the series to be generated.
Step3: Initialize Sum=0, x=0, y=1 and i=3.
Step4: Print x and y as the part of series.
Step5: Repeat a-e until i<=n
a. Calculate Sum=x+y
b. Print Sum as part of series.
c. Assign y to x
d. Assign sum to y
e. i=i+1
Step6: Stop.
19. What is a flowchart? What are the advantages and disadvantages of
flowchart?
Ans: Flowchart [T.U. 2061 Poush]
[Pokhara University 2009, 2006, 2011]
A flowchart is the symbolic representation of the step by step solution
of a given problem, and it indicates the flow of the entire process, the
sequence of data input, operations, computations, decisions, results
and other relevant information, pertaining to a particular problem. It
uses different shape boxes to denote different types of instructions.
The actual instructions are written within these boxes using clear and
vivid statements. These boxes are connected by lines having arrow
marks to indicate the way of operation, the exact sequence in which
the instructions are to be executed.
Advantages of flowchart
a. Communication: Flowcharts are better way of communicating
the logic of a system to all concerned.
b. Effective analysis: With the help of flowchart, problem can be
analysed in more effective way.
c. Proper documentation: Program flowcharts serve as a good
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
44 … A Complete Refresh Manual of C Programming Ans: Differences between algorithm and flowchart: Algorithm
Flowchart
21. Differentiate between algorithm and flowchart with examples.
a. It is pictorial representation a. It is a sequence of
Diamond shape Making decision of a program. instructions of a program.
Flow lines Flowing way b. It takes more time because b. It takes less time because we
Circle Connector we need to draw symbols. need to write sequence of
instructions.
Inp ut A, B
and B.
A > B?
iii. If yes, then display "A is
the biggest." Prin t " B is g reater" P rin t "A is g r eater"
iv. Stop
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Sum = M + N The computer fee for a course is 20,000. Draw a flow chart which
inputs the marks and computes the fee a student has to pay.
Input M
Stop
23. Draw a flow chart to find bigger number among any two different Y
numbers. IS
M > = 90 ? S = (20000 * 40)/10
Ans: Flowchart for finding bigger number
Start
IS Y
Input A, B S = (20000 * 35)/10
M > = 70 AND
M < 90 ?
No IS Yes
A>B?
IS Y
M >= 60 AND S = (20000 * 25)/10
M < 70 ?
Print "B is greater" P rint "A is greater"
N
Fees – 20000 – S
Stop
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Fees = Fees
Stop
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Input A, B , C
B=0
C=1 N IS Y
A>B?
Print C
N N
A= B IS IS
B=C
Y Y
Print "B is greater" P rint "C is greater" P rint "A is greater"
C =A+ B
Y IS Ans: Algorithm
Step 1: Start
Z < = 10 ? Step 2: Read three numbers and store them in A, B and C.
Step 3 If A>B then
N If A>C then
Write “A is the largest”
Stop Else
26. Write and algorithm and draw flowchart to find the largest of
three numbers. [T.U. 2057 Chaitra]
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Stop
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Max
AND A>D)
Tru =A
e
Max=D Max
=C
True
Print Max
Stop
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y 30. Write an algorithm and draw a flowchart to find sum of first 10
y
bu
bu
to
to
natural numbers.
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
k e r - s o f t w a Ans: Algorithm
.
.
ac ac
k e r- s o ft w a
Start
Step 1 : Start
Step 2 : Initialize the variables Count to 1 and Sum to 0..
Step 3 : Write Count A= I
Step 4 : Sum=Sum+Count
Step 5 : Count=Count+1 Ptint A
Step 6 : If Count<=10 then goto step3
Step 7 : Write Sum Sum = Sum + A
Step 8 : Stop
Flowchart A=A+ 2
Start
Yes IS
A=I A <= 100 ?
No
Ptint A
Print Sum
Sum = Sum + A
Stop
A=A+ 1
32. Write an algorithm and draw a flowchart to find factorial of any
Yes IS given number.
A < = 10 ?
Ans: Algorithm
No
Step1: Start
Print Sum
Step2: Write “Enter any number”
Stop
Step3: Read N
31. Write an algorithm and draw a flow chart to print sum of odd Step3: Write Count
numbers up to 100. Step4: Sum=Sum+Count
Ans: Algorithm
Step5: Count=Count+2
Step1: Start
Step6: If Count<=100 then goto step3
Step2: Initialize the variables Count to 1 and Sum to 0..
Step7: Write Sum
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Step8: Stop
Step4: Initialize the value of variables Count=1 and Fact=1.
Step5: Is count less than or equal to N?;then repeat the
steps 5.1 and 5.2
5.1 Fact=Fact*Count
5.2 Count=Count+1
Otherwise;go to step7
Step7: Write Fact
Step8: Stop
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
False Step 2 : First of all, set the value count = 1, which is used to count
Is Count<=N? the number of regions for which temperature conversion is
True done.
Fact=Fact * Count Print Fact Step 3 : Accept region, name & temperature
Coun=Count+1
Step 4 : Compute conversion by using formula
Read N $, E , M , S , C, N
IS Av g > = 4 0 ?
Y
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Print region C
N N IS
Pr int "P as s"
Pr int " Fail" Count = 6 ?
Y
Stop
Sto p
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Sum=0 True
Print J
Read number
lse rem=number
F mod
Is n!=0? 10
a sum=sum+number
/10
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
lowchart to Jread a 5-digit number and check whether the number is
palindrome or = not.
True J [T.U. 2065 Shrawan]
Print sum Ans: Step 1: Start -
1 the variable i.e. n, r, sum & a.
Step 2: Declear
3 Step 3: Read a five digit number ,n
7 Step 4: Assign the value of n to a and zero to sum
Stop
. Step 5: Repeat the step 5.a, 5.b and 5.6 unti the number is not to 0.
5.a r = n% 10
W 5.b sum = sum * 10 + r
r 5.c n = n/10
i
t
e
a
n
a
l
g
o
r
i
t
h
m
a
n
d
d
r
a
w
f
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
True
Display D
False Is False Display number is
n = 0? not palindrome Stop
True points (x1, y1) and (x2, y2), governed by formula D2 = (x2 – x1)2 +
(y2 – y1)2, where x1, y1, y2 are given by user, but should not be zero.
Display number is
palindrome [T.U. 2067 Ashadh]
Ans: Algorithm
Step 1: Start
Step 2: Declare variable x1, x2, y1, y2, D
Stop Step 3: Read Co-ordinates of points x1, y1 & x2, y2
38. Write an algorithm and flow chart of the distance between two
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Display roots are real & unequal
Step 6: Display value of x1 & x2,
Step 7: check if d is equal to zero, perform the following operations.
–b
7.a: calculate, x1 = x2 =
2a
Display roots are real & equal
Step8: Display value of x1 & x2, go to step 10
Step 9: calculate, d = – d
–b d real =
& imag =
2a 2a
Display roots are imaginary
Display x1 = real + imag i
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
1. What is structured programming? Explain its advantages.
Ans: It is the collection of principles and practices that are directed toward
developing correct programs which are easy to maintain and
understand. A structured program is characterized by clarity and
simplicity in its logical flow.
The Three important constructs upon which structured
programming is built are:
• Sequence-Steps are performed one after the other.
• Selection-Logic is used to determine the execution of a
sequence of steps. For example,if-then-else construct.
• Iteration-A set of actions are repeated until a certain condition
is met. For example while construct.
The various advantages of structured programming are:
1. Complexity of program reduces.
2. Easy maintenance.
3. Simplified understanding.
4. Reusability of code increases.
5. Testing and debugging become easier.
2. What is a ‘C’ language? Why we use C? Explain.
Ans: It is a structured programming language which is used to develop
operating system, business system, word processing, database
management system etc. It was developed by Dennis Ritchie at the
Bell Telephone Laboratories in 1972, USA. Every function of C is
accompanied by its own set of library functions and every library
function is accompanied by their separate header files. Unlike some
languages that ignore case, C is case-sensitive. This means that a
variable named a is different from a variable named A.
In today's world of computer programming, there are many high-level
languages to choose from, such as C, Pascal, BASIC, and Java. These
are all excellent languages suited for most programming tasks. Even
so, there are several reasons why many computer professionals feel
that C is at the top of the list:
a/ C is a powerful and flexible language. What you can
accomplish with C is limited only by your imagination. The
language itself places no constraints on you. C is used for
projects as diverse as operating systems, word processors,
graphics, spreadsheets, and even compilers for other languages.
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
should be made. 5. How can you define character set in C language? Explain.
Ans: C uses the uppercase letters A to Z, the lowercase letters a to z, the
digit 0 to 9, and some special characters to form basic program
elements (e.g., constants variables, operators, expression) as we
discussed above. The special characters are listed below:
! * + \ ” <
# ) = ; } >
^ ( ] , { ?
& - [ : ‘ /
% _ = = ~ . (blank)<>
C also uses some combinations of these characters such as \t, \n
to represent special conditions such as horizontal tab and new line
respectively. These character combinations are called escape
sequences. Each escape sequence represents a single character,
although it is a combination of two or more characters.
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
can consist of any combination of digits taken from the (c) Logical operators
set 0 through 9, proceeded by an optional - or + sign. (d) Assignment operators
Various examples of decimal integer constants are: (e) Increments and Decrement operators
123, – 345, 0, 5324, +62 etc (f) Conditional operators
ii. Octal integer constant: An octal integer constant can (g) Bitwise operators
consist of any combination of digits taken from the set 0
through 7, with a leading 0. Some examples of octal
integers are:
0, 01, 0456, 05555 etc
iii. Hexadecimal integer constant: A hexadecimal integer
constant must begin with either 0x or 0x. It can then be
followed by any combination of digits taken from the
sets 0 through 9 and A through F (either upper or
lowercase). Note that the letters A through F represent
the numbers 10 through 15 respectively. Some examples
of hexadecimal integers are:
0x2, 0xgf, 0xbc5, 0x etc
d. Floating point constant: A floating point constant is a base-
10 number that contains either a decimal point or an exponent
(or both).
Some of the valid floating point constants are
0. 1. 0.3. 1.555E+8 .121265e18 etc
If an exponent is present, its effect is to shift the location of the
decimal point to the right if the exponent is positive or to the left if the
exponent is negative. Floating point constants have a much greater
range than integer constants. Typically the magnitude of a floating
point constant might range from a minimum value of app. 3.4E-38 to a
maximum of 3.4E+38. These constants are normally represented as
double precision quantities in C. Each floating point constant will
typically occupy 2 words (8 bytes) of memory. Unlike integer
constants floating point constants are approximations.
9. What is an operator? Explain in brief about the different types of
operators available in C languages. [T.U. 2061 Baisakh, Poush]
Ans: An operator is a symbol which helps the user to command the
computer to do a certain mathematical or logical manipulations.
Operators are used in C language program to operate on data and
variables. C has a rich set of operators which can be classified as
(a) Arithmetic operators
(b) Relational operators
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
OR || exp1 || exp2 NOT ! !exp1
The statements f. Ternary operator
++x;
--y; The ternary operators are the new important feature of C
are the equivalent of these statements: language which is used to check certain relational expression and
x = x + 1; execute the true statement if the condition is true and display the false
y = y - 1; statement if the condition is false.
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
logical conditions that are either true or false.e.g.
printf(“Size of a=%d\n”,sizeof(a)); x+y;
printf(“Size of b=%d\n”,sizeof(b)); y=z;
} x=y+z;
Result: The first expression involves use of addition operator(+), the
Size of ch=1 second expression involve use of assignment operator(=) and third
Size of ch=2 expression involves use of both addition operator & assignment
Size of ch=4 operator.
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
'\?' Question Mark
'\\' Back Slash
'\0' Null
14. Explain the following terms:
(a) Symbolic constants
(b) Library functions
(c) Header files
Ans: (a) Symbolic constants
A symbolic constant allows a name to appear in place of
numeric constant, character constant or a string. symbolic constants
are usually defined at the beginning of the program. Symbolic
constants may appear later in the program in place of numeric
constants, character constant and so on. At the time of compiling each
occurrence of a symbolic constant get replaced by its corresponding
character sequence. Examples :
# define AND &&
# define Pi 3.1412
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
functions are also called built in function as they are built in functions like toupper(),tolower() etc.
with the • The use of angle brackets <> informs the compiler to search
C compiler, and the user mustn’t bother to code them. the compiler’s include directories for the specified file. The use
(c) Header files of the double quotes "" around the filename informs the
• Header files contain definitions of functions and compiler to start the search in the current directory for the
variables which can be incorporated into any C specified file.
program by using the pre-processor #include Some commonly used header files are :
statement.Standard header files are provided with each stdio.h, conio.h, process.h, math.h, ctype.h, string.h, stdlib.h etc.
compiler, and cover a range of areas:string handling, 15. List any six commonly found programming errors in a C program.
mathematics, data conversion, printing and reading of Ans: Commonly found six errors are:
variables, etc. 1. Missing or misplaced; or , missing return type for a procedure,
• To use any of the standard functions, the appropriate missing or duplicate variable declaration.
header file should be included.This is done at the 2. Type mismatch between actual and formal parameters, type
beginning of the C source file. For example, to use the mismatch on assignment.
function printf() in a program, the line #include
<stdio.h> should be at the beginning of the source
file, because the declaration for printf() is found in the
file stdio.h. All header files have the extension .h
stdio.h : It contains the standard input output
functions like
printf(), scanf(), gets(),
puts() etc.
conio.h: It contains some of the functions like getch(),
clrscr()
e
t
c
.
math.h : It contains the mathematical functions like
pow(), sqrt(),
abs()
,fmod()
etc.
String.h: It contains the string manipulating functions
like strlen(),
strcpy() ,strcmp( ),
strrev() etc.
ctype.h : It contains some validating function like
isdigit(),isalnum(),isalpha() and some vale converting
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
C Programming Language …79 (integer, float, character etc.) and need to store the values being used
in the program. C language is rich of data types. A C programmer has
3. Forgetting the precedence of operators, declaration of function
to employ proper data type as per his requirement.
parameters.
4. Output errors means the program runs but produces an Data types are used to declare the variable name in C language.
incorrect result. This indicates an error in the meaning of the C supports different types of data, each of which may be represented
program. differently within the computer’s memory.
5. Exceptions that include division by zero, null pointer and out
of memory.
6. Non-termination means the program does not terminate as
expected, but continues running "forever."
16. What is user-defined data type name? Explain with examples.
Ans: C language allows user to define their own data types which are based
on existing data types.The general format of declaring data type is
typedef existing_data_type user_defined_data_type;
where typedef is keyword.
existing_data_type is C data type.
user_defined_data_type is new data type name which user want
to define.
Example:
#include <stdio.h>
#include <conio.h>
void main ( )
{
typedef int integer;
integer a,b,c;
printf(“Enter value of a and b:”);
scanf(“5d%d”,&a,&b);
c=a+b;
pritf(“Sum=%d”,c);
getch ( );
}
17. What are different data types used in C ? Explain.
[Pokhara University 2010, 2011]
Or, What are data types available in C? Write about their size in
memory, their range and the digit of precision for each data type.
[T.U. 2062 Baisakh]
Or, What are data types available in C? Write about their size in
memory, their range and the digit of precision for each data type.
[T.U. 2062 Baisakh]
Ans: A program usually contains different types of data types
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
80 … A Complete Refresh Manual of C Programming with longer precision. To extend the precision further we can use long
double which consumes 80 bits of memory space.
C language data types can be broadly classified as
A. Primary data type Void Type
B. Derived data type Using void data type, we can specify the type of a function. It
C. User-defined data type is a good practice to avoid functions that does not return any values to
A. Primary data type the calling function.
All C Compilers accept the following fundamental data types
Data type Data sub_ type Bytes Format Range
Character Signed character 1 %c – 128 to 127
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
declaring a constant as from the keyboard and store in the given variable.
unsigned? It reads a whole line of input into a string until a newline or
Ans: An integer constant is any number in the range -32768 to EOF is encountered. It is critical to ensure that the string is large
+32767, because an integer constant always occupies two bytes enough to hold any expected input lines.
in memory and When all input is finished, NULL as defined in stdio.h is
in two bytes we cannot store a number bigger than +32767 or returned.
smaller Syntax:
than -32768. Out of the two bytes to store an integer, the gets(variable name);
highest bit is used to store the sign of the integer. This bit is 1 if Example:
the number is negative and 0 if number is positive. Unsigned gets(sentence); gets(message);
integer constant is an integer constant which has the (ii) puts
permissible range from 0 to 65536. Thus significance of The puts function can be used to display a large text in the
declaring a constant as unsigned almost doubles the size of the monitor. The puts function is used only for the character data types
largest possible value. This happens because on declaring a and the strings.
constant as unsigned, the sixteenth bit is free and is not used
to store the sign of the constant.
19. Why is C standard library needed? Explain.
Ans: C would have been a tough programming language in absence
of the
standard library. The standard library in C includes header files
and standard functions in these files. Header files are collection
of macros, types, functions and constants. Any program that
needs those functions has to include these header files. For
example, the standard library functions, included in “stdlib.h”
header file, are used for number conversions, storage
allocation and other functions on the same line. These
functions are called utility functions. C does not have any built-
in input/output statements as part of its syntax. All I/O
operations are carried out through function calls such as printf
and scanf. There are many functions that have become standard
for I/O operations in C, collectively known as standard I/O
library “stdio.h”. Because of this standard input-output header
file, standard I/O functions can be used on many machines
without any change.
20. What are the functions of string input /output ?Explain.
Ans: The string input/output functions are:
(i)
gets
This function is used to read a string of words(text or
sentence)
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
return 0;
}
Notes:
• Every program statements must end with a semi colon (;).
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
stdio.h, conio.h, string.h etc. program. Under normal circumstances, program execution starts at the
) Header file first statement in main() and terminates at the last statement in main().
The file that are included by the #include directive such as
stdio.h is called header file. The header files are always placed at the
start of a C program.
) Angular <> braces
In C, the angular bracket asks the C preprocessor to look for a
header file in directory other than the current one. If we want to let the
C preprocessor look into current directory first for header file before it
starts to work, we can use double quotes to surround the name of the
header file. Normally, the header file are saved in a sub directory
called include.
) Main function
Every C program must have one and only one main functions.
The main function can be put anywhere but the execution of a
program starts with main function. The main function starts and end
with curly braces {}
) Printf functions and new line character
The printf function is used to print the character string. The
new line character which is usually sufficient at the end of the
message, tells the system to move the cursor to the beginning of next
line.
) Return statements
All function in C can return value. The main function itself
returns an integer value. So, return 0 indicates that 0 is returned from
main function and the program is terminated normally. We can return
values other than 0 to tell the system that there is an error.
Identifiers and Keywords
An identifiers can be defined as the name of the variable,
function, arrays, structures, constants etc, are created by the
programmer. They are the fundamental requirements of any
programming language.
22. For what purpose main() function and #include directive are used.
Ans: The main() Function
The only main component that is required in every C program
is the main() function.The main() function consists of the name main
followed by a pair of empty parenthesis (()) and a pair of braces ({}).
Within the braces are statements that make up the main body of the
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
si=(p*t*r)/100;
ta=si+p;
printf("\nSimple Interest=%.3f",si);
printf("\nTotal Amount=%.3f",ta);
getch();
}
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
c = (f – 32)/1.8; Rupees=2
Paisa=5
printf ("Result = %.2f", c);
getch ( );
}
Result:
Enter temp in fahereinheit=200
Result=93.33
4. Write a program asking cost of pen in paisa. Convert it into the
nearest rupees and paisa.
Ans: #include <stdio.h>
#include <conio.h>
void main ( )
{
int paisa, rup, paisa1;
printf ("Enter cost of pen in paisa =");
scanf ("%d", & paisa);
rup = paisa/100;
paisa1 = paisa%100;
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
m=km*1000;
c=m*100;
i=c/2.54;
f=i/12;
printf("\nMeter=%d",m);
printf("\nCentemeter=%d",c);
printf("\nInch=%.3f",i);
printf("\nFeet=%.3f",f);
getch();
}
Result:
Enter value of distance in kilometer=200
Meter=200000
Centimeter=20000000
Inch=3610108.250
Feet=300842.344
6. Write a program to enter 4-digit number and find sum of the first
and last digit of the number. [e.g. If n = 4567, then sum = 4 + 7]
Ans: #include <stdio.h>
#include <conio.h>
void main ( )
{
int n, r, sum = 0;
printf ("Enter 4-digit number =");
scanf ("%d", &n);
r = n/10000;
sum = sum + r;
r = n%10;
sum = sum + r;
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a float per; k e r- s o ft w a
{
int num, r, n, sum = 0;
total = eng + nep + math + acc + cscience;
printf ("Enter 4-digit number =");
per = total/5;
scanf ("%d", & num);
printf ("Total = %d\n", total);
r = num % 10;
printf ("Percentage = %.2f", per);
getch ( ); n = num/10;
} sum = sum + r;
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
printf(“\nNew content of x: %d”,x);
} printf(“\nNew content of y: %d”,y);
Result: getch();
Enter basic salary of employee:5000 }
z
=
x
;
x
=
y
;
y
=
z
;
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
15. Write a program to find compound interest. The value principal, Area of a triangle:3.00
time and interest rate is given by user. 17. Write a program to read the value of radius of a sphere. Find
Ans: #include<stdio.h> volume and area of it. Also define value of as constant.
#include<conio.h> Ans: #include<stdio.h>
#include<math.h> #include<conio.h>
void main ( ) float p,t,r,si;
{ printf(“\nEnter the value of principal:”):;
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
scanf(“%f”,&p); #define pi 3.14
void main ( )
{
int r;
float v,a;
printf(“\nEnter the value of time:”); printf(“Enter the value of radius:”);
scanf(“%f”,&t); scanf(“%d”,&r);
printf(“\nEnter the value of rate:”);
scanf(“%f”,&r); v=4/3*pi*r*r*r;
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
printf(“Enter a character value for grade:”);
scanf(“%c”, grade);
if(grade = = ‘A’)
printf(“The grade is excellent \n”);
printf(“Thanks for using this program\n”);
getch():
}
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
printf(“Enter a character value for grade:”);
scanf(“%c”, grade);
if(grade= =’A’)
printf(“grade is excellent \n”);
else if(grade= =’B’)
printf(“grade is very good\n");
else
printf(“Good\n”);
getch();
}
switch…case: It is alternate statement of if …. else /if ….else if …
else statement
It allows us to select a choice out of many available choices.
The switch statement causes a particular group of statements to be
chosen from several available groups. The selection is based upon the
current value of an expression that is included within the switch
statement.
Syntax :
switch(expression) {
case a:
DoSomething();
break;
case b:
DoSomethingElse();
break;
……….
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
[T.U. 2057 Chaitra] unconditionally from one point to another in the program.The goto
Or, Describe how switch statement work. What are keyword is followed by a label, which is basically some identifier
the restrictions on case labels? placed elsewhere in the program where the control is to be transferred.
[T.U. 2058 Chaitra] During running of a program, the statement like “goto label1;”
Ans: Question no.(1), d. cause the flow of control to the statement immediately following the
4. Write the syntax and an example of "goto" label “label1”. We can have a forward jump or a backward jump.
statement. syntax > goto /identifier
Ans: C support goto statement to branch
getch(); Example : A program for finding odd or even numbers by using
} GOTO statement.
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
ample : printf ("%10d", r); // prints 2 leaving 10
spaces. Right justified.
printf ("%–d", r); // prints 2 left justified
printf ("%f",a); // prints 12.56201547
printf ("%10.2f",a); // prints 12.56 leaving 10
spaces
# include <stdio.h>
printf ("%e",a); // prints1.256201c + 01
#include<conio.h>
void main ( ) printf ("%15.2e",a); //prints 1.25e + 01
{ leaving 15 spaces. Right
char ch; justified.
printf ("Enter any letter :-"); }
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
getch ( );
}
Result:
Enter ay number=4
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
getch(); getch ( );
} }
Result:
Result: Enter year= 2000
Enter an integer=12
Leap year.
Divisible by 4 but not by 11. 6. Write a computer program that asks the user to type two integer
values from the terminal. Test these two values to determine if the
4. Write a program to enter any character and check whether the first is evenly divisible by the second and display appropriate
entered character is vowel or consonant. message at the terminal. [T.U.2057 Chaitra]
Ans: # include <stdio.h> Ans: #include<stdio.h>
# include <conio.h> #include<conio.h>
void main ( ) void main()
{
{ int first,second;
char ch; printf("Enter any two numbers:");
printf ("Enter any character ="); = 'E' || ch = = 'O' || ch = = 'I' ||
scanf ("%c", ch); ch = =
'U')
printf ("vowel");
if (ch = = 'a' || ch = = 'e' || ch = = 'i' ||
ch = = '0' || ch = = 'u' || ch = = 'A' || ch = else
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
} scanf("%d%d",&first,&second);
printf ("consonant");
if(first%second==0)
getch ( ); printf ("First is divisible by second
number");
else
printf("Not divisible");
getch();
Result:
Enter any two numbers:20 3
} Not divisible
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
if (a > b)
printf ("A is the greater number.");
else
printf ("B is the greater number");
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
totalsales);
}
else
{
commission = sales * 3/100;
totalsales = sales + commission;
printf ("Totalsales = %.2f",
totalsales);
}
getch ( );
}
Result:
Enter sales amount =4000
Totalsales=4060.00
10. In a company an employee is paid as under:
(i) HRA=10% of Basic salary and DA=55% of Basic salary.
ii) If his Basic salary is greater than 1500 then
HRA=500/- and DA=60% of Basic Salary.
Write a C program to find Gross salary of the employee.
Ans: #include<studio.h>
#include<conio.h>
void main()
{
float basic,hra,da,gross;
clrscr();
printf("\n Enter the basic salary of the
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
}
13. Citizen are eligible to cast vote if they are 18 years or above and
Result:
Enter value of CP and SP:5000 3000 must be nepali. Write a program for this purpose.
Ans: #include<stdio.h>
Gain=2000 #include<conio.h>
void main ( )
{
int age;
char nep;
printf ("Enter the age = ");
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
getch ( );
}
Result:
Enter the age: 19
14. Write a program to enter a character and check whether the entered
character is digit or not.
Ans: #include<stdio.h>
#include<conio.h>
void main ( )
{
char ch;
printf ("Enter any character =");
scanf("%c ", &ch);
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
lowercase character to uppercase and vice-versa. printf(“Imaginary roots can not be
Ans: #include<stdio.h> obtained.”);
#include<conio.h>
void main ( ) getch();
{ }
char ch;
printf ("Enter any character =");
scanf("%c",ch);
getch ( );
}
Result:
Enter any character =y
d=sqrt(b*b-4*a*c);
if(d>0)
{
x=(-b+sqrt(d))/2*a;
y=(-b-sqrt(d))/2*a;
printf(“\nX=%.2f”,x);
printf(“\nY=%.2f”,y);
}
else
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
> "); printf("\nGreatest is b :%d",b);
ch=getch(); else
} printf("\nGreatest is c :%d",c);
getch();
}
printf ("\nThanks for using it!"); Result:
getch(); Enter 3 numbers=2 3 8
}
Greatest number is c:8
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
#include<conio.h>
if(m==1 || m==3 || m==5 || m==7 || m==8 || m==10 void main()
|| m==12) {
printf("The month has 31 days"); float length, width, height;
else if(m==2) clrscr();
printf("The month has 28 or 29 days");
else if(m==4 || m==6 || m==9 || m==11) printf("enter the dimension of box\n");
printf("The month has 30 days"); printf("Length= ");
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Length=2
width= 2
height=3
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
per = total/5;
Name = Haris
Class=13
Total =254
Percentage = 50.80
Remarks=Passed
Division=Second
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
switch (ch)
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
28. Write a menu driven program which has the following options:
1. Perimeter of circle
2. Perimeter of rectangle
3. Perimeter of triangle
Ans: #include "conio.h"
#include "stdio.h"
int main()
{
int choice;
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
{
int a,b,c,per;
printf("\nEnter the sides of the triangle\n");
printf("\nEnter the first side:\t"); scanf("%d",&a);
printf("\nEnter the second side:\t");
scanf("%d",&b);
printf("\nEnter the third side:\t");
scanf("%d",&c);
per=a+b+c;
printf("\nThe perimeter of triangle is:%d",per);
break;
}
default:
{
printf("Wrong choice");
}
}
getch();
}
Result:
Enter choice 1 for perimeter of circle Enter
choice 2 for perimeter of rectangle Enter
choice 3 for perimeter of triangle Enter your
choice:1
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
until the condition is false.The syntax of ‘for’ loop is
Syntax>for (exp1; exp2; exp3)
{
statement1;
..................
}
When a for statement is encountered during program execution,
the following events occur:
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
#include<stdio.h> Result:
#include<conio.h> 2 4 5 7 8 10
void main()
{
int a =1;
while (a < 20)
{
a = a + 1;
if (a%3==0)
break;
printf ("%d",a);
}
getch();
}
Result:
2
Continue :
Continue is another keyword that controls the flow of loops. If we
are executing a loop and hit a continue statement, the loop will stop its
current iteration, update itself (in the case of for loops) and begin to
execute again from the top. Essentially, the continue statement is
saying "this iteration of the loop is done; let's continue with the loop
without executing whatever code comes after me." Example :
#include<stdio.h>
#include<conio.h>
void main()
{
int a =1;
while (a < 10)
{
a = a + 1;
if (a%3==0)
continue;
printf ("%d",a);
}
getch();
}
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Inc/Dec True
Condition ? Statements True
True Condition? Statements
Condition ? Statements
False
False False
printf("%d\t",
(d) Example:Displaying 1 to 10 numbers a);
(d) Example: Displaying 1 to
# include <stdio.h> 10 numbers
getch( );
# include <conio.h> # include <stdio.h>
}
void main ( ) # include <conio.h>
{ void main ( )
int a; {
for (a = 1; a <= 10; ++a) int a;
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
a = 1; D of the form:
while (a <= 10) do
o
{ { ...block of statements to execute...
printf("%d\t", a); } while(conditional expression);
a = a + 1; w
While construct checks the conditional expression before the
} h
loop is executed. Sometimes it is necessary to execute the body of the
getch( ); i
loop before the conditional expression is evaluated. Such situations
} l
are handled by do-while loop construct. On reaching the do statement,
e
the body of the loop is evaluated and at the end of the loop, the
conditional expression is checked for true or false. If true,it continues
s
to evaluate the body again and when condition become false, the
t
control is transferred to the statement immediately after the while
a
statement.
t
e
m
e
n
t
T
h
i
s
l
o
o
p
c
o
n
s
t
r
u
c
t
i
s
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
void main()
{
int n1,n2,i;
printf(“Enter the value of n1 and n2:”);
scanf(“%d%d”,&n1,&n2);
for(i=n1;i<=n2;++i)
{
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
printf(“Sum=%d\n”,sum);
printf(“Average=%.2f”,avg);
6. Write a program to compute factorial for a given number n, where
n is a non-negative integer,[n!=1*2*3*4......(n-1)*n].
getch();
}
Ans: #include<stdio.h>
#include<conio.h>
void main()
Result: {
How many number?5 Enter numbers:40
Enter numbers:5
Enter numbers:10
Enter numbers:20 Sum=105
Enter numbers:30 Average=21.00
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
i scanf("%d",& num);
n
t if(num<0)
printf("Error.Plz enter positive no only.");
n else if(num==0)
= printf("The factorial of the given no
1 is:%d",fact);
, else
f {
a while(n<=num)
c {
t fact=fact*n;
=
1
,
n
u
m
;
p
r
i
n
t
f
(
"
E
n
t
e
r
n
u
m
b
e
r
:
"
)
;
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
for(i=1;i<=n;++i)
7. Write a program to print the series 1,5,9,13........upto 10th terms. {
Ans: #include<stdio.h> printf("Enter value of radious of circle=");
#include<conio.h> scanf("%d",&r);
void main() a=3.1416*r*r;
{ printf("Area=%.2f\n",a);
int cnt=1,i; }
printf("%d\t",cnt);
getch();
for(i=0;i<10;++i) }
{ Result:
cnt=cnt+4; Enter number of circle=3
printf("%d\t",cnt);
} Enter the valu of radiuys of circle=2
getch(); Area=12.56
} Enter the valu of radiuys of circle=3
Area=28.27
Result: Enter the valu of radiuys of circle=4
1 59 13 17 21 25 27 Area=50.26
33 37 41
8. Write a program to display following series:30,25,20,………up to
10th terms. 10. Write a C program to read any number and check whether the
Ans: #include<stdio.h> entered number is palindrome or not.
#include<conio.h> Ans: #include<stdio.h>
void main() #include<conio.h>
void main( )
{
{
int a,i=30;
i=i-5;
for(a=1;a<=10;++a)
}
{
getch();
printf(“%d”,i);
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
} int n,num,rev=0,r;
Result: printf(“Enter any number:”);
scanf(“%d”,&num);
30 25 20 15 10 5 0 -5 -10 -15
n=num;
while(num!=0)
{
r=num%10;
rev=rev*10+r;
num=num/10;
}
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
if(sum==i)
printf("The entered number is armonstrong”);
else
printf("Not armonstrong");
getch();
}
Result:
Enter any nuber=371
12. Write a program to print the fibonacci series until the term is less
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Result: }
Enter how many numbers to be entered:7
Result:
1 1 2 3 5 8 13 1 25 10 17 26 37 50
13. Write a program to print the following series until the term value is 65 82 101 122 145 170 197 266
less than 750.The series is 1,2,5,10,17,26,37…… ………………………………………… 559505 561002
[T.U. 2063 Baishakh]
Ans: #include<stdio.h>
#include<conio.h> 14. Write a C program to find the sum of the following series and input
void main() steps from keyboard. Sum=1 + 2 + 6 + 24 + 120 + ......
{ Ans: #include<stdio.h>
int a=0,n=1;
while(n<=750) #include<conio.h>
{ void main()
printf("%d\t",a*a+1); {
++a; long int fact=1,i,n,sum=0;
++n; clrscr();
} printf("\n ENTER THE VALUE OF N: ");
getch();
int a=0,b=1,c,count; scanf("%ld",&n);
printf("\n THE SERIES IS ");
printf("%d\t%d\t",a,b);
for(count=2;count<500;count++) for(i=1;i<=n;i++)
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
}
else
if (n > 20)
{
goto x;
sum-=start;
printf ("-%d",start); else
} {
start+=2; for (i = 1; i <=10; i ++)
} {
mul = n * i;
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
if (a==j)
printf ("\n Prime no=%d ", a);
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
int n, r; n = n/10;
printf ("\n Enter any number :-"); }
scanf ("%d", &n);
printf(“The reverse order of digits getch ( );
=”); }
Result:
while (n!=0) Enter any number=123
{
r = n%10; The reverse order of digits =321
printf ("%d", r);
19. Write a program to calculate the value of a raised to b. [e.g. 23 = 8]
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
r = n%10;
C Programming Language …141 sum = sum + r;
21. Write a computer program that reads in an integer value for n then n = n/10;
sums the integers from n to 2n if n is non-negative, or from 2n to n }
if n is negative. Display the sum. [T.U. 2057 Chaitra] printf ("Sum of digit of number =%d",sum);
Ans: #include <stdio.h> getch ( );
#include <conio.h> }
void main ( )
{
int a,n,sum=0;
printf(“Enter value of n:”);
scanf(“%d”,&n);
if(n>0)
{
for(a=n;a<=2*n;++a)
sum=sum+a;
}
else
{
for(a=2*n;a<=n;++a)
sum=sum+a;
}
printf(“Sum=%d”,sum);
getch();
}
Result:
Enter value of n:10
Sum=165
22. Write a program to accept any number and find sum of digit of that
number. [e.g. 275, sum = 2 + 7 + 5 = 14]
Ans: #include <stdio.h>
#include <conio.h>
void main ( )
{
int n, sum = 0, r;
printf ("Enter any number =");
scanf ("%d", &n);
while (n > 0)
{
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
{
142 … A Complete Refresh Manual of C Programming printf ("Enter the number =");
scanf ("%d", &no);
Result: if (no > max)
Enter any number=124 max = no;
}
Sum of digit of number=7
printf ("\n maximum no = %d", max);
23. Write a program to read a number and check whether the entered getch ( );
number is perfect or not.
Ans: #include<stdio.h>
#include<conio.h>
int main()
{
int n,i=1,sum=0;
printf("\nEnter a number:-");
scanf("%d",&n);
while(i<n)
{
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("\nThe no %d is a perfect number",i);
else
printf("\nThe no %d is not a perfect
number",i);
getch();
}
Result:
Enter a number:5
The no is not a perfect number.
24. Write a program to read 'n' different user defined numbers and
picks largest of them.
Ans: #include <stdio.h>
#include <conio.h>
void main ( )
{
int n, max = 0, no, count;
printf ("Enter how many numbers =");
scanf ("%d", &n);
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Result:
Enter decimal number=15
Binary number=1111
26. Write a program that reads 10 integer value and prints the total of
all non negative numbers in the list.
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Sum=128
27. Write a program to enter highest and lowest number and find the
sum of lowest to highest number.
Ans: #include <stdio.h>
#include <conio.h>
void main ( )
{
int high, low, sum = 0, temp, num;
printf ("Enter a high number =");
scanf ("%d", &high);
printf ("Enter a low number =");
scanf ("%d", &low);
if (low > high)
{
temp = low;
low = high;
high = temp;
}
for (num = low; num < = high; num + +)
{
sum = sum + num;
}
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
for (i =1;i<=n;i++)
{
printf ("Enter numbers : \n");
scanf ("%d",&no);
if(no>0)
pcount = pcount + 1;
else
ncount = ncount + 1;
}
Result:
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
scanf("%d",&n);
printf(“The reverse order of digits=”);
while(n>0)
{
r=n%10;
printf("%d",r);
n=n/10;
sum=sum+r;
}
printf("sum=%d",sum);
getch();
}
Result:
Enter any no=123
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
case 2 :
{
printf ("Enter number =");
scanf ("%d", &num);
for (i = 2; i < num; i + +)
{
if (num % i = = 0)
{
printf ("\n Not Prime");
break;
}
}
if (i = = num)
printf ("\n Prime number");
}
break;
case 3 :
{
printf ("Enter number =");
scanf ("%d", &num);
if (num%2 = = 0)
printf ("\n even number");
else
printf ("\n odd number");
}
break;
case 4 :
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
} bin_no;
break; pow8=1;
oct_no=0;
case 5 :
exit (0); while (no>0)
{
default : i=0;
printf ("You are not inter_oct=0;
allowed to type pow2=1;
other”); while (i<=2)
exit (0); {
} if (no==0)break;
getch ( ); rem=no%10;
} inter_oct+=rem*pow2;
Result: i++;
1. Factorial number pow2*=2;
2. Prime
3. Odd or even
4. Multiplication Table
5. Exit
Enter your choice (1/2/3/4/5):-1
Enter number=4
Factorial no=24
31. Write a C program to convert a binary
number to its corresponding octal number.
Ans:
#include
<stdio.h>
#incl
ude
<coni
o.h>
void main ( )
{
long int bin_no,no;
int oct_no,oct_p,i,rem,pow2,pow8 ,
inter_oct;
clrscr();
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
34. Write a program to read an integer and add /10;
the individual digits contained in it until the }
final sum is single digit. For example if
entered number is n=2175698 then
2+1+7+5+6+9+8=38
38=3+8=11
11=1+1=2 is the final answer.
[T. U. 2063 Poush] Ans: #include<stdio.h>
#include<conio.h>
void main()
{
int
n,r,sum=0
,r1,s1=0;
printf("E
nter any
number=")
;
scanf("%d
",&n);
while(n>0)
{
r
=
n
%
1
0
;
s
u
m
=
s
u
m
+
r
;
n
=
n
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
if(m<n)
{
temp=m;
m=n;
n=temp;
}
do
{
rem=m%n;
m=n;
hcf=n;
n=rem;
}while(rem!=0);
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
printf("\nEnter two numbers:"); scanf("%s",&ch);
scanf("%d %d",&n1,&n2);
x=n1,y=n2;
while(n1!=n2)
{
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
}
printf("L.C.M=%d",x*y/n1);
getch();
}
Result:
Enter two number=34 2
L.C.M.=34
37. Write a program that reads positive numbers until user enters “no”
and then sum the numbers divisible by 4 that lie between the range
of 10 and 50 and finally display the count and the average value.
[T.U. 2065 Shrawan]
Ans: #include<stdio.h>
#include<conio.h>
void main()
{
char ch='y';
int sum=0,a=10,num,count=0;
float avg;
while(ch=='Y' || ch=='y')
{
if(a<=50)
{
printf("Enter numbers:");
scanf("%d",&num);
if(num%4==0)
{
sum=sum+num;
count=count+1;
}
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Count=2
Average=12.00
38. Write a program to find the terms in the given series till the value of
the term is less than 1000.(12+22)/3,(22+32)/4,(32+42)/5,……..
[T. U. 2059 Chaitra]
Ans: #include<stdio.h>
#include<conio.h>
void main()
{
int a=1;
float term;
do{
term=((a*a)+(a+1)*(a+1))/((float)(a+2));
printf("%.2f\t",term);
++a;
} while(a<250);
getch();
}
Result:
1.67 3.25 5.00 6.83 8.71 10.62 12.56 14.50
16.45 18.42 20.38 22.36 24.33 26.31 28.29 30.28
…………………………………………………………………
…………………………………………………494.02 496.02
39. Write C programs to evaluate the series until the term becomes less
than 10-6.
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
for(i=1;i<n;++i) float */
{ printf("\n How many terms ? ");
term=term*1/pow(x,i);
scanf("%d", &n);
sum=sum+term;
}
printf(“Sum=%.2f”,sum); for ( i = 1; i <= n ; ++i)
getch(); {
} term = i/(i +1); /*cast to float is must else
Result: term = 0*/
Enter value of n and x:5 2 ft += (float) i / ( i + 1);
Sum=0.64 sum += ft;
}
40. Sum of first n terms of a series 1 /2 + 2/3 +3/4 ……….
In this program it is essential to use cast. As shown in the program. printf("\n sum = %.2f", sum); /*Note TERM IS
PRINTED AS 0*/
Ans: #include<stdio.h> printf ("\n term = %.2f", term);
#include<conio.h>
getch();
void main()
}
{
int n, i; Result:
float sum = 0, term = 0, ft ; /* ft is term in How many terms?5
int i,j,n; Sum=9.85
float sum=1,term=1; Term=0.00
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
42. Write a program to find sum of series given below for given n terms {
long int x,i,j,k,n,sq,cnt;
and x..
double fact,sum=0;
x-x3/3!+x5/5!-x7/7!+…….. [T.U. 2062 Poush] clrscr();
Ans: #include<stdio.h>
#include<conio.h> printf("\n ENTER THE VALUE OF N: ");
void main()
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
scanf("%ld",&n);
printf("\n ENTER THE VALUE OF X: "); 43. Code for program to print the sum of series 1 + 1/2 + 1/3 + 1/4 + ... +
1/n. in c programming
Ans: #include<stdio.h>
#include<conio.h>
void main()
{
double n,sum=0,i;
clrscr();
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
and so on till n/n. }
Ans: #include<stdio.h>
#include<conio.h> printf("Sum = %.2f\n",sum);
void main() getch();
}
{
float sum,n,term; Result:
Enter value of n:10
int count = 1;
sum = 0; Sum=1.00
46. Write a C program to convert a binary number to its corresponding
printf("Enter value of n\n"); octal number.
scanf("%f",&n); Ans: #include<stdio.h>
term = 1.0/n; #include<conio.h>
void main()
while(count<=n) {
{
main()
sum = sum + term;
{
count++;
long int bin_no,no;
} int oct_no,oct_p,i,rem,pow2,pow8,inter_oct;
} clrscr();
printf("\n\n THE SUM IS %ld",sum); printf ("\nEnter the Binary number: -> ");
getch(); scanf("%ld",&bin_no);
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
{
fact*=j;
j++;
}
printf("\tX^%d/%ld\t+",i,fact);
i*=2;
}
getch();
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
1x5=5 stdio.h>
2x5=10……………… #include<conio.h>
void main()
………………...10x5=5
{
0 int i,j,f;
1x6=6 for(i=1;i<=10;++i)
2x6=12……………… {
………………...10x6=6 f=1;
0 for(j=i;j>0;--j)
1x7=7 {
2x7=14……………… f=f*j;
}
………………...10x7=7
printf(“%d=%d\n”,i,f);
0 }
1x8=8 getch();
2x8=16……………… }
………………...10x8=8
0
1x9=9
2x9=18………………
………………...10x9=9
0
1x10=10
2x10=20………………
…………….10x10=100
2. Write a program to
display factorial
number of first 10
natural number.
A
n
s
:
#
i
n
c
l
u
d
e
<
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
for(j=1;j<=i;++j)
4. Write a C program to generate a series of Armstrong numbers. A {
number is an Armstrong number if the sum of the cube of its digit is printf("%d",i);
equal to the number. }
Ans: #include<stdio.h> printf("\n");
#include<conio.h> }
#include<math.h> getch();
void main() }
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
void main()
C Programming Language …167 {
9. Write a C Program to display the following: int i,j;
12345678 for(i=0;i<=4;++i)
{
2345678
for(j=1;j<=4;++j)
345678 {
45678 printf("%d\t",(i+j)*10);
5678 }
678
78
8
Ans: #include<stdio.h>
#include<conio.h>
void main()
{
int b,c;
for(b = 1; b <= 8; b++)
{
for(c = b; c <= 8; c++)
{
printf("%d", c);
}
putchar('\n');
}
getch();
}
Result:
12345678
2345678
345678
45678
5678
678
78
8
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
printf ("\n\t\t");
}
getch ( );
}
Result:
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
R
e 1
s 2 3
u 4 5 6
l 7 8 9 10
t 11 12 13 14 15
:
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
5 12345
4 1234
3 123
2 12
1 1
!
W
W
O
O
N
N
y
y
bu
bu
to
to
* * * * *
ww
ww
om
om
1
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
* * * *
.
.
ac
k e r- s o ft w a 3 5 ac
k e r- s o ft w a
7 29
Ans: #include<stdio.h> 19. Write a program to display the following series
#include<conio.h> 1
void main() 2 3
{ 3 4 5
int i,j;
for(i=5;i>=0;--i) 4 5 6 7
{ 5 6 7 8 9
for(j=1;i>=j;++j) Ans: #include<stdio.h>
{ #include<conio.h>
printf("*\t"); void main()
} {
printf("\n"); int i,j,sum=0;
} for(i=1;i<=5;++i)
getch(); {
} for(j=0;j<i;++j)
Result: { sum=i+j;
printf("%d\t",sum);
* * * * *
* * * * }
printf("\n");
* * *
}
* *
3 5* getch();
2 3
7 9 11 } 3 4 5
18. Write a program to display the following series
13 15 17 19 Result: 4 5 6 7
1
21 23 25 27 29 15 6 7 8 9
Ans: #include<stdio.h>
#include<conio.h>
void main()
{
int i,j=1,k;
for(i=1;i<=5;++i)
{
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
for(k=1;k<=i;j++,k++)
printf("%d\t",j);
printf("\n");
}
getch();
}
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
The table below shows the values that are stored in the particular
numbers.
Num[0]
Num[1]
Num[2]
Num[3]
Num[4]
57
20
56
17
23
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
int a[10][10];
int i, j row,col;
printf(“\n Input row and column of a
matrix:”);
scanf(“%d %d”,
&row,&col); for(i=0;
i<row;i++)
for(j=0;j<col;j++)
scanf(“%d”,
&a[i][j]);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf(“%5d”,
a[i][j]);
printf(“\n”);
}
}
2. What are the concept remembered about arrays? Explain.
Ans: Followings are some of the concept to be remembered about
arrays:
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
(f) While declaring the 2D array, the number of columns
should be specified and its a mandatory. where as for
number of rows
there is no such
rule.
(g) An Array index by default starts from 0 and ends with
the index no. (n-1).
(h) The size of an Array can not be changed at Run Time
6. What is a string? Explain, how string is declared?
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Number of characters in the string is=8
(b) Strcpy ( )
C does not allow you to assign the
characters to a string directly as in the statement
name=”Robert”; Instead use the strcpy(0 function
found in most compilers the syntax of the
function is illustrated below.
strcpy(string1,string2);
Strcpy function assigns the contents of
string2 to string1. string2 may be a character
array variable or a string constant.
strcpy(Name,”Robert”);
In the above example Robert is
assigned to the string called name.
Let us consider an example:
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
the second and a positive number if the string is greater than the
second. If the two strings are identical,strcmp() returns a value zero. If
they are not, it returns the numeric difference between the ASCII
values of the non-matching character.
Let us consider an example:
#include <stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[ ]=“National”;
char name1[ ]= “Open”;
if(strcmp(name,name1)==0)
printf(“Both are equal.”);
else
printf(“Not equal.”);
getch();
} Result:
Not equal.
This function takes two strings, S1 and S2 and compares them.
It returns
• A negative value if name is less than name1.
• 0 if name and name1 are equal .
• A positive value if name is greater than name1.
(e) Strrev()
This function reverses the characters in a string.
Syntax:
strrev(string);
For ex strrev(“program”) reverses the characters in a string into
“margrop”.
Let us consider an example:
#include <stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[ ]=“National”;
printf(“Reverse string=%s.”,name);
getch();
}
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
characters that the compiler would copy from the source string. int grades[3][4];
Here The first bracket ([3]) tells the compiler that we are declaring 3
is the syntax: pointers, each pointing to an array. We are not talking about a pointer
char* strncpy(char* Destination, const char* Source, int Number);
variable or pointer array. Instead, we are saying that each element of
The Number argument specifies the number of
characters that will be copied from the Source string. Here is an the first dimension of a two dimensional array reference a
example: corresponding second dimension. In this example, all the arrays
#include <stdio.h> pointed to by the first index are of the same size. The second index
#include<conio.h>
#include<string.h>
void main()
{
char CarName1[] = "Ford Escort";
char CarName2[] = "Toyota 4-Runner";
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
getch();
C Programming Language …189 }
2.9 C Programmings -5 Result:
Enter age of 100 persons:
34 54 12 90 …………………………….23 45
1. Write a program to display the following numbers.
3,56,76,5,90 34 54 12 90 ……………………………23 35
Ans: #include <stdio.h>
#include <conio.h>
void main ( )
{
int i,num[5]={ 3,56,76,5,90};
printf(“List of number\n”);
for(i=0;i<=4;++i)
printf(“%d\t”,num[i]);
getch();
}
Result:
List of number
3 56 76 5 90
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Enter a number:90
190 … A Complete Refresh Manual of C Programming Enter a number:87
3. Write a C program to read 10 numbers and print them in reverse Enter a number:76
order. Enter a number:65
Ans: #include<stdio.h> The number in reverse ordr are:
65 76 87 90 69 67 65 56
#include<conio.h>
34 43
void main()
{ 4. Write a C program to calculate the average of a set of N numbers.
int num[10],i; Ans: #include<stdio.h>
for(i=0;i<10;i++) #include<conio.h>
{ void main()
printf("Enter a number:"); {
scanf("%d",&num[i]); int i,a[1000],n,sum;
} float average;
printf("The number in reverse order are:\n"); clrscr();
for(i=9;i>=0;i--) printf("How many elements you want to enter");
printf("%d\t",num[i]); scanf("%d",&n);
getch(); printf("enter values: \n");
}
Result: for(i=0;i<n;i++)
Enter a number:43 scanf("%d",&a[i]);
Enter a number:34
Enter a number:56 sum=0;
Enter a number:65 for(i=0;i<n;i++)
Enter a number:67 {
Enter a number:69
sum=sum+a[i];
}
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
5. Write a 'C' program to read salaries of 200 employees and count the
number of employees getting salary between 5000-10000.
Ans: #include <stdio.h>
#include <conio.h>
void main ( )
{
int count = 0, salary [200], i;
// Reading salary of 200 employees
for (i = 0; i < 200; i + +)
{
printf ("Enter salary :");
scanf ("%d", & salary [i]);
}
/* counting number of employees getting salary
between 5000 - 10000 */
for (i = 0; i < 200; i++)
{
if (salary [i] > 5000 && salary [i] <
10000)count++;
}
printf ("Total number of employees getting salaries
between 5000-10000 = %d", count);
getch ( );
}
Result:
Enter salary:5600
Enter salary:9000
Enter salary :4500
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
following rules : Enter mark of a subject:90
Percentage greater or equal to 60 First division Name=Yogesh
Percentage between 45 and 59 Second division Total=477.000000
Percentage=68.142860
Percentage between 35 and 44 Third division
Percentage less than 35 Fail
Write a program using C language to process result of all students
based on specification stated above.
Ans: #include <stdio.h>
#include <conio.h>
void main ( )
{
char name [20];
int i;
float x [7], per, total = 0;
printf ("Enter name =");
scanf ("%s", name);
for (i = 0; i < 7; i + +)
{
printf ("Enter mark of a subject =");
scanf ("%f", &x [i]);
total = total + x [i];
}
printf ("\n\n");
per = total/7;
printf ("Name = %s\n", name);
printf ("Total = % f\n", total);
printf ("Percentage = %f\n", per);
if (per>60)
printf ("First division\n");
else if (per > = 45)
printf ("Second division\n");
else if (per > = 35)
printf ("Third division");
else
printf ("Fail\n");
getch ( );
}
Result:
Enter name:Yogesh
Enter mark of a subject:34
Enter mark of a subject:56
Enter mark of a subject:65
Enter mark of a subject:67
Enter mark of a subject:76
Enter mark of a subject:89
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
for(i=0;i<n;++i)
{
printf("Enter number:");
scanf("%d",&a[i]);
if(a[i]%7==0 &&a[i]%5!=0)
{
sum=sum+a[i];
count=count+1;
}
}
printf("\nSum of numbers=%d",sum);
printf("Total nos stored=%d",count);
getch();
}
Result:
How many number?5
Enter number:21
Enter number:1
Enter number:7
Enter number:20
Enter number:35
Sum of numbers=28
Total nos stored=2
8. Write a program to read an array of 20 elements and find sum of
squares of odd numbers contained in the array. Also, find the
numbers of elements stored.
Ans: #include<stdio.h>
#include<conio.h>
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
count=count+1; count + +;continue;
sum=sum+a[i]*a[i]; }
} printf ("Total number of persons in the age
}
group between 50 and 60 are : %d", count);
printf("\nSum of squares of odd
nos=%d",sum); getch ( );
printf("Total nos stored=%d",count);
getch();
}
Result:
Enter nos:20
Enter nos:3
Enter nos:5
…………………………….
…………………………..
Enter nos:35
Enter nos:70
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
while(num!=0)
{
result=result*10;
a[i]=num%10;
num=num/10;
result=result+a[i];
++i;
}
if(result==n)
printf(“The number is palindrome.”);
else
printf(“Not”);
getch();
}
Result:
Enter the number:121
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
max=a[0];
min=a[0];
for (i = 0; i <n; i + +)
{
sum=sum+a[i];
if (a [i] > max)
max = a[i];
else if( a[i]<min)
min = a[i];
else
;
}
svg=sum/n;
printf ("\nThe greatest number = %d",max);
printf ("\nThe smallest number = %d",min);
printf(“\n Sum of numbers=%d”,sum);
printf(“\n Average=%.2f”,avg);
getch ( );
}
Result:
How many nos?5
Enter numbers:500
Enter numbers:5
Enter numbers:20
Enter numbers:901
Enter numbers:51
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
#include<conio.h> for(i=2;i<=n;++i)
void main() {
{ a[i]=a[i-2]+a[i-1];
int a[100],i,n; printf("Enter } for(i=0;i<=n;++i)
number:"); scanf("%d",&n); printf("%d\t",a[i]);
a[0]=0; getch();
a[1]=1; }
// sorting in ascending order Result:
for (i = 0; i < n; i + +) Enter number:5
{
for (j = i+1; j < n; j + +) 0 1 1 2 3 5
{
if (a [i] > a [j])
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
len++;
printf("New array\n");
for(a=0;a<len;++a)
{
printf("%d\t",n[a]);
}
}
Result:
Enter data to be read:5
Enter integer:10
11
12
13
14
Enter integer to be inserted:5
Enter position in the array for
insertion:3
New array
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
13
C Programming Language …201 14
10 11 12 5 13 14 Enter position to delte element:2
17. Write a C program to delete an element in an array. New array
Ans: #include<stdio.h> 10
11
#include<conio.h> 13
void del(int num[],int,int); 14
int n,a;
void main()
{
int num[100],pos;
printf("How many elements in the array?:");
scanf("%d",&n);
printf("Enter integers:");
for(a=0;a<=n-1;++a)
{
scanf("%d",&num[a]);
}
printf("Enter position to delte element:");
scanf("%d",&pos);
del(num,pos,n);
getch();
}
void del(int num[],int pos,int n)
{
int j,item;
item=num[pos];
for(j=pos;j<=n-1;++j)
{
num[j]=num[j+1];
}
n=n-1;
printf("New array.\n");
for(a=0;a<=n-1;++a)
printf("%d\n",num[a]);
}
Result:
How many elements in the array?:5
Enter integer:10
11
12
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
202 … A Complete Refresh Manual of C Programming students and prints them in tabular format.
Ans: #include<stdio.h>
#include<conio.h>
18. Write a program to search a number from the list of N numbers. void main()
You should read the number to be searched from the user. {
[T.U. 2066 Jestha] char name[10][10];
Ans: #include<stdio.h> int mark[10],i;
#include<conio.h>
void main() for(i=0;i<10;i++)
{
int a[100],i,n,x,flag=0;
printf("How many numbers:");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf(“Enter numbers:”):
scanf(“%d”,&a[i]);
}
printf(“Enter the number to searched:”);
scanf(“%d”,&x);
for(i=0;i<n;++i)
{
if(x==a[i])
{
printf(“Number found”):
flag=1;
break;
}
}
if(flag==0)
printf(“Number not found”);
getch();
}
Result:
How many numbers: 5
Enter numbers=56
Enter numbers=1
Enter numbers=5
Enter numbers=89
Enter numbers=76
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Name Mark
Mahesh 400
Ram 360
………………………………………
………………………………………
Manish 410
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
{
deviation=value[i]-mean;
sumsqr+=deviation*deviation;
} variance=sumsqr/(float)n;
stddeviation=sqrt(variance);
printf("\n Number of items : %d\n",n);
printf(" Mean : %f\n",mean);
printf("Standard Deviation :%f\n",
stddeviation);
getch();
}
Result:
Input values : input -1 to end
2
3
8
78
-1
Number of items:4
Mean:22.750000
Standard Deviation:31.979486
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
// Entering elements of a matrix
Printf(“Enter elements of a matrix:”): for (i
= 0; i <3; i + +)
{
for (j = 0; j < 3; j + +)
scanf ("%d", & mat [i] [j]);
}
printf ("\nThe entered matrix is\n");
// The entered matrix is
for (i = 0; i <3; i + +)
{
for (j = 0; j <3; j + +)
printf ("%d \t", mat [i] [j]);
printf ("\n");
}
// Finding transpose
Printf(“The transepose of a matrix
is\n”):
for (j = 0; j < 3; j + +)
{
for (i = 0; i <3; i + +)
printf ("%d \t", mat [i] [j]);
printf ("\n");
}
getch ( );
}
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
24. Write a program to find sum of elements above and below the
diagonal element.
Ans: #include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j,sum1=0,sum2=0;
// Entering the elements of matrix
printf("Enter the elements of matrix =");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
scanf ("%d", &a [i] [j]);
}
for (i = 0; i < 3; i ++)
{
for (j = 0; j < 3; j ++)
{
if(i>j)
sum1=sum1+a[i][j];
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Enter elements of a matrix=1 Result:
2 1 00
3 0 10
4 0 01
5
6
7
8
9
Total of element above:19
Total of element below:26
25. Write a program to convert square matrix into a unit matrix.
Ans: #include<stdio.h>
#include<conio.h>
#define m 3
#define n 3 void
main ( )
{
int i, j;
int a[m][n]={ {1,2,3},
{4,5,6},
{7,8,9}
};
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
("%d\t", c[i][j]); printf {
("\n"); for (j = 0; j < 3; j + +)
} scanf ("%d", &b[i] [j]);
}
getch ( );
} // Finding subtraction of matrices
for (i = 0; i < 3; i + +)
{
for (j = 0; j <3; j + +)
c [i] [j] = a[i] [j] - b[i] [j];
}
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
i,j,k,l,d;
C Programming Language …211
// Printing the results printf("Enter the order of 1st matrix:");
Printf(“The result is\n”); scanf("%d%d",&m,&n);
for (i = 0; i <3; i + +)
{ printf("Enter the element of first matrix:-------
for (j = 0; j <3; j + +) \n");
printf ("%d\t", c[i][j]); for(i=1;i<=m;i++)
printf ("\n"); {
}
getch ( );
}
Result:
Enter the elements of matrix A=9
9
9
9
9
9
9
9
9
Enter the elements of matrix A=1
1
1
1
1
1
1
1
1
The result is
8 88
8 88
8 88
28. Write a program that reads two matrices A[ ] m x r and B[ ] r x n
and prints the result of the product of two matrices such that C[ ]
m x n = A[ ] m x r * B[ ] r x n.
Ans: #include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[10][10],b[10][10],c[10][10],m,n,
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
printf("\n");
212 … A Complete Refresh Manual of C Programming }
for(j=1;j<=n;j++) getch();
scanf("%d",&a[i][j]); }
}
printf("\n\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=d;j++)
{
c[i][j]=0; for(k=1;k<=l;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
printf("The resultant matrix is---\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=d;j++)
printf("%d\t",c[i][j]);
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
{ void main()
rsum[i]=0; {
for(j=1;j<=n;j++) int mat[10][10],n,i,j,flag;
rsum[i]=rsum[i]+mat[i][j];
printf("%d\n",rsum);
}
for(j=1;j<=n;j++)
{ csum[j]=0;
for(i=1;i<=m;i++)
csum[j]=csum[j]+mat[i][j];
}
Result:
Enter order of matrix:2
2
Enter the element of matrix-----
1
2
3
4
The entered matrix is-----
1 2
3 4
The matrix with row and colomn sum is- ----
1 2 3
3 4 7
4 6
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
if(flag)
printf("Symmetric matrix");
else
printf("Not symmetric matrix.");
getch();
}
Result:
Enter order of matrix:2
Enter the element of matrix--------
1
2
3
4
Symmetric matrix.
32. Write a program to compute the determinant of a square matrix of
nth order and then check whether the given matrix is singular or
not. You need to have two function as read() and process().
[T.U. 2065 Shrawan]
Ans: #include<stdio.h>
#include<conio.h>
int check(float [][],int);
float process(float[][],int);
void read(float[][],int);
void main()
{
float a[10][10],value;
int i,j,order;
printf(“Enter order of determinant:”0;
scanf(“%d”,&order);
read(a,order);
if(check(a,order==0)
value=0;
else
value=process(a,order);
printf(“Determinant value:%f”,value);
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
b elements in ascending order using a function.
r Ans: #include<stdio.h>
e #include<conio.h>
a #define MAX 100
k int a[MAX];
; void main ( )
} {
i void input( ); // function declaration
f input( );
( getch( );
j }
= void input ( )
= {
( void output (int a [ ], int n);
f int sort (int a [ ], int n);
o int a [MAX];
r int n;
d printf ("How many nos ?");
e scanf ("%d", &n);
r // Entering elements of array
) printf ("Enter elements \n");
) for(int i = 0;i<n;i++)
r {
e scanf ("%d", &a[i]);
t }
u printf ("Unsorted array\n");
r output (a, n);
n sort (a, n);
printf ("Sorted array \n");
0 output (a, n);
; }
} void output (int a[ ], int n)
{
for(j=0;j<forder;++j) printf ("\n");
{ for (int i=0;i<n;i++)
array[i][j]=array[j
][i]-array[j][k];
}
}
}
return 1;
}
33. Write a program to read a set of numbers from
keyboard and to sort out the given array of
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
mintemp,i;
printf ("Enter temperature \n");
for (i = 0; i < 7; i ++)
{
printf ("Maximum temperature of day
%d=",i + 1);
scanf ("%d", &maxm[i]);
printf ("Minimum temperature of day
%d=",i + 1);
scanf ("%d", &minm[i]);
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
char fname [5] [15], lname [5] [15];
int phone [5], i;
// Entering data
for (i = 0; i < 5; i + +)
{
printf ("\nEnter first name:");
scanf ("%s", fname [i]);
printf ("\nEnter last name:");
scanf ("%s", lname [i]);
do
{
printf ("\nEnter phone number : \n");
scanf ("%d", &phone [i]);
}
while (phone [i] < 20000 || phone [i] >
799999);
printf ("\n\n");
}
// printing in tabular form printf ("\n
First name \t last name \t phone number \n");
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
sentence : %d", count);
C Programming Language …223
getch ( );
printf(" .....\t ....\t ...........\n"); }
for (i = 0; i < 5; i + +) Result:
printf ("%s \t% s \t% d \n", fname [i], We are a student.
lname [i], phone [i]);
getch ( ); Total no.of alphabet ‘a’ occurred in a sentence:2
}
Result:
Enter first name:Ramesh
Enter last name: Thapa
Enter phone number:2525
Enter phone number:34345
……………………………………………………………
……………………………………………………………
……………………………………………………………
Enter first name:Niruta
Enter last name: Thapa
Enter phone number:63525
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
else if (str [i] >= 97 && str [i] <123)
224 … A Complete Refresh Manual of C Programming {
40. Write a program to read a line and replace space by "*". str [i] = str [i] - 32;
Ans: #include <stdio.h> printf("%c",str[i]);
#include <conio.h> }
void main ( ) else;
{ }
char line [20]; getch ( );
int i; }
printf ("Enter a line :");
gets (line);
// Replacing space by *
for (i = 0; line [i]! ='\0'; i++)
{
if (line[i]= =' ' ||line[i]==’\t’)
line[i]='*';
printf(“%c”,line[i]);
}
getch ( );
}
Result:
Enter a line:We are student of engineering.
We*are* student*of*engineering.
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Result:
Enter a string:->Computer is an electronic device
Enter the word to be searched:->is
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
44. Input names of 'n' number of students and sort them in alphabetical
order. [T.U. 2066 Jestha]
Ans: #include<stdio.h>
#include<conio.h>
#include<string.h>
void main ( )
{
char name [100] [100], temp[100];
int i, j, n;
printf ("Enter number of students =");
scanf ("%d",&n);
// Data putting for n students
for (i = 0; i < n; i ++)
{
printf ("Enter name :");
scanf ("%s", name [i]);
fflush (stdin);
}
// sorting methods
for (i = 0; i<n; i ++)
{
for (j = i+ 1; j < n; j ++)
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Amar
Janak
Kamal
Om
Umesh
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
void main()
228 … A Complete Refresh Manual of C Programming {
if(strcmp(name[i],name[j])>0) int i,j,k,l,flag=0;
{ char a[50];
strcpy(temp,name[i]); clrscr();
strcpy(name[i],name[j]); printf("Enter the characters in the array:\n");
strcpy(name[j],temp); gets(a);
l=strlen(a);
strcpy(temp1,add[i]); for(i=0;i<l-1;i++)
strcpy(add[i],add[j]);
strcpy(add[j],temp1);
}
}
printf("The result is\n");
printf("\nName\tAddress\n");
printf("...............\n");
for(i=0;i<5;i++)
printf("%s\t%s\n",name[i],add[i]);
getch();
}
Result:
Enter name and address:
Harish LTP
Kamal PTN
Niraj BKT
Om Baneshwor
Arun Naxal
The result is
Name Address
…………………………………………………
Arun Naxal
Harish LTP
Kamal PTN
Niraj BKT
Om Baneshwor
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
We arestudn.
47. Write a C program that reads two strings str1 and str2 and finds the
number of occurrence of smaller strings in large string.
Ans: #include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
char str1[100],str2[100],a[100],b[100],c[100];
int len1,len2,l1,l2,k=0,i,j,count=0,n;
clrscr();
printf("\n Enter the first string : ");
gets(str1);
printf("\n Enter the second string : ");
gets(str2);
len1=strlen(str1);
len2=strlen(str2);
if(len1>len2)
{
strcpy(a,str1);
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
c[k++]=a[n++];
c[k]='\0';
if(strcmp(b,c)==0)
++count;
k=0;
}
printf("\n The number of occurences is :
%d",count);
getch();
}
Result:
Enter the first string : We are a student.
Enter the second string:We
i=strlen(a);
j=strlen(b);
for(k=0;k<i;k++)
c[k]=a[k];
for(k=0;k<i;k++)
c[i+k]=b[k];
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
49. Write a C program to read a sentence. Remove all the space and
combine all the string at one.
Ans: #include<stdio.h>
#include<conio.h>
void main()
{
char a[100],b[100];
int i=0,j=0;
clrscr();
printf("Enter the set of lines:\n");
gets(a);
while(a[i]!='\0')
{
while(a[i]!=' '&&a[i]!='\t'&&a[i]!='\0')
{
b[j]=a[i];
j++;
i++;
}
while(a[i]==' '||a[i]=='\t')
i++;
} b[j]='\0';
printf("%s",b);
getch();
}
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
menu scanf("%s",&str);
1. Print message length in terms of characters printf ("%s", strupr (str));
break;
2. Print the message in reverse order
case 4 :
3. Print the message in capital letters printf ("Enter a string \n");
4. Copy the message from one location of screen to another
location
5. Check the palindrome
6. Exit
Ans: #include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
void main ( )
{
char str[40],dest[40],dest2[40];
int choice,l;
printf ("Main menu \n");
printf ("1. Print message length \n");
printf ("2. Print message in reverse
order\n");
printf ("3. Print the message in capital
letters \n");
printf ("4. Copy the message from one
location to another\n");
printf ("5. Check the palindrome \n");
printf ("6. Exit \n\n");
printf ("Enter your choice(1/2/3/4/5/6):");
scanf ("%d",& choice);
switch (choice)
{
case 1 :
printf("Enter a string:");
scanf("%s",&str);
l=strlen(str);
printf ("Length of message = %d",l);
break;
case 2 :
printf ("Enter a string \n");
scanf("%s",&str);
printf ("String in reverse order
\n");
printf ("%s", strrev (str));
break;
case 3 :
printf ("Enter a string \n");
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
for (j = i + 1; j <= 9; i ++)
if (num [i] < num [j])
{
temp = num [i];
num [i] = num [j];
num [j] = temp;
}
printf ("%d \t", num [i]);
}
break;
case 4 :
exit (0);
default :
printf ("\n Not allowed to type other");
exit (0);
}
getch ( );
}
Result:
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
if(a[j]!=a[i-1])
{
flag=0;
break;
}
if(a[j]==a[i-1])
{
j++;
i--;
}
}
if(flag==0)
printf("it is a not palindrome");
else
printf("it is a palindrome");
getch();
}
Result:
Enter a string:
ada
String is palindrome.
53. An organization has 5 stores and it deals in items. The stock position
of these items shown below :
Item 1 Item 2 Item 3 Item 4
Store 1 30 35 0 45
Store 2 24 35 12 0
Store 3 0 34 8 19
Store 4 12 0 34 18
Store 5 10 10 17 45
Develop a computer program in C to input this table and indicate the
items that are completely out of stock at store i, where the value of i
may be input from the keyboard.
Your program should calculate the total stock of each item, in the
organization and total items in store number 5.
Ans: #include <stdio.h>
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
se
ccount = ccount + 1;
}
Enter stoe no:2 lcount = ccount + vcount;
Item 4 of store 2 is out of stock.
printf ("No. of vowels = %d\n", vcount);
Total stock in store 5 is 82 printf ("No. of consonants = %d\n",
Total stock of item 1 is 76 ccount);
Total stock of item 2 is 114 printf("No. of words = %d\n", wcount + 1);
Total stock of item 3 is 71
Total stock of item 4 is 127
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Enter a sentence : sen[0]=toupper(sen[0]);
We are a student. printf ("%s",sen);
Enter a character:e }
break;
The total number of entered character in a case 3:
sentence =3 {
for (i = 0;i<n;i++)
sen[i]=tolower(sen[i]);
56. Write a program that reads a string and prints the string in
sen[0]=toupper(sen[0]);
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
3. Title Case
sen [i] = toupper (sen [i]); 4. toGGLE cASE
sen [0] = tolower(sen[0]);
for (i=0;i<n;i++) Enter your choice (1/2/3/4/):
{ 1
WE ARE STUDENT.
if(sen[i]==' ')
sen[i+1]=tolower(sen[i+1]);
else;
}
puts (sen);
}
break;
default :
{
printf ("Wrong choice \n");
getch ( );
exit (0);
}
}
getch ( );
}
Result:
Enter a string :
We are student.
Main Menu
1. UPPER CASE
2. Sentence case
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
{ }
temp = name [0]; temp[j]='\0';
for (j = 0; j < n – 1; j + +) i=0,j=0;
name [j] = name [j + 1); while(str2[i]!='\0'){
name (n – 1) = temp; str1[j++]=str2[i++];
puts (name); }
printf (" \n"); str1[j]='\0';
} i=0,j=0;
getch ( );
}
Result:
Enter name: space
Space paces acesp cespa espac
58. Write a
program to swap
string.
Ans:
#include<
stdio.h>
int main()
{
int i=0,j=0,k=0;
char
str1[20],str2[20],
temp[20];
puts("Enter first
string");
gets(str1);
puts("Enter second string");
gets(str2);
printf("Before swaping the strings
are\n");
puts(st
r1);
puts(st
r2);
while(s
tr1[i]!
='\0'){
temp[j+
+]=str1
[i++];
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
}while(str1[i]!=0 || str2[i]!='\0');
if(d==0)
printf("Both are equal");
else
printf("Not");
getch();
}
Result:
Enter the 1st string:We
Enter the 2nd string:We
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
gets(str1); }
printf("Enter the 2nd string:"); if(flag==1)
gets(str2); str1[i]='\0';
printf("%s",str1);
if(strlen(str1)<strlen(str2)) getch();
{ }
char *str; strcpy(str,str2);
strcpy(str2,str1); strcpy(str1,str); Result:
} Enter the 1st string:We
printf("Enter the index after which u want to insert Enter the 2nd string:Student
2nd string in 1st : ");
scanf("%d",&m); Enter the index after which u want to insert 2nd
i=0; string in 1st :2
while(i<=m)
{ Stuwet
i++;
} j=0; while(str2[j]!='\0')
{
str1[i]=str2[j];
i++; j++; if(str1[i]=='\0') flag=1;
Ans: #include<stdio.h> 61. Write a program that reads the name, roll number and marks in 5
#include<conio.h>
different subjects of 10 students, and prints name, roll and total
#include<string.h>
void main() marks obtained by each student in tabular form.
{ Ans: #include<stdio.h>
#include<conio.h>
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
printf ("Enter the number to be searched
printf ("The numbers entered in :\n");
ascending order are:\n"); scanf ("%d",&x);
for (i = 0; i < n; i ++)
printf ("%d \t", j[i]); first = 1;
printf (" \n \n"); last = n;
while((first <= last) && (pos== 0))
// Process for searching a number {
mid = (first + last)/2;
if (j [mid] == x)
pos = mid;
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
....\n"); printf("Enter ur choice"); printf("The data in namewise orders
are:\n\n");
for(i=0;i<n;++i)
printf("%s\t%d\t%d\n",name[i],roll[i],
marks[i]);
}
break;
case 2:
{
for(i=0;i<n;i++)
{
printf("Enter name,roll,marks:");
scanf("%s%d%d",name[i],&roll[i],
&marks[i]);
}
//Rollwise sorting
for(i=0;i<n;i++)
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
strcpy(temp1,name[i]); given matrix of order P x Q by 3. Display the resultant matrix.
[T.U. 2057 Chaitra]
Ans: #include<stdio.h>
#include<conio.h>
#include<math.h>
#define P 3
#define Q 4
void printmatrix(int a[P][Q]);
void main()
{
int i,j,a[P][Q],b[P][Q];
for(i=0;i<P;++i)
for(j=0;j<Q;++j)
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
printf("Enter elements:");
C Programming Language …251 scanf("%d",&b[i]);
{ }
printf(“a[%d][%d]=”,i,j); c=(int*)malloc(sizeof(int)*(M+N));
scanf(“%d”,&a[i][j]);
} for(i=0;i<P;++i) for(i=0;i<M;++i)
for(j=0;j<Q;++j) *(c+i)=a[i];
b[i][j]=pow(a[i][j],3);
for(i=0;i<M;++i)
{
printf("Enter elements:");
scanf("%d",&a[i]);
}
for(i=0;i<M;++i)
{
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
#include<conio.h>
C Programming Language …253 void sen(char str[80],int,int,int);
67. Write a program to display the following patters: void main()
H {
char str[80];
HE
int v,c,d;
HEL
HELL printf("Enter a string:");
HELLO gets(str);
HELLOW
Ans: #include<stdio.h>
#include<conio.h>
void main()
{
char str[]="HELLOW";
int i,j;
for(i=0;i<6;i++)
{
for(j=0;j<=i;j++)
{
printf("%c",str[j]);
}
printf("\n");
}
getch();
}
Result:
H
H E
H E L
H E L L
H E L L O
H E L L O W
68. Write a function that takes a string as input and counts the number
of vowels,consonants, numeric characters and other characters. The
calling function should read the string and pass it to the function.
After the function call, the count of vowels, consonants, numeric
characters and other characters should be displayed in the calling
function. [T.U. 2061 Poush]
Ans: #include<string.h>
#include<stdio.h>
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
if(a[i]==32 || a[i]=='\t' || a[i]=='\0')
254 … A Complete Refresh Manual of C Programming { c[k]='\0';
sen(str,v,c,d); if(strcmp(b,c)==0)count++;
//,int v,int c,int d) k=0;
getch(); }
} else
void sen(char str[80],int v,int c,int d) {
{
int i; v=0;c=0;d=0;
for(i=0;str[i]!=0;++i)
{
if (ch = = 'a' || ch = = 'e' || ch = = 'i' || ch
= = '0' || ch = = 'u' || ch = = 'A' || ch = =
'E' || ch = = 'O' || ch = = 'I' || ch = = 'U')
++v;
else if(str[i]>='0' && str[i]<='9')
++d;
else
++c;
}
printf("\nTotal number of Vowel=%d",v);
printf("\nTotal no. of digits=%d",d);
printf("Total no. of consonat=%d",c);
}
69. Write a C program that reads the text and counts all occurrences of
a particular word.
Ans: #include <string.h>
#includ<stdio.h>
#include<conio.h>
void main()
{
char a[100],b[20],c[20];
int i,count=0,k=0,len;
clrscr();
printf ("\nEnter a string:-> ");
gets(a);
printf("\nEnter the word to be searched:->
");
gets(b);
len=strlen(a);
for(i=0;i<=len;i++)
{
/*Assuming Space , Tab and NULL as word
separator*/
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
c[k]=b[j];
j++;
k++;
}
if(a[i]==b[j])
{
c[k]=a[i];
k++; i++; j++;
count++;
}
}
if(i<=n && j==m)
{
while(i<=n)
{
c[k]=a[i];
i++;
k++;
}
}
if(i==n&&j<=m)
{
while(j<=m)
{
c[k]=b[j];
i++;
j++;
}
}
Printf(The elements are:\n”);
for(i=0;i<m+n-count;i++)
printf("%d\t",c[i]);
}
Result:
How many numbers u want to enter in 1st array :3
Enter numbers in ascending order:
12
13
14
15
How many numbers u want to enter in 2nd array :3
Enter numbers in ascending order :
16
17
18
The elements are
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
int i;
clrscr();
printf ("\nEnter the string:-> ");
gets(a);
for (i=0;a[i]!='\0';i++)
{
if (a[i+1]=='\0')break;
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
i++;
}
puts(a);
getch();
}
74. Write a C function strend(s, t), which returns 1 if the string t occurs
at the end of the string s, and zero otherwise.
Ans: #include<conio.h>
#include<string.h>
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
read(a,m);
process(a,b,m);
display(b,m);
getch();
}
void read(int x[][10],int m)
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
c[i][j]=a[i][]j];
}
for(i=1;i<m;i++)
{
for(j=0;j<m;j++)
{
b[][]=0; for(k=1;k<m;k++)
b[i][j]=b[i][j]+a[i][k]*b[k][j];
}
}
}
void display(int [][10],int m)
{
int I,j;
for(i=0;i<m;++i)
{ printf(“\n”); for(j=0;j<m;++j)
printf(“%d\t”,x[i][j]);
}
}
75. Program to calculate standard deviation using function.
Ans: #include<math.h>
main()
{
float value[5], stddev();
int i;
printf(“Enter the values\n”);
for(i=0;i<5;i++)
scanf(“%f”, &value[i]);
printf(“Std.deviation is %f\n”, stddev(value,5);
}
float stddev(x,n)
float x[ ];
int n;
{
int i;
float mean(),y,sum=0.0;
y=mean(x,n);
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
names or more complex
expressions. Examples:
sqrt(),strcmp(),pow() etc.
b
.
U
s
e
r
d
e
f
i
n
e
d
f
u
n
c
t
i
o
n
The functions developed
by the user in any programming
language is called user defined
language.The user defined
function
name must not be same as
library functions.User defined
function consists following
statements:function declaration
,function call and
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
– Function portability: useful functions can be used in a number
of programs.
– Supports the top-down technique for devising a program
algorithm. Make an outline and hierarchy of the steps needed to
solve your problem and create a function for each step.
– Easy to debug. Get one function working well then move on to
the others.
– Easy to modify and expand. Just add more functions to extend
program capability
– For a large programming project, you will code only a small
fraction of the program.
– Make program self-documenting and readable.
10. What do you know about function declaration, function call and
function definition? Explain clearly.
Or, Explain various parts of functions.
Ans: Function declaration OR,
Function prototype [T.U. 2058]
It specifies function name, argument types and return value. Alerts
computer (and programmer) that function is coming up later.
Examples : void display( );
int sum (int, int, int); etc.
Function call
It is also a component of the function and causes the function to be
executed.
Examples : display ( );
result = sum(a, b, c); etc.
Function definition
The function itself contains the lines of code that constitute the
function.
Examples : void display( )
{
for (int i = 0; i < 9; i + +)
printf ("+");
}
int sum(int x, int y, int z)
{
res = x+ y + z;
return res;
}
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
{
C Programming Language …267
int factorial;
11. What do you know about function parameter and function return ? if(n==1||n==0)
Ans: Function parameter : The parameter specified in function call are return(1);
known as actual parameter and those specified in function declaration else
are known as formal parameter. factorial=n*fact(n-1);
Example : res = sum(a, b, c); return (factorial);
Here a, b, c are actual parameter
int sum (int x, int y, int z)
Here x, y, z are formal parameter.
Function return : Function can be organized into two type.
(a) Function that do not have a return type i.e. void function.
(b) Function that do have a return value (i.e. return (res);)
12. What do you understand by return statement? Explain with suitable
examples about its advantages and disadvantages.
[T.U. 2060, 2061 Baisakh]
Or, What is recursion ? Give its example.
Or, Write about return statement clearly stating different conditional
that may arise where returning from a function .
[T.U. 2058 Chaitra]
Ans: Recursion is a special case of function call where a function calls
itself. These are very useful in the situations where solution can be
expressed in terms of successively applying same operation to the
subsets of the problem.
Advantages
a. May be much easier to write.
b. To solve some problem which are naturally recursive such as
towers of Hanoi.
Disadvantages
a. Recursive functions are generally slower than non-recursive
function.
b. It is difficult to think recursively so one must be very careful
when writing recursive function.
c. May require lots of memory.
For example, a recursive function to calculate factorial of a number
n is given below:
fact(int n)
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
268 … A Complete Refresh Manual of C Programming Call by value means sending the values of the arguments- The value
of each of the actual arguments in the calling function is copied into
} corresponding formal arguments of the called function. The changes
Assume n=4, we call fact(4)
made to the formal arguments have no effect on the values of actual
Since n 1or 0, factorial=n*fact(n-1)
Factorial=4*fact(3)(again call fact function with arguments in the calling function. This technique of passing
n=3) arguments is called call by value illustrated by swapv(int x, int y)
=4*3*fact(2)(again call fact function with n=2) function in the following example.
=4*3*2*fact(1)(again call fact function with n=1)
=4*3*2*1 (terminating condition)
=24
We should always have a terminating condition with a
recursive function call otherwise function will never return.
Example : To calculate factorial using recursion
#include <stdio.h>
#include <conio.h>
int factorial (int);
void main ( )
{
int n;
printf ("Enter a number =");
scanf ("%d", &n);
printf ("Factorial no = %d", factorial (n));
getch ( );
}
int factorial (int n)
{
if (n < = 1)
return 1;
else
return (n * factorial (n - 1));
}
13. Explain the difference between 'call by reference' and 'call by
value'.
Or, In how many ways,we can pass arguments to a function? Explain
with examples. [T.U. 2061 Shrawan, 2066 Jestha]
Or, Distinguish between call by value and call by reference with
examples. [T.U. 2064 Falgun]
Or, How can you differentiate between call by value and call by
reference with examples in C programming. [T.U. 2067 Asadh]
Or, Differentiate between pass by value and pass by reference
arguments. Describe both with meaningful example.
[T.U. 2069 Ashad]
Ans: Differentiation between 'call by reference' and 'call by value':
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
void main()
C Programming Language …271 {
•
Life: till the control remains within the block in which register int x,y,z;
defined. x=0; y=0;
do
Example:Demonstration of automatic variable in function {
#include<stdio.h>
z=function1(x,y);
#include<conio.h>
int function1(int);
void main()
{
int i; for(i=1;i<=10;++i)
printf("%d\t%d\n",i,function1(i));
getch();
}
int function1(x)
int x;
{
int s=10; //Automatic variable
return s+=x;
}
Result:
1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
10 20
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
#include<conio.h>
272 … A Complete Refresh Manual of C
Programming int function1(int);
void main()
printf("%d\t%d\n",x,z);
++x; {
++y; int i; for(i=1;i<=5;++i)
}while(x<=5); printf("%d\t%d\n",i,function1(i));
getch(); getch();
} }
int function1(x,y) int function1(x)
register int x,y;
int x;
{
register {
temp; static int s=100;
temp=x*y return s+=x;
; return
temp; }
}
Result:
0
0
1
1
2
4
3
9
4
18
5
25
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
void function1()
{
for(y=0;y<=x-1;++y)
{
printf("Inside the main()\n");
printf("I am C program.\n");
}
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
To define a macro, # define statement is used. This Ans: Size of any function is calculated as:
statement, also known as macro definition takes the following Size of function = Size of all local variable which has declared in
general form: function + Size of those global variables which has used in function+
#define identifier string Size of all its parameter+ Size of returned value if it is an address.
The pre-processor replaces every occurrence of the Example:
identifier in the source code by the string. The preprocessor
directive definition is not terminated by a semicolon. For
example
#define COUNT 100 will replace all occurrences of
COUNT with
100 in the whole program before compilation.
18. What is a subroutine? How do subroutines help in program
writing? Ans: Subroutine [T.U.
2062 Baisakh] A subroutine is a named, independent section of C
code that
performs a specific task and optionally returns a value to the
calling
program. Some of the important characteristics of subroutine
that help in program writing are:
• A subroutine is named, each have a unique name. By
using that name in another part of the program, one can
execute the statements contained in the subroutine.
• A subroutine is independent that can perform its task
without interference from or interfering with other parts
of the program.
• A subroutine performs a specific task. A task is a
discrete job that a program must perform as part of its
overall operation, such as sending a line of text to a
printer, sorting an array into numerical order, or
calculating a cube root.
• A subroutine can return a value to the calling program.
When a program calls a subroutine, then statements it
contains are executed. These statements can pass
information back to the calling program.
19. How to calculate size of a function in c? Explain.
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Result:
Enter the value of radius =2
2. Write a program to print all even numbers from 2-40 using non-
returnable function.
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Ans: #include<stdio.h>
#include<conio.h>
void print_even ( );
void main ( )
{
printf ("Here are even numbers from 2 to
40\n");
print_even ( );
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
}
C Programming Language …277
printf ("You got the result");
getch ( );
}
void print_even ( )
{
int a;
for (a = 2;a<=40;a+=2)
printf ("%d\n", a);
}
Result:
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
You got the result
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
void main()
278 … A Complete Refresh Manual of C Programming {
sum=sum+num; printf ("Sum of square of first 10
} natural numbers.\n");
printf("Sum of integer=%d",sum); sum();
} printf ("You got sum of first 10 natural
numbers.\n");
Result: getch ( );
Enter,how many number?5 }
Enter number:2 void sum( )
Enter number:3 {
Enter number:4 int a, sum = 0;
Enter number:5 for (a = 1; a < = 10; a ++)
Enter number:6 sum = sum + a * a;
printf ("Sum of square of first 10
Sum of integer:20 natural numbers = %d\n",sum);
}
4. Write a program to find sum of square of first 10 natural numbers
using functions. Result:
Sum of square of first 10 natural numbers
Ans: #include <stdio.h> Sum of square of first 10 natural numbers=385
#include <conio.h>
You got sum of first 10 natural numbers
void sum();
void sumofint(int n) 5. Write a program to find multiplication table of any number using
{ function.
int x,sum=0,num; Ans: #include<stdio.h>
for(x=0;x<n;++x) #include<conio.h>
{ void numbers(int );
printf("Enter number:"); void main( )
scanf("%d",&num); {
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
6. Write a program to count the positive and negative numbers from 'n'
entered numbers using functions.
Ans: #include<stdio.h>
#include<conio.h>
void numbers(int no[100],int n );
void main ( )
{
int n,no[100],i;
printf ("Enter the value of n =\n");
scanf ("%d", &n);
for (i = 1; i<n; i++)
{
printf ("Enter numbers :");
scanf ("%d", &no[i]);
}
numbers(no,n);
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Enter numbers :20
Enter numbers :-25
Result:
programming is fun.
programming is fun.
programming is fun.
programming is fun.
programming is fun.
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
void display(char
C Programming Language …281 name[20][20]);
8. Write a program to display all the prime numbers within the ranges void main()
specified by the users using functions. [T.U. 2063 Baisakh] {
char
Ans: #include<stdio.h>
name[
#include<conio.h>
20][2
void sum(int);
0];
void main( )
{ int
i;
int x;
printf("Enter,how many two numbers= for(i
=0;i<
");
scanf ("%d", &x); 20;++
i)
printf(“\n”);
printf(“The prime number are:\n”); {
sum(x);
getch ( );
}
void sum(int x)
{
int i,j;
for (i =1;i<=x;++i)
{
for(j=2;j<=i;++j)
{
if(i%j==0)
break;
} if(i==j)
printf("%d\t",i);
}
}
Result:
Enter,how many two numbers=20
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Result:
Enter name:Ram Enter name:Hari
Enter name:Umesh Enter
name:Pradip Enter name:Niruta
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
void main()
C Programming Language …283 {
10. Write a C program to generate fibonacci series up to nth terms. int ceount=0,ocount=0;
[T.U. 2063 Poush] long int n;
Ans: #include<stdio.h>
printf(“Enter value of n:”);
#include<conio.h> scanf(“%ld”,&n);
void fibonacci(int,int,int); count(n,&ecount,&ocount);
void main()
{
int n,f0=0,f1=1;
printf("Enter number of terms:");
scanf("%d",&n);
printf("%d\t%d\t",f0,f1);
fibonacci(f0,f1,n-2);
getch();
}
void fibonacci(int f0,int f1,int n)
{
int c;
if(a==0)
{
return a;
}
else
{ c=f0+f1;
printf("%d\t",c);
}
fibonacci(f0,f1,n-1);
}
Result:
Enter number of terms:5
1 12 3 5
11. Write a program to read an integer number, count the even and odd
digits present in the entered number. You must write a function for
calculating the result and result must be displayed in the main
function itself. [T.U. 2063 Poush]
Ans: #include <stdio.h>
#include <conio.h>
void count(lont int,int *,int *);
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
for(i=0;i<n;++i)
284 … A Complete Refresh Manual of C Programming {
printf(“\nTotal no. of even digit.=%d”,ecount); if(*(x+i)%2==0)
printf(“\nTotal no. of odd digits.=%d”,ocount); *e=*e+1;
getch(); else
} *o=*o+1;
void count(int n,int *e,int *o) }
{ }
int r;
while(n!=0)
{
r= n%10;
if(r%2= =0)
*e=*e+1;
else
*o=*o+1;
n=n/10;
}
}
12. Write a program that reads ‘n’ positive integer numbers into an
array dynamically. Pass this to a function that counts the number of
even and odd numbers. Return the result to the calling function and
display the result. [T.U. 2064 Jestha]
Ans: #include <stdio.h>
#include <conio.h>
void count(int *,int *,int *,int);
void main()
{
int *x,ecount=0,ocount=0,i;
printf(“Enter value of n:”);
scanf(“%d”,&n);
x=(int *)malloc(n*sizeof(int));
printf(“Enter numbers:”);
for(i=0;i<n;++i)
scanf(“%d”,(x+i));
count(x,&ecount,&ocount,n);
printf(“\nTotal even nos.=%d”,ecount);
printf(“\nTotal odd nos.=%d”,ocount);
getch();
}
void count(int *x,int *e,int *o,int n)
{
int i;
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
getch ( );
16. Write a function to calculate factorial of an integer. }
Ans: #include <stdio.h> long int factorial (int n)
#include <conio.h> {
long int factorial (int); long int fact = 1;
void main ( ) for (int i = 1;i<=n; i ++)
{ fact = fact * i;
int n; return fact;
long int fact; }
printf ("Enter a number ="); Result:
scanf ("%d", &n); Enter a number =5
fact = factorial (n); Factorial number of 5 = 120.
printf ("Factorial number of %d = %ld.",
n,fact);
}
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
{
18. A digit positive integer is entered through the keyboard, write a int s, rs;
function to calculate sum of digits of number. int n;
(a) Without using recursion. printf ("Enter number =");
(b) Using recursion. scanf ("%d", &n);
s = wsum (n);
Ans: #include <stdio.h> printf ("Sum of digits without recursion
#include <conio.h>
=
int wsum (int);
%d\n",s);
int rsum (int);
rs = rsum (n);
void main ( )
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
19. Write a program to print 10 positive integers and their factorials.
Ans: #include<stdio.h>
#include<conio.h>
long int factorial (int);
void main ( )
{
int i;
printf ("\nFactorial numbers are:\n");
for(i=1;i <=10; i++)
{
printf ("%d=%ld\n",i,factorial(i));
}
getch ( );
}
long int factorial(int n)
{
long int fact = 1;
int i;
for (i = 1; i <= n; i++)
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
} 22. Write a program that passes an array to a function and print the
Result: largest and smallest element. [T.U. 2069 Ashad]
Enter the value of x:5 Ans: #include<stdio.h>
#include<conio.h>
The value for x=5.000000 void numbers(int no[100],int n );
is -39.000000. void main ( )
{
int n,no[100],i;
Sum=15 printf ("Enter the value of n =");
scanf ("%d", &n);
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
Enter the value of n =5 double term=0,s=0;
O
O
N
N
y
y
bu
Enter numbers:50 10 15 20 25
bu
printf("Enter the value of a:");
to
to
ww
ww
om
om
k
k
lic
lic
scanf("%f",&a);
C
C
.c
.c
w
w
tr re tr re
Maximum no=50
.
.
ac ac
k e r- s o ft w a do{ k e r- s o ft w a
s=s+term;
term=pow(a,i)/series(i);
printf("Term=%lf\n",term);
++i;
}while(term>0.000001);
printf("Sum=%lf",s);
getch();
}
long int series(int n)
{
long int fact=1;
int j;
if(n==0)
return 1; else
{
for(j=1;j<=n;++j)
fact=fact*j; return
fact;
}
}
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
getch();
}
void insert(char str1[20],char str2[20],int n)
{
int i,j;
char temp[20];
for(i=n-1;j=0;str1[i]!=’\0’;++i;++j)
temp[i]=str1[i];
temp[j]=’\0’;
for(i=n-1;j=0;str2[j]!=’\0’;++i;++j)
str1[i]=[str2[j];
for(i=n-1;j=0;str1[i]!=’\0’;++i;++j)
str2[i]=temp[j];
str2[i]=’\0’;
}
27. Write a recursive function to find the sum of natural numbers. Test
the function in your main program. [T.U. 2062 Poush]
Ans: #include <stdio.h>
#include <conio.h>
void main ( )
{
int n;
static int i=1,sum=0;
if(i==1)
{
printf ("How many terms to add:-");
scanf ("% d",&n);
}
if(i<=n)
{
sum=sum+i;
i++;
main();
}
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
if (p== 1)
return n; else
return(table(n,p-1)+n);
}
Result:
Enter nunmbers :-5
5x1=5
5x2=10
5x3=15
5x4=20
5x5=25
5x6=30
5x7=35
5x8=40
5x9=45
5x10=50
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
printf("Enter two number:");
scanf("%d%d",&a,&b);
gcd=recgcd(a,b);
printf("GCD of %d and %d is=%d",a,b,gcd);
getch();
}
int recgcd(int x,int y)
{
int r;
if(y==0)
{
return x;
}
else
{
r=x%y;
return recgcd(y,r);
}
}
Result:
Enter two number:5 8
GCD of 5 and 8 is 1.
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
clrscr();
C Programming Language …299 printf (" Value of m : ");
34. Write a C program to ask the user to enter the value of a number scanf ("%d",&m);
and power to be calculated for that number. Use recursive method. printf (" Value of n : ");
scanf ("%d",&n);
Ans: #include<conio.h>
#include<stdio.h>
sum=sum_recursive(m);
int power(int,int);
printf ("\n\n\n\n%d",sum);
void main()
{
int n,p,result;
printf("Enter the value of n and p:");
scanf("%d%d",&n,&p);
result=power(n,p);
printf("Base %d to power %d is %d.",
n,p,result);
getch();
}
int power(int n,int p)
{
static int r=1;
if(p==0)
{
return 1;
}
else
{ r=r*n;
power(n,p-1);
}
return r;
}
Result:
Enter the value of n and p:2 3
Base 2 to power 3 is 8.
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
int n,s;
300 … A Complete Refresh Manual of C Programming printf(“Enter number of terms:”);
} scanf(“%d”,&n);
int sum_recursive(int m) s=sum(n);
{ printf(“\n Sum=%d”,s);
int sum; getch();
if (m == n) }
return (n*n); int sum(int n)
else {
sum = (m*m) + sum_recursive(m+1);
printf ("\t%d",sum);
return sum;
}
36. Write a nested macro that gives the minimum of three values.
Ans: #include<conio.h>
#include<stdio.h>
#define min(a,b) ((a>b)?b:a)
#define minthree(a,b,c) (min(min(a,b),c))
void main()
{
int x,y,z,w;
clrscr();
printf("Enter three numbers :\n");
scanf("%d%d%d",&x,&y,&w);
z=minthree(x,y,w);
printf("Minimum of three value is %d",z);
getch();
}
Result:
Enter three numbers :20 3 8
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
#include<stdio.h> scanf(“%d”,&n);
float sum(int);
int fun(int);
void main()
{
int n;
float s;
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
};
data_type member 2;
}
..................................
..................................
data_type member n;
structure members
structure type variables
structure user_defined_name var1, var2, ........;
e.g. struct student
{
char name [20];
int class;
int age;
};
struct student a;
In the above example,
struct =keyword
student = structure data type name
name[20],class and age =Member name
a=declaring structure variable
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
The data members are accessed by structure variables with followed
by a . (period sign), called dot operator.
The data of structure can be accessed in the way
a . name;
a . class;
a . age;
with the help of dot operator.
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
{
char name;
int pages;
float price;
};
The compiler allocates a piece of storage that is large enough to
store the largest variable types in union. All three variables will share
the same address and 4 bytes of memory is allocated to it. This
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
printf("%s",stu[i].name);
C Programming Language …307 printf("\nroll no");
program of structure will read name, roll no and marks in 6 subject of printf("%d",stu[i].roll_no);
3 students & then display name and roll of student who scored more for(j=0;j<6;j++)
{
than 70% marks in total.
printf("\nmarks in %d
#include<stdio.h> subject\t",j+1);
#include<conio.h> printf("%d",stu[i].marks[j]);
struct student
{
char name[10];
int roll_no;
int marks[6];
int total;
int per;
};
void main()
{
struct student stu[3];
int i,j,req;
clrscr();
for(i=0;i<3;i++)
{
stu[i].total=0;
printf("enter data for %d students
:",i+1); printf("\nenter name");
scanf("%s",stu[i].name);
printf("\nenter roll no ");
scanf("%d",&stu[i].roll_no);
printf("\nenter marks in subjects\t");
for(j=0;j<6;j++)
{
printf("\nenter marks in %d
subject\t",j+1);
scanf("%d",&stu[i].marks[j]);
stu[i].total=stu[i].total+stu[i].marks[j];
}
stu[i].per=stu[i].total/6;
printf("\n");
}
for(i=0;i<3;i++)
{
if(stu[i].per>70)
{
printf("\nSTUDENT %d",i+1);
printf("\nname :");
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
int main()
308 … A Complete Refresh Manual of C Programming {
} company employee[MAX];
printf("\nTOTAL :%d",stu[i].total); int n;
} printf(“A program for collecting employee\n”);
} information";
getch(); printf(“And displaying the collected
} information");
4. What do you understand by structure? Explain about printf("How many employees:");
nested structure with examples. [T.U. scanf(“%d”,&n);
2061 Shrawan] void CollectInformtion(company
employee[MAX], int n);
Ans: void Display(company employee[MAX], int n);
Structure:Structure,Question no(1)
Nested structure // functions calling
When a structure is decleared as the member of CollectInformtion(employee, n);
another structue, it is called Structure within a structure. It is Display(employee, n);
also known as
nested
structure.
Example:
Write a program which can collect employee
information(such
as employee name, id, sex, date of birth etc.) of a
company and display the collected information t
#include<iostream.h>
#include<string.h>
#define MAX
1000 struct
date{
int
day;
int
month
; int
year;
};
struct company{
char name[20];
long int employee_id;
char sex[5];
int age;
struct date dob;
};
company employee[MAX];
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
printf(“%s\n”,employee[i].dob.month");
printf(“%s\n”,employee[i].dob.year);
}
}
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
char name [15];
C Programming Language …311 float price;
int pages;
2.13 C Programming - 7 } list [10];
int i;
1. Write a program to read roll number, name and age of a student and
display it in proper format. // Entering data from keyboard
Ans: #include<stdio.h> for (i = 0; i < 10; i + +)
#include<conio.h> {
struct student{
int rollno;
char name[20];
int age;
}a;
void main()
{
printf(“Enter roll number:”);
scanf(“%d”,&a.rollno);
printf(“Enter name:”);
scanf(“%s”,a.name);
printf(“Enter age:”);
scanf(“%d”,&a.age);
printf(“Name=%d\nAge=%s\nRoll
no=%d”,a.rollno,a.name,a.age);
getch();
}
Result:
Enter roll number:2
Enter name:manish
Enter age:25
Name=Manish
Age=25
Roll Number=2
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
{
312 … A Complete Refresh Manual of C Programming int tot=0,per,i;
printf ("Enter name, price & pages of %d printf("Enter name:");
book:",(i + 1)); scanf("%s",list.name);
scanf ("%s %f %d", list [i].name, &list [i]. for(i=0;i<5;i++)
price,&list [i]. pages); {
} printf("Enter mark:");
scanf("%d",&list.mark[i]);
// Displaying data tot=tot+list.mark[i];
printf ("Name \t . price \t pages \n");
for (i = 0; i < 10; i + +)
{
printf ("%s \ t % f \t % d\n", list
[i].name, list [i].price, list [i]. pages);
}
getch ( );
}
Result:
Enter name, price & pages of 1 book:
Practical Physics 200 300
Enter name, price & pages of 1 book:
Practical Chemistry 344 500
………………………… ……………………… ………………………
Enter name, price & pages of 1 book:
C programming 300 400
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
…………………………………………………
…………………………………………………………
Enter name :Lila
Enter address:Kupondol
Enter phone number:5543344
for (i = 0; i < 5; i + +)
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Printf(“\nSN\tProcessor\tMemory\tHDD\t
Monitor\t Casing\t Total”);
Printf(“\n………………………………………………………………………”):
for (i = 0; i < 5; i + +)
{
Printf(“\n%d\t%d\t%d\t%d\t%d\t%d\t%d”,
i+1,a[i].pricessor,a[i].memory,a[i].HDD
,a[i].monitor,a[i].casing,a[i].processor
+a[i].memory+a[i].HDD+a[i].monitor+
a[i].casing);
}
getch();
}
Result:
Enter price of processor:4500
Enter price of memory:1800
Enter price of HDD:3000
Enter price of monitor:10000
Enter price of casing:1500
………………………………………………………………….
……………………………………………………………………
……………………………………………………………………
Enter price of processor:4000
Enter price of memory:1600
Enter price of HDD:3200
Enter price of monitor:9500
Enter price of casing:2000
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
mark [j];
316 … A Complete Refresh Manual of C Programming }
6. Write a program to read and display the }
name,address,telephone number and salary of an employee
// Displaying result
using structure. [T.U. 2057]
printf ("\ n Roll number \t Name \t
Ans: We can solve this as structure, question no (1). Total\n");
7. Write a computer program to read and display the name, printf("......................... \n");
address for (i = 0; i < 10; i + +)
,account number and balance of a person using structure. printf ("%d \t % s \t %f", list [i]. roll,
[T.U. 2058 list [i].name,
Poush] Ans: We can solve this as structure,question no(1). list [i].total);
8. Program that reads the name, roll number and marks in 5 getch ( );
different subjects of 10 students and prints name, roll }
and total marks obtained by each student.
Ans: #include
<stdio.h>
#include <conio.h>
struct student
{
int roll;
char name [20];
float mark [5], total;
};
void main ( )
{
int i, j;
struct student list [10];
// Entering data
for (i = 0; i < 10; i + +)
{
printf ("Enter name :");
scanf("%s”,list
[i].name); printf ("Enter
Roll number:"); scanf
("%d", &list [i]. roll);
list [i].total = 0;
printf ("Enter marks in 5
different subjects:\n");
for (j = 0; j < 5; j + +)
{
scanf ("%f", & list [i].
mark[j]); list [i].total =
list [i].total + list [i].
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
int i,j;
printf("enter name and age:");
for(i=0;i<10;i++)
scanf("%s%d",a[i].name,a[i].age);
for(i=0;i<10;i++)
{ for(j=i+1;j<10;j++)
if(strcmp(a[i].name,a[j].name)>0)
{
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
for(i=0;i<10;i++) {
printf("%s\t%d\n",a[i].name,a[i].age); int n, i, total = 0;
getch(); float average;
} printf ("\nEnter number of students:");
Result:
Enter name and age:Madan 27
Enter name and age:Kamal 24
Enter name and age:Laila 25
Enter name and age:Chhaila 45
Enter name and age:Pankaj 43
The result is
Name Age
…………………………………………………
Chhaila 45
Kamal 24
Laila 25
Madan 27
Pankaj 43
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
// Displaying results
for (i = 0; i < n; i++)
{
if (list [i]. mark <4. 0)
printf("Roll = %d \n Name = % s \n Mark =
% f ",list[i].roll, list [i]. name, list
[i]. mark);
else if (list [i].mark > 7.9)
printf ("Roll = %d \n Name = %s \n Mark =
%f \n mark is more than 7.9", list [i].
roll, list [i]. name, list [i]. mark);
else;
}
printf ("Average marks = %.2f ", average);
getch ( );
}
Result:
Enter number of students:2
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
{
list [i]. total = 0;
printf ("Enter registration number =");
do
{
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
else Ans: #include<stdio.h>
strcpy (list [i]. div, "fir"); #include<conio.h>
} struct vowner
{
// Displaying results char name[20];
char addr[20];
int vnum;
char phone[20];
}
void main()
}
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
scanf("%f%f",&b.real,&b.imag); void main()
add(a,b);
sub(a,b);
getch();
}
void add(struct complex a1,struct complex a2)
{
float rsum,isum;
rsum=a1.real+a2.real;
isum=a1.imag+a2.imag;
printf("Sum of complex numbers=%f+i(%f)\n",
rsum,isum);
}
void sub(struct complex b1,struct complex b2)
{
float rdif,idif;
rdif=b1.real+b2.real;
idif=b1.imag-b2.imag;
printf("Difference of complex number=
%f+i(%f)\n",rdif,idif);
}
Result:
Enter first real and imaginary value:5 6
Enter second real and imaginary value:7 8
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
void displaytime(TIME t)
C Programming Language …325 {
{ printf("%d:%d:%d",t.hr,t.min,t.sec);
struct TIME starttime,stoptime, }
sumtime,diftime; Result:
printf("\n\n Start Time.\n"); Start Time
Enter hour:
printf("Enter hour:\n"); 8
scanf("%d",&starttime.hr);
printf("Enter minute:\n");
scanf("%d",&starttime.min);
printf("Enter second:\n");
scanf("%d",&starttime.sec);
printf("\nStop Time\n");
printf("Enter hour:\n");
scanf("%d",&stoptime.hr);
printf("Enter minute:\n");
scanf("%d",&stoptime.min);
printf("Enter second:\n");
scanf("%d",&stoptime.sec);
adddiff(starttime,stoptime,
&sumtime,&diftime);
printf("\nStart time\n");
displaytime(starttime);
printf("\nStoptime\n");
displaytime(stoptime);
printf("\nAdd time\n");
displaytime(sumtime);
printf("\nSubtract Time\n");
displaytime(diftime);
getch();
}
void adddiff(struct TIME t1,struct TIME t2,struct
TIME *t3,struct TIME *t4)
{
t3->sec=t1.sec+t2.sec;
t3->min=t1.min+t2.min;
t3->hr=t1.hr+t1.hr;
t4->sec=t1.sec-t2.sec;
t4->min=t1.min-t2.min;
t4->hr=t1.hr-t1.hr;
}
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Start Time
8:30:30
Stop Time
10:10:10
Add Time
16:40:40
Subtract Time
0:20:20
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
struc rcmp(student[i].name,
t student[j].name)<=0)
schoo {
l temp = student[i];
temp; student [i] = student [j];
int student [j] = temp;
i, j; }
for }
(i =
0; i
< n;
i++)
{
f
} o
r
Result:
(
j
i
+
1
;
<
n
;
j
+
+
)
i
f
(
s
t
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
How many data ?3
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
// Entering data
for (i = 0; i < 20; i + +)
{
printf ("Enter Roll number:");
scanf ("%d",&list [i]. roll);
printf ("Enter marks in 5 different
subjects:\n");
for (j = 0; j < 5; j + +)
{
scanf ("%f", &list [i]. mark[j]);
} decl(list);
getch();
}
void decl(struct student list[])
{
float total[20],max;
struct student s; int
i,j; for(i=0;i<20;++i)
{
total[i]=0;
for(j=0;j<5;++j)
{
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Declaration of pointer
variables
A pointer should be declared before its use like any other
variable. Data type of the pointer is the data type of the
variable to which it is pointing.
Pointer variable is defined by using an indirection
symbol ‘*’. Using this operator, it becomes possible to point
to the address of other variable. A pointer variable is declared
as follows;
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
C Programming Language …333 Or, Compare array and pointer with examples. [T.U. 2063 Baishakh]
Or, Compare with and pointer syntactically and physically.
Syntax: Data_type * variable_name; [T.U. 2065 Sheawan]
Eg; int *ptr; Ans: In fact, the compiler translate array notation into pointer notation
Here int indicates that the pointer will point to variables of when compiling. Since the internal architecture of the
integer type. ‘*’ indicates the pointer variable and ptr is the name of microprocessor understands pointers but doesn’t understand arrays.
pointer variable that points to integer variable. To make clear the relationships between pointer and arrays
When pointer is declared, it doesn’t points to anywhere. here is a simple program.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,*p,*q;
printf(“Enter the value of a:”);
scanf(“%d”,&a);
p=&a;
q=&p;
printf(“Value of a=%d\t Address=%d\n”,a,&a);
printf(“Value of a=%d\t Address=%d\n”,*p,p);
printf(“Value of a=%d\t Address=%d\n”,**q,*q);
getch();
}
3. Explain the meaning of each of the following declarations :
(i) int *p;
(ii) int *p [10];
(iii) int (*p) [10];
(iv) int *p [void];
(v) int *p [char *a];
Ans: Meaning of declared pointer statements are :
(a) int *p; = A printer that will point to an integer data.
(b) int *p [10]; = P is 10-element array of pointer to integer
data.
(c) int (*p) [10]; = P is a pointer to a 10-element integer data.
(d) int *p [void]; = P is a function that returns a pointer to an
integer data.
(e) int *p (char*a); = P is a function that accepts an argument
which is a pointer to a character and
returns a pointer to an integer data.
4. What is the relation of array and pointer? Explain.
[T.U. 2067 Ashwin]
Or, How do you relate array and pointer, illustrate with example.
[T.U.2057]
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
334 … A Complete Refresh Manual of C Programming indirection or dereference operator * gives the “contents of an object
pointed to by a pointer”.
/*Print values from array*/ A pointer is declared as follows:
#include<stdio.h> int *ptr;
#include<conio.h> where *ptr is a pointer of int type.
void main() //display the contents of an array using pointer
{
int i,num[5]={42,89,65,74,25};
[T.U. BBA Model Question 2009]
#include<conio.h>
for(i=0;i<5;i++)
printf(“Value=%d\\t”,num[i]);
getch();
}
This is straight forward program. Array notation is used to
access individual elements of the array in the expression num [i]. This
expression can be replaced by *(num+i) as:
#include<stdio.h>
#include<conio.h>
void main()
{
int *p,i,num[5]={42,89,65,74,25};
for(i=0;i<5;i++)
printf(“Value=%d\t”,*(num+i));
getch();
}
Since array elements are always stored in contiguous memory
location a pointer then incremented always points to an immediately
next location of its type.
Here, num[i]=*(num+i);
&num[i]=num+i;
42 89 65 74 25
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
[T.U. 2062 Poush] an array of elements, initializes them to zero and then returns a pointer
Or, Describe three dynamic memory allocation functions available in C. to the memory. This function is normally used for requesting memory
[T.U. 2058 Chaitra] space at run time. While malloc allocates a single block of storage
Or, What is dynamic memory allocation? What library functions are
used for this and what type of values do these functions return?
[T.U. 2059 Poush]
Or, What is dynamic memory allocation?What are the various functions
available in C for dynamic memory allocation. [T.U. 2064 Jestha]
Ans: Direct Memory Allocation(DMA) [T.U. 2065 Shrawan]
The mechanism of allocating required amount of memory at
run time is called dynamic allocation of memory. Sometimes it is
required to allocate memory at runtime. When we declare array in any
program, the compiler allocates memory to hold the array. Now
suppose the numbers of items are larger than the defined size then it is
not possible to hold all the elements and if we define an array large
enough and data to be stored is less, in that case the allocated memory
is wasted leading to a need for dynamic memory allocation. In this
mechanism we use a pointer variable to which memory is allocated at
run time.
The various memory allocation functions are described below:
(i) malloc( ) [T.U. 2058 Poush]
It is a memory allocation function that allocates requested size
of bytes and returns a pointer to the first byte of the allocated space.
The malloc function returns a pointer of type void so we can assign it
to any type of pointer. It takes the the following form:
ptr= (cast type *) malloc(byte-size);
where ptr is a pointer of type cast-type. For example, the statement
x=(int *) malloc(10 *sizeof(int)) means that a memory space
equivalent to 10 times the size of an int byte is reserved and the
address of the first byte of memory allocated is assigned to the pointer
x of int type.
The malloc function can also allocate space for complex data
types such as structures. For example:
ptr= (struct student*) malloc(sizeof (struct student)); where ptr is a
pointer of type struct student.
(ii) calloc( ) [T.U. 2058 Poush, T.U. 2065 Shrawan]
It is another memory allocation function that allocates space for
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
ptr=stu;
while(ptr<stu+3)
{
printf("%s%5d\n",ptr->name,ptr->roll_no);
ptr++;
}
getch();
}
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
34
C Programming Language …339 The array elements after storing:
Address=1245024 Value=12
2.15 C Programming – 8 Address=1245028 Value=21
Address=1245032 Value=23
Address=1245036 Value=32
1. Write a program to print values and addresses of 10 elements stored
Address=1245040 Value=34
in an array.
Ans: #include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
int *p,*q,a[10];
p=&a[0];
q=p;
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
temp = *x;
340 … A Complete Refresh Manual of C Programming *x = *y;
2. Write a C program using pointers to compute the sum of all *y = temp;
elements stored in an array. ) }
Ans: #include<stdio.h> Result:
#include<conio.h> Enter two integers :4 5
void main()
{ Now,a=5 b=4
int i,*p,s;
static int a[10]={10,20,30,40,50,60,70,80,90,100};
clrscr();
i=0;
p=a;
s=0;
while(i<10)
{
s=s+ *p;
i++;
p++;
}
printf("sum=%d",s);
getch();
}
Result:
Sum=550
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Ans: #include<stdio.h>
#include<conio.h>
void arr(int *);
int i,j,n;
void main()
{
int *p,*q,a[10];
p=&a[0];
q=p;
printf("Enter the value of N:");
scanf("%d",&n);
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
//to print the roll and name of student
printf("%d\t%s\t",q->roll,q->name);
//to print the marks of 5 subjsects
for(j=0;j<5;j++)
{
if(q->mark[j]<40)
printf("%0.1f*\t",q->mark[j]); else
printf("%.1f\t",q->mark[j]);
}
//to print the marks of particular student
printf("%0.1f\n",total[i]);
q++;
}
getch();
}
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Enter the record of std[2]: 10. Write a program which concates a string to other [use pointer].
Enter the roll:9
Ans: #include<stdio.h>
Enter the name:Pintu
#include<conio.h>
Enter the marks in 5 subjects:
void preconcatenate(char str1[20],char str2[20]);
54 56 76 87 90
void main ( )
{
Roll Name SUB1 SUB2 SUB3 SUB4 SUB5 Total char str1[20], str2[20];
2 Mohan 45 65 75 85 95 365 int i;
9 Pintu 54 56 76 87 90 363 // Entering two strings
printf ("Enter two strings :");
scanf ("%s %s", str1, str2);
9. Write a program that will print out all the rotations of a string typed // Add two strings
into it. For example, the rotations of the word "empty" are : empty preconcatenate(str2,str1);
mptysptysm tysmp ysmpt [use pointer] printf (" \n \n After adding str2 to
Ans: #include<stdio.h> other");
#include<conio.h> printf (" \n The new string : %s", str2);
getch ( );
#include<string.h>
}
void main ( )
if (*s = = '\o')
{
s = str;
char str [20];
}
int i, j, n;
printf ("\n");
char *s;
}
printf ("Enter a string :");
printf ("\n \n");
scanf ("%s", str);
getch ( ):
n = strlen (str);
}
for (i = 0; i < n; i + +)
{
s = &str [i];
for(j = 1; j < = strlen (str); j + +)
{
printf ("%c", *s);
s++;
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
void preconcatenate(char *i, char *s)
{
while (*i)
i ++;
while (*s)
*i ++ = *s ++;
* i ='\o' ;
}
Result:
Enter two strings:You
Me
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
if(*a > *b && *a > *c) }
printf("%d",*a); Result:
else if(*b > *a && *b > *c) How many elements?5
printf("%d",*b); Enter the elements:1
else 2
printf("%d",*c); 3
} 4
5
Average=
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
if(i>j)
C Programming Language …349 return (1);
}
14. Write a function that accepts two strings str1 and str2 as arguments Result:
and finds which of the two is alphabetically greater (without using Enter 1st string:ram
the library functions). The function should return 1 if str1 is greater Enter 2nd string:hari
than str2, 0 if str1 is equal to str2, and -1 is str1 is smaller than str2.
Ans: #include<stdio.h>
#include<conio.h>
int comp(char a[10],char b[10]);
void main()
{
char a[10],b[10];
int k;
clrscr();
printf("Enter 1st string:");
gets(a);
printf("Enter 2nd string:");
gets(b);
k=comp(a,b);
if(k==1)
printf("%s is greater than %s",a,b);
if(k==-1)
printf("%s is less than %s",a,b);
if(k==0)
printf("%s is equal to %s",a,b);
getch();
}
int comp(char *a,char *b)
{
int i=0,j=0,k;
while(a[i]!='\0'&& b[j]!='\0’)
{ if (a[i]<b[j])
return (-1);
else if(a[i]>b[j])
return (1);
else
i++;
j++;
}
if(i==j)
return (0);
if (i<j)
return (-1);
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
int is_palindrome(char *string)
350 … A Complete Refresh Manual of C Programming {
15. Write a program to enter a string and check whether the entered int check, length;
string is palindrome or not using library function. char *reverse;
length = string_length(string);
[Palindrome : A string is said to be palindrome if it remains same if
reverse = (char*)malloc(length+1);
we reverse it. e.g. ADA, MALAYALAM] copy_string(reverse, string);
Ans: #include <stdio.h> reverse_string(reverse);
int is_palindrome(char*); check = compare_string(string, reverse);
void copy_string(char*, char*); free(reverse);
void reverse_string(char*); if ( check == 0 )
int string_length(char*); return 1;
int compare_string(char*, char*); else
main() return 0;
{ }
char string[100]; int string_length(char *string)
int result; {
printf("Enter a string\n"); int length = 0;
gets(string); while(*string)
result = is_palindrome(string); {
if ( result == 1 ) length++;
printf("\"%s\" is a palindrome string.\n", string); string++;
else }
printf("\"%s\" is not a palindrome string.\n", return length;
string); }
return 0;
}
void copy_string(char *target, char *source)
Ram is greater than hari {
while(*source)
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Ans: #include<stdio.h>
#include<conio.h> 17. Write a program that reads roll, name and marks in 5 different
void arr(int *); subjects of N different students and prints them in appropriate
int i,j,n; format [Use structure, pointer & malloc ( ) function]
void main() Ans: #include<stdio.h>
{ #include<conio.h>
int *p,*q,a[10]; #include<stdlib.h>
p=&a[0]; struct student
q=p; {
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
for(i=0;i<n;i++)
{ total[i]=0;
printf("Enter the record of std[%d]:\n",i+1);
printf("\nEnter the roll:");
scanf("%d",&p->roll);
fflush(stdin);
printf("Enter the name:");
gets(p->name);
clrscr();
printf("\nRoll\tName\tSUB1\tSUB2\tSUB3\tSUB4\tSUB5\
tTotal\n");
printf(" \n");
for(i=0;i<n;i++)
{
//to print the roll and name of student
printf("%d\t%s\t",q->roll,q->name);
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Enter the no. of student:2 other=%d”,o); getch();
Enter the record of }
std[1]: Enter the void count(char str[],int *v,int *c,int *n,int *o)
roll:2 {
Enter the name:Mohan Int I;
Enter the marks in 5 subjects: For(i=0;str[i]!=0;++i)
45 65 75 85 95 {
Enter the record of
std[2]: Enter the
roll:9
Enter the name:Pintu
Enter the marks in 5 subjects:
54 56 76 87 90
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
{
C Programming Language …355 if(*(p+i)%2!=0)
if(toupper(sen[i])=='A' ||toupper(sen[i]) =='E' || {
toupper(sen[i])=='I' || toupper(sen[i]) a[c]=*(p+i);
=='O' || toupper(sen[i])=='U') c++;
*v= *v + 1; }
else }
return c;
if(toupper((sen[i])>=' A'||toupper(sen[i])<='Z')
}
*c=*c+1;
else if(str[i]>=’0’ && str[i]<=’9’)
*n=*n+1;
else
*o=*o+1;
}
}
19. Write a program that reads positive integers into an array
dynamically and pass them to a function where odd numbers are
filtered into another array and display the array containing odd
numbers in the main program. [T.U. 2065 Shrawan]
Ans: #include<stdio.h>
#include<conio.h>
void odd(int *,int[],int);
void main()
{
int *p,n,i,o;
printf(“Enter value of n:”):
scanf(“%d”,&n);
p=(int *)malloc(n*sizeof(int));
printf(“Enter element:”):
for(i=0;i<n;++i)
scanf(“%d”,(p+i));
o=odd(p,o,n);
printf(“Odd numbers are:”):
for(i=0;i<o;++i)
printf(“\n%d”,o[i]);
getch();
}
int odd( int *p,int a[],int n)
{
int I,c=0;
{
for(i=0;i<n;++i)
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
for(i=0;*(s1+i)!=’\0’;++i)
356 … A Complete Refresh Manual of C Programming
{
20. Write a program that dynamically allocates two different strings and if(*(s2+i)!=*(s1+i-1-k)
pass them to function where the second string is compared with the return 1;
reverse of the first string on the character basis and display the }
message in main function. You are not allowed to use the string return 0;
functions. [T.U. 2065 Shrawan] }
Ans: #include<stdio.h>
#include<conio.h>
int compare(char*,char *);
void main()
{
char *s1,*s2;
int l,n;
printf(“Enter maxmimum length of string:”);
scanf(“$d”,&l);
s1=(char*) malloc(l*sizeof(char));
s2=(char*) malloc(l*sizeof(char));
printf(“First string:”):
gets(s1);
printf(“Enter second string:”):
gets(s2);
n=compare(s1,s2);
if(n==0)
printf(“\n Second string and reverse of
first string is same”):
else
printf(“\nSecond string and reverse of
first string is not same”);
getch();
}
int compare(char *s1,char *s2)
{
int i,j,k;
for(i=0;*(s1+i)!=’\0’;++i)
for(j=0;*(s1+j)!=’\0’;++j)
if(i==j)
return 1;
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
C Programming Language …357 Language places no constructs on the file, and it may be read from, or
written to, in any manner chosen by the programmer.\
2.16 Data File
It organizes data into blocks containing contiguous bytes of
1. What is data file ? Explain its type. Also, Write down its operation information.It contsists sound,images,graphical files etc.A binary file
mode. is made up of machine-readable symbols that represent 1’s and 0’s.
B. Low Level
Or, What is a file? What is the difference between a text file and binary
file? Explain with an example. [T.U. 2064 Jestha]
Or, Differentiate between text versus binary mode of file.
[T.U. 2065 Shrawan]
Ans: It is a data structure which help us to store our data permanently on
the secondary storage media (i.e. hard disk, floppy disks etc.) and to
access and alter information whenever necessary.C programming has
different types of library functions for creating and managing data
files.Types of data files are:
A. High Level
Types of high level files are:
a. Text files
A text file can be a stream of characters that a computer can
process sequentially. It is not only processed sequentially but only in
forward direction. For this reason a text file is usually opened for only
one kind of operation (reading, writing, or appending) at any given
time.
A text file file is a human-readable sequence of characters and
words they form that can be encoded into computer-readable formats
such as ASCII.A text file is also known as ASCII file and can be read
by any word processing package.There is no sound or video files or
graphical files.
b. Binary files
A binary file is no different to a text file. It is a collection of
bytes. In C Programming Language a byte and a character are
equivalent. No special processing of the data occurs and each byte of
data is transferred to or from the disk unprocessed. C Programming
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
(ii) w : open file for writing
(iii) a : open file for appending
List of additional file modes are :
(i) r + : Existing file is opened to the beginning for both
reading and writing.
(ii) w+ : Same as w except both for reading and writing.
(iii) a+ : Same as a except both for reading & writing.
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
{ printf("Enter marks:");
printf("Enter roll number:"); scanf("%d",&marks[i]);
scanf("%d",&std_no[i]); fprintf(fp,"%d%s%d",std_no[i],name[i],
fflush(stdin); marks[i]);
printf("Enter name"); }
scanf("%s",name[i]);
{ rewind(fp);
ch=getchar(); printf("Roll number\tName\tMarks\n");
putc(ch,f1); printf("--------------------------\n");
}
fclose(f1); for(i=0;i<n;++i)
{
printf("\nData output\n"); fscanf(fp,"%d%s%d",&std_no[i],name[i],&marks[i]);
f1=fopen("input.txt","r"); printf("%d\t%s\t%d\n",std_no[i],name[i],
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
f1=fopen("upper.txt","r");
5. Write a program that reads 'n' integers for a file and reads every
integer from file to store in odd file (if odd) and even file (if even)
and finally prints all the file in console.
Ans: #include <stdio.h>
#include <conio.h>
void main ( )
{
FILE *fp, *fpo, *fpe;
int n, num, i;
fp = fopen ("MAIN.DAT","w");
printf ("How many numbers ?");
scanf ("%d", &n);
// Entering nos
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
EOF) break;
f1=fopen(name1,"r"); else
if(f1==NULL) fputc(ch,f2);
{ }
puts("Source file does not printf(“Files copied.”);
exist"); fclose(f1);
exit(0); fclose(f2);
} getch();
f2=fo }
pen(n
ame2, Result:
"w"); Enter source file name: main.dat
if(f2 Enter destination file name:record.dat
==NUL
L) Files copied.
{
puts("Does not open target file");
exit(0);
}
while(1)
{
c
h
=
f
g
e
t
c
(
f
1
)
;
i
f
(
c
h
=
=
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Ram:2………hari:3 82 84 86 86 90 92 94 96
98
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
{
C Programming Language …369 char ch;
char str[3][15]={"DVD","DDR2","Woofer"}; int c=0;
int qty[3]={20,180,67},i;
int rate[3]={100,1800,1500}; FILE *f1;
float total[3]; f1=fopen(“record1.dat”,”w”);
printf(“Enter a string:”);
FILE *f1; while(1)
f1=fopen("f:\\inventory.dat","w"); {
for(i=0;i<3;++i)
{
fscanf(f1,"%s%d%d",str[i],qty[i],rate[i]);
total[i]=qty[i]*rate[i];
fprintf(f1,"%s%d%d%f",str[i],qty[i],
rate[i],total[i]);
}
fclose(f1);
//Data output
f1=fopen("f:\\inventory.dat","r");
printf("Particulars\t\tQty\t\tRate
\t\tTotal\n");
for(i=0;i<3;++i)
{
fscanf(f1,"%s%d%d%f",str[i],qty[i],rate[i]
,total[i]);
printf("%s\t\t%d\t\t%d\t\t%.2f\n",str[i]
,qty[i],rate[i],total[i]);
}
fclose(f1);
getch();
}
Result:
DVD 20 100 2000.000000
DDR2 180 1800 324000.000000
Woofer 67 1500 100500.000000
10. Write a program that reads characters from keyboard and writes to a
data file. The program should display the numbers of words entered
and terminate when the user enters a dot. [T.U.2059]
Ans: #include<stdio.h>
#include<conio.h>
void main()
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
scanf(“%d”,&a.roll);
370 … A Complete Refresh Manual of C Programming fflush(stdin);
ch=getche(); printf("\nEnter name:");
if(ch==’.’) gets(a.name);
break; fflush(stdin);
else printf("\nEnter address:");
{ gets(a.add);
fputc(ch,f1); printf("Enter telephone number:");
if(ch==’ ‘) scanf("%ld",&a.tel);
++c;
}
}
printf(“Total number of words in the entered string
is %d”,c);
getch();
}
Result:
FILE *fp;
fp=fopen("record2.dat","wb");
do
{
printf(“\nEnter roll number:”)
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Ans: #include<stdio.h>
12. Write a program to open a binary file. Define a structure variable #include<conio.h>
struct student
that hold the name,address and telephone number of a student. Ask {
the user for the number of each student records that has to be filed. char name[30],add[20];
Read in a structure for each student and write the structure to the long int tel;
file. After having read all records, read in the content of the file and
display them. [T.U. 2064 Baisakh]
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
printf("Name\tAddress\tTel. Number\n"); }
while(fread(&a,sizeof(a),1,fp)==1)
{ 13. Write a program to open anew file. Read
printf("%s\t%s\t%ld\n",a.name, produc_code,product_name and price for 10 items and write them to
a.add,a.tel); the file. [T.U. 2060 Shrawan]
}
fclose(fp);
Ans: #include<stdio.h>
getch();
}a;
void main() #include<conio.h>
void main ( )
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
Enter name : C++ programming
C Programming Language …373 Enter price:350
{
char product_name[10][30]; Product Code Product Name Price
101 C programming
int product_code[10]; 300
………………..float price[10];
……………………….. ……………
int i;
FILE *fp;
fp = fopen("STUD.DAT", "wb");
for(i = 0;i<10; i++)
{
printf("\nEnter product code:");
scanf("%d",&product_code[i]);
printf ("\nEnter name : ");
scanf ("%s",&product_name[i]);
printf(“\nEnter price:”);
scanf ("%f",&price [i]);
fprintf(fp,"%d%s%f",product_code[i],
product_name[i],price[i]);
}
rewind(fp);
// Reading from file
printf("\nProduct Code\tProduct Name\t
Price\n");
for (i = 0; i<10; i++)
{
fscanf(fp,"%d%s%f",&product_code[i],
product_name[i],&price[i]);
printf("%d\t%s\t%f\n",product_code[i],
product_name[i],price[i]);
}
fclose (fp);
getch ( );
}
Result:
Enter product code:101
Enter name : C programming
Enter price:300
………………………………………….
……………………………………………..
…………………………………………..
Enter product code:121
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
exit(1);
374 … A Complete Refresh Manual of C Programming }
14. Write a program to open anew file, read name, roll for(i=0;i<50;++i)
number, and total score of 50 students from user and {
write to the file. After writing, display the contents of file.
do
[T.U. 2062 Poush] {
Ans: printf("\nEnter roll number:");
#include<stdio.h> scanf("%d",&a.roll);
#include<conio.h> fflush(stdin);
struct student printf("\nEnter name:");
{ gets(a.name);
char name[30]; for(i=0;i<5;++i)
int roll; {
float marks; printf("Enter marks:");
}a[50]; scanf("%d",&a.marks[i]);
void main ( ) }
{ fwrite(&a,sizeof(a),1,fp);
int
i;
15. Write a program that first appends the customer records in a binary
FILE file and display the number of customer records as well as the
*fp; contents from the file. The file name should be given by user and
display message if it does not exit. [T.U. 2065 Shrawan]
fp=fopen("score.dat","wb"); Ans: #include<stdio.h>
if(==NULL) #include<conio.h>
{ struct customer
printf(“File can not be opened:”): {
getch();
Char name[30];
121 C++ programming 350 void main ( )
{
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
printf("%d\n",a.marks[i]);
C Programming Language …375 }
16. Write a program to read name,rollno, and marks obtained by }
students in five subjects and store the result in a file until user is not fclose(fp);
satisfied. The program should have another portion to read all the getch();
}
stored information. Your program should ask the user to read from
or write to the file and call the specific functions.
[T.U. 2062 Baisakh]
Ans: #include<stdio.h>
#include<conio.h>
struct student
{
char name[30];
int roll;
int marks[5];
}a;
void main ( )
{
char ch[10];
int i; FILE *fp;
fp=fopen("record4.dat","wb");
do
{
printf("\nEnter roll number:");
scanf("%d",&a.roll);
fflush(stdin);
printf("\nEnter name:");
gets(a.name);
for(i=0;i<5;++i)
{
printf("Enter marks:");
scanf("%d",&a.marks[i]);
}
fwrite(&a,sizeof(a),1,fp);
printf("Do you want to add another record?");
scanf("%s",&ch);
}while(ch=='y'); fclose(fp);
fp=fopen("record4.dat","rb");
printf("Roll No.\tName\tAddress\tTelephone
Number\n");
while(fread(&a,sizeof(a),1,fp)==1)
{
printf("%d\t%s\t",a.roll,a.name);
for(i=0;i<5;++i)
{
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
{
376 … A Complete Refresh Manual of C Programming if(list[i].rank>list[j].rank)
Result: {
rtemp=list[i].rank;
strcpy(ntemp,list[i].name);
strcpy(ctemp,list[i].country);
list[i].rank=list[j].rank;
strcpy(list[i].name,list[j].name);
17. Write a program that reads name, rank and country of N different
players for a data file and prints all the information in descending
order on the basis of rank.
Ans: #include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 500
struct player
{
int rank;
char name[15],country[15];
}list[MAX];
void main()
{
FILE *fp;
int i,j,rtemp;
char ntemp[15],ctemp[15];
int n;
printf(“How many records?”);
scanf(“%d”,&n);
printf("ADD RECORDS\n\n");
fp=fopen("data.txt","w");
for(i=0;i<n;i++)
{
printf("enter the record of player[%d]\n",i+1);
fflush(stdin);
printf("Enter the Name:");
gets(list[i].name);
printf("Enter the rank:");
scanf("%d",&list[i].rank);
fflush(stdin);
printf("Enter the country:");
gets(list[i].country);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
C Programming Language …377 file. After taking the information it closes the file and displays the
information about the student whose student_id has been entered by
strcpy(list[i].country, the user.
list[j].country);
Ans: #include<stdio.h>
list[j].rank=rtemp; #include<conio.h>
#include<process.h>
strcpy(list[j].name,ntemp);
struct student
strcpy(list[j].country,ctemp);
}
}
}
fwrite(&list,sizeof(list),1,fp);
fclose(fp);
clrscr(); fp=fopen("data.txt","r");
printf("Rank\tName\tcountryress\n ");
while(fread(&list,sizeof(list),1,fp)==1)
{
for(i=0;i<n;i++)
printf("%d\t%s\t%s\n",list[i].rank,list[i].name,l
ist[i].country);
}
fclose(fp);
getch();
}
Result:
How many records?3
ADD RECORDS
enter the record of player[1]
Enter the Name:Ena
Enter the rank:3
Enter the country:Nepal
enter the record of player[2]
Enter the Name:Fagu
Enter the rank:30
Enter the country:India
enter the record of player[3]
Enter the Name:Snjiv
Enter the rank:13
Enter the country:Bhutan
Rank Name Countryees
3 Ena Nepal
13 Sanjiv Bhutan
30 Fagu India
18. Write a program that reads the following information from the
keyboard – student_id, student name and total marks and writer to a
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
{
378 … A Complete Refresh Manual of C Programming int ctr=0;
{ char ch = 'y';
int stu_id; fp=fopen("student.c","w");
char stu_name[30]; while(ch=='y')
int marks; {
}s[100],b; void ctr++;
menu(); void clrscr();
create(); void
display(); void
search(); void
heading(); void
disp();
int i=0;
FILE *fp,*ft;
void main()
{
int first_ch;
clrscr();
heading();
printf("\n\n\n\t\t\t\t MENU");
printf("\n\n\n\t\t1.Create a new database.");
printf("\n\n\t\t2.Continue with the existing
database.");
printf("\n\n\t\t3. Exit.");
printf("\n\n\n\t\tEnter your choice( 1 - 3): ");
scanf("%d",&first_ch);
switch(first_ch)
{
case 1:
create();
break;
case 2:
menu();
break;
case 3:
exit(0);
break;
default:
printf("\n\n\n\t\tYou have entered wrong
choice...!!!..The program will exit now.");
}
getch();
}
void create()
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
fp=fopen("student.c","r"); display();
rewind(fp); }
while(fread(&s,sizeof(s[i]),1,fp)==1) }
{ fcloseall();
ctr++; if(flag==0)
clrscr();
heading();
printf("\n\n\n\tFollowing are the details :-");
printf("\n\n\tRecord #%d",ctr);
printf("\n\n\t\tStudentid
:%d",s[i].stu_id);
printf("\n\n\t\t Name :%s",
s[i].stu_name);
printf("\n\n\t\tTotal Marks:%d",
s[i].marks);
printf("\n\n\n\n\t\tPlease Press
Enter...");
getch();
}
menu();
}
void search()
{
int flag; char
ch='y';
while(ch=='y')
{
clrscr();
flag=0;
heading();
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
clrscr();
fp=fopen("data.txt","r");
printf("Rank\tName\tcountryres
s\n ");
while(fread(&list,sizeof(list)
,1,fp)==1)
{
for(i=0;i<n;i++)
printf("%d\t%s\t%s\n",list[i].rank,lis
t[i].name
,list[i].country);
}
f
c
l
o
s
e
(
f
p
)
;
g
e
t
c
h
(
)
;
}
Result:
How many records?3
ADD RECORDS
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
21. Write a program that ask the user to input Name, Age of N different
film actors for a file and prints them in appropriate format. [Use
fwrite( ) and fread( ) functions]
Ans: #include <stdio.h.>
#include <conio.h>
struct actor
{
int age;
char name [25];
}rec;
void main ( )
{
FILE *fp;
int i, N;
fp = fopen ("dec.dat", "w+");
printf ("Enter how many nos:");
scanf ("% d", &N);
// Entering data
for (i = 0; i < N; i++)
{
fflush(stdin);
printf ("Enter name : \n");
gets (rec.name);
printf ("Enter age : \n");
scanf ("%d", &rec.age);
fwrite (&rec, size of (rec), 1, fp);
}
rewind (fp);
// Displaying records
printf ("Name \t Age \n");
printf(" ......................\n");
while (fread (&rec, sizeof (rec), 1,
fp) = =1)
printf ("%s \t %d \n", rec.name,
rec.age);
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
printf("\nBoth files are not equal"); putc(h,fp);
getch(); switch(h)
} {
23. Write a program to create a data file and count total number of case 'a':
vowel. case 'e':
Ans: #include<stdio.h> case 'i':
#include<Conio.h> case 'o':
void main() case 'u':
{ v++;
FILE *fp; }
char h; int v=0; clrscr(); }
fp=fopen("text.dat","w"); printf("\nTotal vowels are %d",v);
printf("\nInput text to write into file \n"); fclose(fp);
while((h=getc(stdin))!=EOF) getch();
{ }
}
l1=0; Result:
l2=0; Input text to write into file
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
{
C Programming Language …387 printf ("\nEnter Employee
We are studying computer engineering.^Z Code:-> ");
flushall();
Total vowels are 13 scanf ("%d",&emp.code);
printf ("\nEnter
Employee Name:-> ");
24. Write a complete C program for reading an employees file flushall();
containing {emp_number, name, salary, address}. Create an output gets(emp.name);
file containing the names of those employees along with their salary
and address whose salary is > 20,000.
Ans: #include<stdio.h>
#include<conio.h>
#include<process.h>
struct employee
{
int code;
char name[30];
char address[50];
char phone[10];
float sal;
}emp,tmp;
FILE *fp1,*fp2;
void main()
{
char ch='y';
int i=1;
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
fclose (fp2);
388 … A Complete Refresh Manual of C Programming fp2=fopen("output.txt","r");
printf ("\nEnter Employee i=1;
Address:-> "); while (1)
flushall(); {
gets(emp.address); i=fread(&emp,sizeof(emp),1,fp2);
printf ("\nEnter Employee if(i==0)break;
Phone Number:-> ");
flushall();
gets(emp.phone);
printf ("\nEnter Employee
Salary:-> ");
flushall();
scanf("%f",&emp.sal);
/*Writing records to
Employee.txt file*/
fwrite(&emp,sizeof(emp),1,fp1);
printf ("\nDo u want to add more records (y/n):->”);
flushall();
scanf("%c",&ch);
if (ch=='n' || ch=='N')break;
}
fclose(fp1);
fp1=fopen("employee.txt","r");
}
printf ("\nList of Employees having Salary
greater than 20000\n");
printf ("\nCode\tName\t\tAddress\tPhone
No.\tSalary\n"); i=1;
fp2=fopen("Output.txt","w");
getch();
if (fp2==NULL)
{
printf ("\nUnable to create Output
file\n");
getch();
exit(0);
}
while (1)
{
i=fread(&emp,sizeof(emp),1,fp1);
if(i==0)break;
if (emp.sal>20000)
fwrite(&emp,sizeof(emp),1,fp2);
}
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
{
C Programming Language …389 FILE *fp,*fpt;
printf("\n%d\t%s\t\t%s\t%s\t%f" struct employee list,temp;
,emp.code,emp.name,emp.address,emp.ph int ch,emco;
one,emp.sal); while(1)
} {
getch(); printf("\tMAIN MENU\n");
} printf("1.Add record\n");
Result:
Add new records
Enter Employee code:-> 1
Enter Employee Name:-> Rabin
Enter Employee Address:-> Baneshwor
Enter Employee Phone Number:-> 44354
Enter Employee Salary:-> 21000
Do u want to add more records (y/n):->n
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
post,list.salary);
390 … A Complete Refresh Manual of C Programming }
printf("2.Display record\n"); if(emco==0)
printf("3.Search record\n"); printf("File not found");
printf("4.Delete record\n"); fclose(fp);
printf("5.Update record\n"); }
printf("6.Exit\n");
printf("\n\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
fp=fopen("employee.dat","a");
printf("Enter ecode:\n");
scanf("%d",&list.ecode);
fflush(stdin);
printf("Enter Name:\n");
scanf("%s",&list.name);
printf("Enter post:\n");
scanf("%s",&list.post);
printf("Enter salary:\n");
scanf("%f",&list.salary);
fwrite(&list,sizeof(list),1,fp);
fclose(fp);
}
break;
case 2:
{
fp=fopen("employee.dat","r");
printf(“Ecode\tName\Post\Salary\n”);
printf(“…………………………………………\n”);
while(fread(&list,sizeof(list),1,fp)==1)
printf("%d\t%s\t%s\t%f\n",list.ecode,list.name,list.
post,list.salary);
fclose(fp);
}
break;
case 3:
{ fp=fopen("employee.dat","r");
printf("Enter employee code:");
scanf("%d",&emco);
while(fread(&list,sizeof(list),1,fp)==1)
{ if(emco==list.ecode)
printf("%d\t%s\t%s\t%f\n",list.ecode,list.name,list.
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
5. Update record
6. Exit
Enter your choice:1
Enter ecode:101
Enter name:Gauri
Enter post:Peon Enter
salary:4500
MAIN MENU
1. Add record
2. Display record
3. Search record
4. Delete record
5. Update record
6. Exit
Enter your choice:2
Ecode Name Post Salary
101 Gauri Peon 4500
MAIN MENU
1. Add record
2. Display record
3. Search record
4. Delete record
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
graphics driver from disk (or validating a registered driver) then
putting the system into graphics mode. initgraph also resets all
graphics settings (color, palette, current position, viewport, etc.) to
heir defaults, then resets graphresult to 0. Arguments:
*graphdriver: Integer that specifies the graphics driver to be used.
You can give graphdriver a value using a constant of the graphics
drivers enumeration type.
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
ellipse(500, 100,0,360, 100,50);
C Programming Language …395 outtextxy(480, 170, "Ellipse");
*graphmode : Integer that specifies the initial graphics mode (unless line(100,250,540,250);
*graphdriver = DETECT). If *graphdriver = DETECT, initgraph sets outtextxy(300,260,"Line");
*graphmode to the highest resolution available for the detected driver.
sector(150, 400, 30, 300, 100,50);
You can give *graphmode a value using a constant of the outtextxy(120, 460, "Sector");
graphics_modes enumeration type.
pathtodriver : Specifies the directory path where initgraph looks for
graphics drivers (*.BGI) first. If they're not there, initgraph looks in
the current directory. If pathtodriver is null, the driver files must be in
the current directory. This is also the path settextstyle searches for the
stroked character font files (*.CHR).
closegraph() function switches back the screen from graphcs mode to
text mode. It clears the screen also. A graphics program should have a
closegraph function at the end of graphics. Otherwise DOS screen will
not go to text mode after running the program. Here, closegraph() is
called after getch() since screen should not clear until user hits a key.
If you have the BGI file in the same folder of your program, you can
just leave it as "" only. you need not mention *graphmode if you give
*graphdriver as DETECT.
In graphics mode, all the screen co-ordinates are mentioned in terms of
pixels. Number of pixels in the screen decides resolution of the screen.
In the example 1.0, circle is drawn with x-coordinate of the center
200, y-coordinate 100 and radius 150 pixels. All the coordinates are
mentioned with respect to top-left corner of the screen.
Basic Shapes and Colors:
Now let us write a program to draw some basic
shapes.
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT, gm;
int poly[12]=
{350,450,350,410,430,400,350,350,300,
430,350,450};
initgraph(&gd, &gm, "");
circle(100,100,50);
outtextxy(75,170, "Circle");
rectangle(200,50,350,150);
outtextxy(240, 170, "Rectangle");
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
396 … A Complete Refresh Manual of C place where last drawing is ended. These functions are declared as
Programming follows:
void far outtextxy(int x, int y, char *text);
drawpoly(6, poly); void far outtext(char *text);
outtextxy(340, 460, "Polygon");
Circle, arc, pieslice are declared as follows:
getch(); Declaration:
closegraph(); • void far arc(int x, int y, int stangle, int endangle, int radius);
} • void far circle(int x, int y, int radius);
Here is the screenshot of output: • void far pieslice(int x, int y, int stangle, int endangle, int radius);
Remarks:
• arc draws a circular arc in the current drawing color.
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
C Programming Language …397 setfillstyle, if we use functions like floodfill, fillpoly, bar etc, shpes will
be filled with fill color and pattern set using setfillstyle
• circle draws a circle in the current drawing color. These function declarations are as follows.
• pieslice draws a pie slice in the current drawing color, then fills it Declaration:
usingthe current fill pattern and fill color. • void far setfillstyle(int pattern, int color);
Arguments: • void far setcolor(int color);
• (x,y): Center point of arc, circlew, or pie slice • void far setbkcolor(int color);
• stangle: Start angle in degrees
• endangle: End angle in degrees r radius: Radius of arc, circle, and
pieslice adius: Radius ofarc, circle, and pieslice
Here, stangle and endangle are in degrees starting from the +ve
x-axis in the polar coordinate system in the anti-clockwise direction. if
stangle is 0, endangle is 360, it will draw a full circle. Refer this figure
for clear idea: For the details of current color, fill color and fill
patterns, refer the sections Lines and Colors.
Here is some idea about colors.
There are 16 colors declared in graphics has listed bellow:
BLACK: 0
BLUE: 1
GREEN: 2
CYAN: 3
RED: 4
MAGENTA: 5
BROWN: 6
LIGHTGRAY: 7
DARKGRAY: 8
LIGHTBLUE: 9
LIGHTGREEN: 10
LIGHTCYAN: 11
LIGHTRED: 12
LIGHTMAGENTA: 13
YELLOW: 14
WHITE: 15
To use these colors, use functions setcolor(), setbkcolor() and
setfillstyle(). setcolor() function sets the current drawing color. If we
use setcolor(RED); and draw any shape, line or text after that, the
drawing will be in red color. You can either use color as defined
above or number like setcolor(4);. setbkcolor() sets background color
for drawing. Setfillstyle sets fill pattern and fill colors. After calling
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
{
398 … A Complete Refresh Manual of C Programming int gd,gm;
Remarks: gd=DETECT;
• setfillstyle sets the current fill pattern and fill color. initgraph(&gd, &gm, "");
• setcolor sets the current drawing color to color, which setcolor(3);
can range from 0 to getmaxcolor. setfillstyle(SOLID_FILL,RED);
• setbkcolor sets the background to the color specified bar(50, 50, 590, 430);
by color. The parameter pattern in setfillstyle is as follows:
setfillstyle(1, 14);
Names Value Means Fill bar(100, 100, 540, 380);
With... EMPTY_FILL 0 while(!kbhit())
Background color SOLID_FILL {
1 Solid fill LINE_FILL 2
--- LTSLASH_FILL 3 ///
SLASH_FILL 4 ///,
thick lines BKSLASH_FILL
5 \\\, thick lines LTBKSLASH_FILL
6 \\\ HATCH_FILL 7
Light hatch
XHATCH_FILL 8 Heavy
crosshatch INTERLEAVE_FILL 9
Interleaving lines WIDE_DOT_FILL 10
Widely spaced dots CLOSE_DOT_FILL
11 Closely spaced dots USER_FILL
12 User-defined fill pattern
Here is an example program with colors, pixels,
bar, cleardevice etc. stdlib.h is used for random number
generation. We have a function random(no), it returns a random
number between 0 an no. The effect is by drawing random
radius, random color circles with same center and random
pixels. kbhit() function(defined in conio.h) returns a nonzero
value when a key is pressed in the keyboard. So, the loop will
continue until a key is pressed.
#include "graphics.h"
#include "conio.h"
#include
"stdlib.h"
void main()
F -X C h a n ge F -X C h a n ge
PD PD
!
W
W
O
O
N
N
y
y
bu
bu
to
to
ww
ww
om
om
k
k
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
putpixel(random(439)+101,random(279)+101,random(16))
setcolor(random(16));
circle(320,240,random(100));
} getch();
closegraph();
}
Here is a sample program to illustrate how to use BARS which are
used for visual statistics
#include<graphics.h>
#include<conio.h>
main()
{
int gd=DETECT,gm,maxx,maxy,x,y,button;
initgraph(&gd,&gm,"");
line(80,150,200,150);
line(80,150,80,50);
settextstyle(1,HORIZ_DIR,1);
outtextxy(100,153,"<-X axis");
settextstyle(1,VERT_DIR,1);
outtextxy(60,50,"<-Y axis");
bar(100,100,120,150);
bar(130,120,150,150);
getch();
closegraph();
}
Good Luck