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

1638699417_Computer-Programming-Lab (2)

The document is a lab manual for the Computer Programming Lab at Gudlavalleru Engineering College, detailing the vision and mission of the institution and the Computer Science and Engineering department. It outlines the objectives, learning outcomes, program educational objectives, and program outcomes related to computer programming. Additionally, it includes guidelines for students, required equipment, and procedures for conducting various programming experiments using C language and MS Office applications.

Uploaded by

m.anand1803
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

1638699417_Computer-Programming-Lab (2)

The document is a lab manual for the Computer Programming Lab at Gudlavalleru Engineering College, detailing the vision and mission of the institution and the Computer Science and Engineering department. It outlines the objectives, learning outcomes, program educational objectives, and program outcomes related to computer programming. Additionally, it includes guidelines for students, required equipment, and procedures for conducting various programming experiments using C language and MS Office applications.

Uploaded by

m.anand1803
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 74

GUDLAVALLERU ENGINEERING COLLEGE

(An Autonomous Institute with Permanent Affiliation to JNTUK, Kakinada)


SeshadriRao Knowledge Village, Krishna District, AndhraPradesh,
GUDLAVALLERU - 521 356.

Department of Computer Science and Engineering

LAB MANUAL

ON

COMPUTER PROGRAMMING LAB

I B.Tech I Sem – R17

Department of Computer Science and Engineering GEC Page 1


GUDLAVALLERU ENGINEERING COLLEGE
(An Autonomous Institution with Permanent Affiliation to JNTUK, Kakinada)
Seshadri Rao Knowledge Village, Gudlavalleru – 521356
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING
INSTITUTE VISION & MISSION
Institute Vision:
To be a leading institution of engineering education and research, preparing students for
leadership in their fields in a caring and challenging learning environment.

Institute Mission:
 To produce quality engineers by providing state-of-the-art engineering education.
 To attract and retain knowledgeable, creative, motivated and highly skilled individuals whose
leadership and contributions uphold the college tenets of education, creativity, research and
responsible public service.
 To develop faculty and resources to impart and disseminate knowledge and information to
students and also to society that will enhance educational level, which in turn, will contribute
to social and economic betterment of society.
 To provide an environment that values and encourages knowledge acquisition and academic
freedom, making this a preferred institution for knowledge seekers.
 To provide quality assurance.
 To partner and collaborate with industry, government, and R&D institutes to develop new
knowledge and sustainable technologies and serve as an engine for facilitating the nation’s
economic development.
 To impart personality development skills to students that will help them to succeed and lead.
 To instil in students the attitude, values and vision that will prepare them to lead lives of
personal integrity and civic responsibility.
 To promote a campus environment that welcomes and makes students of all races, cultures
and civilizations feel at home.
 Putting students face to face with industrial, governmental and societal challenges.

Department of Computer Science and Engineering GEC Page 2


DEPARTMENT VISION & MISSION

VISION
To be a Centre of Excellence in computer science and engineering education and training to meet
the challenging needs of the industry and society
MISSION
 To impart quality education through well-designed curriculum in tune with the growing software
needs of the industry.

 To be a Centre of Excellence in computer science and engineering education and training to


meet the challenging needs of the industry and society.

 To serve our students by inculcating in them problem solving, leadership, teamwork skills and
the value of commitment to quality, ethical behavior & respect for others.

 To foster industry-academia relationship for mutual benefit and growth.

PROGRAMME EDUCATIONAL OBJECTIVES(PEOs):-


PEO1: Identify, analyze, formulate and solve Computer Science and Engineering problems
both independently and in a team environment by using the appropriate modern tools.

PEO2: Manage software projects with significant technical, legal, ethical, social, environmental and
economic considerations.

PEO3: Demonstrate commitment and progress in lifelong learning, professional development,


leadership and Communicate effectively with professional clients and the public.

Department of Computer Science and Engineering GEC Page 3


PROGRAM OUTCOMES (POs)
Engineering students will be able to:
1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering fundamentals,
and an engineering specialization to the solution of complex engineering problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics, natural sciences,
and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems and design
system components or processes that meet the specified needs with appropriate consideration for the
public health and safety, and the cultural, societal, and environmental considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of the
information to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities with
an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
professional engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering solutions
in societal and environmental contexts, and demonstrate the knowledge of, and need for sustainable
development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of
the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write effective reports
and design documentation, make effective presentations, and give and receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the engineering
and management principles and apply these to one’s own work, as a member and leader in a team, to
manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.

PROGRAM SPECIFIC OUTCOMES


Students will be able to
13.

PSO1: Design, develop, test and maintain reliable software systems and intelligent systems.
PSO2: Design and develop web sites, web apps and mobile apps.

Department of Computer Science and Engineering GEC Page 4


Mapping of Course Outcomes with Program Outcomes

Department of Computer Science and Engineering GEC Page 5


Program No. Document Name Page No
Procedural Manual For Conducting Experiments In
Problem Solving Using C Lab
Program 1 Creating a document using MS Word. 20
Familiarizing with the usage and applications of MS Excel
Program 2 Using Excel 22

Program 3 Creating a presentation using MS PowerPoint. 26


Write a C program to calculate the area of triangle using the
Program 4 29
formula area = ( − )( − )( − ),where s= (a+b+c)/2.
Write a C program to find the largest of three numbers using
Program 5 31
ternary operator.
Implement a C program to find the roots of a quadratic
Program 6 equation. 33
Implement a C program to read two integer operands and one
operator form the user, perform theoperation and then print the
Program 7 36
result. (Consider the operators +,-,*, /, % anduse Switch
Statement)
Program 8 Implement a C program to display first N natural numbers. 39
Implement a C program to check whether given number is
Program 9 Prime (or) not. 41

Implement a C program to search whether the given element is


Program 10 in the array. 44
Implement a C program to perform Addition and multiplication
Program 11 of two Matrices. 47

Implement a C program to find the factorial of a given integer


Program 12 55
using recursive function.
Implement a C program to function to exchange (Swap) values
Program 13 58
of two integers using call by reference.
Implement a C program to illustrate string handling functions-
Program 14 61
strlen(), strcmp(), strcat(), strcpy(),strrev()
Write a C Program to implement a structure to read and display
Program 15 65
the Name, date of Birth and salary of n Employees.
Implement a C program to count the number of lines, words and
Program 16 70
characters in a file.

Department of Computer Science and Engineering GEC Page 6


EQUIPMENT REQUIRED FOR CONDUCTING THE LAB
EXPERIMENTS

HARDWARE AND SOFTWARE REQUIRED:


1. A working computer system with either Windows XP or Windows 2003.
2. Every student should be provided with a separate Cabinet for assembling and dissembling the
components of a Cabinet.
3. Windows XP Installation Software.
4. Pentium IV Processor and Motherboard will all its components,
5. 1GB RAM
6. 250 GB Hard Disk
7. CD ROM,CD Writer and Floppy Disk Drives
8. Keyboard & Mouse
9. Microsoft Office Suite 2007 (Including PowerPoint, Word, Excel)

Department of Computer Science and Engineering GEC Page 7


PROBLEM SOLVING USING C LAB OBJECTIVE:

 To emphasize the use of flowcharts and pseudo code in problem solving.


 To gain knowledge in C language
 To develop C Programs to solve problems.
 To familiarize with the discrete components of a computer, MS Office
LEARNING OUTCOMES:
 design flowcharts and pseudo code for solving problems.
 understand C tokens and control statements.
 gain knowledge on arrays, strings, pointers, functions, structures and files.
 use C language for solving problems
 self-learn advanced features of C.
 prepare applications using MS-Office.

Department of Computer Science and Engineering GEC Page 8


REFERENCES

1. Introduction to Information Technology, ITL Education Solutions limited, Pearson Education.


2. Introduction to Computers, Peter Norton, 6/e McGraw Hill
3. Comdex Information Technology course tool kit Vikas Gupta, WILEY Dreamtech
4. IT Essentials PC Hardware and Software Companion Guide Third Edition by David Anfinson and
KenQuamme. – CISCO Press, Pearson Education.
5. PC Hardware and A+Handbook – Kate J. Chase PHI (Microsoft)
6. C Programming,E Balaguruswamy, 3 rd edition, TMH.
7. C Programming, A Problem Solving Approach, Forouzan, Gilberg, Prasad, CENGAGE.
8. Programming in C, Second Edition PradipDey and ManasGhosh, OXFORD Higher Education

Department of Computer Science and Engineering GEC Page 9


GUIDELINES TO STUDENTS
 Equipment in the lab for the use of student community. Students need to maintain a proper
decorum in the computer lab.

 Students must use the equipment including PCs, Keyboard and Mouse with care. Any damage
is caused is punishable.

 Students are required to carry their observation and record books with completed exercises
while entering the lab.

 Students are supposed to occupy the machines allotted to them and are not supposed to talk or
make noise in the lab.

 Lab can be used in free time / lunch hours by the students. Students who need to use the
systems should take prior permission from the lab in-charge.

 Lab records need to be submitted on or before date of submission.

 Students are not supposed to use floppy disks/CDs/Pen drivesetc during the Lab session.

 Use of computer network is not encouraged.

Department of Computer Science and Engineering GEC Page 10


 INSTALLATION OF MICROSOFT WINDOWS:

1. Keep on press the delete button and go to advanced BIOS feature.


[ BIOS- Basic Input Output System ]
2. And go to boot sequence. Select first boot drivers. CD ROM and press F10 to save the bios
feature. Yes and then enter. Press any key to boot from CD. Press enter to setup windows XP.
3. Insert the Windows XP CD-ROM into your CD-ROM drive.

4. Press F8 to accept the LicenceAggrement

Department of Computer Science and Engineering GEC Page 11


5. The screen will now list all existing partitions and un-partitioned space for each hard disk. Use the
cursor/arrow keys to choose where to create the new partition.
6. Press D to delete an existing partition or
7. Press C to create a new partition in un-partitioned space.
8. If you press D to delete an existing partition, you must then press L to confirm that you want to
delete that partition.

9. Repeat this process for each of the existing partitions that you want to move (or remove) in order
to create your new partition.
10. When all necessary partitions are deleted, you can then select the remaining un-partitioned space
and then press C to create new partitions.

Department of Computer Science and Engineering GEC Page 12


11. Type the size in megabytes (MB) that you want the new partition to be. Then press ENTER.
Alternatively, you can just press ENTER to create the partition using all the available space (i.e.
create the biggest possible partition).
12. If you want to install Windows XP, use the arrow keys to select the partition where you want to
install Windows XP and then press ENTER.
13. To format the partition, use the arrow keys to select the partition where you want to install
Windows XP.
14. Select the NTFS formatting option and press ENTER

15. Setup will start formatting the disk and will then start copying files from the CD-ROM to the disk

Department of Computer Science and Engineering GEC Page 13


16. The system will then need to reboot

Department of Computer Science and Engineering GEC Page 14


17. After the restart, Windows XP starts the first time with the Graphical User Interface

18. Setup will continue to copy files and to install devices

Department of Computer Science and Engineering GEC Page 15


19. Personalize your system by setting regional and language settings, entering your name
and organization , Type your product key, define name and password for the administrator
account, set up date and time

Department of Computer Science and Engineering GEC Page 16


Department of Computer Science and Engineering GEC Page 17
Department of Computer Science and Engineering GEC Page 18
20. Setup continues with "Finalizing installation "

21. The system will reboot again :

This completes installing Windows XP

Department of Computer Science and Engineering GEC Page 19


Program 1: Creating a document using MS Word

Aim: Write a leave letter to the Principal by using different alignments, correct formats in MS-
Word.
Description:
Microsoft word is a powerful text editor which supports different font types, styles, image
operations etc. It is widely used for detailed documentation in various sectors like educational
institutes, business documentation, academic projects etc.
Procedure:
Step 1: Open MS-Word by click on START button; go to All Programs, then select Microsoft
Office Word2007.
Step 2: To open a new document, Click on Office Button then select New - > Blank
Documentthen click on create option.
Step 3: Then select TEXT AREA, and then write Leave Letter as a heading, Select the text, click on
bold

button to make it bold as “LEAVE LETTER”, and change the font


size to 16. Step 4: Then write date and place in a format as follows
DATE:
15/09/201
4, Guntur.

Then Select the text and make it right by clicking on right alignment button
Step 5: Then write To address as follows and select this text and make it left by clicking on left
alignment

The Principal,
College of Engineering,
Guntur.
Step 6: Then write Subject according to your letter. And select this text and press tab button for two
times. Step 7: Then write the body of the letter according to your letter. And select this text and make it
justification

by clicking on justify alignment button


Step 8: Then write “Thanking you Sir,” select this text and make it to center by clicking on center
alignment

button
Step 9: Now write the “From address”
as followsYours Faithfully, xxxxx.

Then make it right by clicking on Right alignment button


Step 10: This is the final step in writing leave letter. In this step, we have to save the letter as
“leaveletter.doc” by selecting “Save” option from Office button. Then a prompt window will ask
you to write a file name. Now you have to give the file name and press the save button.

D:15/9/2018,

Department of Computer Science and Engineering GEC Page 20


OUTPUT:
LEAVE LETTER
Guntur.
To
The Principal,
College of Engineering,
Guntur.

Sub:Requesting for 5 days leave-Reg

Respected Sir,

I, XXXXX studying B.Tech I year in CSE department in your


college. As I am going to my home on the occasion of devali festival and also to
celebrate my birthday on the next day.So I kindly request you to grant me leave for
5 days i.e.,16/9/2014-20/9/2014.

Thanking You Sir,

Yours Faithfully,
XXXXX,
B.Tech I year,
CSE Branch.

Viva Questions:

1. What is the importance of MS Word?


2. How do you insert the Header and Footer in a word document?
3. How do you set the Font?
4. What is the difference between MS Word and LaTex word document?

Department of Computer Science and Engineering GEC Page 21


Program 2: Familiarizing with the usage and applications of MS Excel using Excel

Description: Microsoft Excel is a spreadsheet program. It features an intuitive interface,


calculation and graphing tools. These tools could be used for business financial analysis
and other administrative tasks.
Open MS-Excel by any of the following ways

Method1: Method2:
1. Click on start button 1. Click on Start button
2. Click on programs 2. click run
3. Click on Ms-office 3. type the Excel
4. Click on Excel 4. After click ok button

Work Book: Collection of sheets is nothing but work book.


Work sheet: Collection of cells [1048576 x (XFD)16384 ]
Cells: Combination of rows & columns
Row: Horizontal line [1, 2,3,……]
Column: Vertical line [A, B, C, ……………..]
Cell address: Column name followed by Row name
Ex :( A1, B10, H2, C3…)

New Work Book: (ctrl+N)


1. Click on file menu
2. click on new [It will appears
dialog box]
3. Click on blank work book
4. Default file
name(bok1,book2,
….)
5. Extension Excel file .XLS

Close the Work book: (ctrl+w)


1. Click on the file menu
2. click on close

Save the work Book(Ctrl+s):


1. Click on file menu
2. Click on Save
3. Type the file name
4. Click on save button(Ctrl+l)

Department of Computer Science and Engineering GEC Page 22


Default setting Insert the Rename the
1. Sheets -- 3 work Sheet: work sheet:
2. Font - Arial
3. Font size - 10 1. Click on the 1. Click on
4. Row Hight- 12.75 insert menu format menu
5. Column Width- 8.43 2. Click in work 2. Click on the
6. Font Style regular sheet sheet
7. Font Color- Black Delete the 3. Click on the
8. Text alignment- Left work Sheet: rename
9. Numbering alignment =- Right alignment 1. Click on the 4. type the sheet
edit menu name
2. Click on delete 5. After press
Navigation in spread Sheet
To move the cell pointer to the left row Side -- (left arrow)
To move the cell pointer to the up row Side - (up arrow)
To move the cell pointer to the down t row Side -- (down arrow)
To move the cell pointer to the right row Side (right arrow)
To move the cell pointer to the first column in same row (“HOME”)
To move the cell pointer to the last column in last row (“CTRl+RIGHT ARROW”))
To move the cell pointer to the first column in same row CTRl+LEFT ARROW”)) To
move the cell pointer to the same row column same row (CTRl+LEFT ARROW”)

Department of Computer Science and Engineering GEC Page 23


Aim: Create a marks sheet for 10 students using EXCEL.
Steps:

1 Open MS-EXCEL by clicking on START Button, go to Programs then click on


Microsoft Office then select Microsoft Office Excel 2007.
2 Write the title name is College of Engineering at (A1 address)
3 Write the sub title name is I.B.Tech CSE MARKS SHEET at (A2 address).
4 Now write the requirements fields for calculation of marks, like
S.No,H.T.No,subjects,Grand Total & Percentage from A3 to O3 cells as follows
E E CP PHY/CHE PC
S.No H.T.No ENG PHY CHE M-1 MM PSUC ED LAB LAB LAB ITWS G.TOT %

5 Write S.no’s from 1-10 in A4 - A13 cell addresses and H.T.No’s from B4- B13.
6 Now enter the marks in C4 – M13 cell addresses.
7 Now in the G.Totalcell,select N4 cell and apply the formula for adding the marks
contained in C4 – M4 cell addresses in the formula bar as
fx= C4+D4+E4+F4+G4+H4+I4+J4+K4+L4+M4 then press ENTER key.
8 Select the N4 cell and drag towards down for further students calculations.
9 Now calculate the Percentage.For that select O4 cell and type % calculation
formula in Formula bar as
fx=N4/1000*100
10 Select the O4 cell and drag towards down for further students results.
11 Now the % appears in decimal values,now in order to appear the values as rounded
values,select the O4 cell then right click the mouse button , select Format cells then
Numbers and now make decimal places to 0(zero) then click OK.Now again drag
for other cell values to be rounded.
12 Now for keeping the borders,select cells from A1 – O13 and select All Borders on
the home Menu.
13 Now for saving click on OFFICE button and click on Save and type file name as
“Marks Sheet”,then click OK.

Department of Computer Science and Engineering GEC Page 24


OUTPUT:

Viva Questions:

1. What is the importance of MS Excel?


2. How do you insert Formulae in an Excel sheet?
3. How do you filter different text? What are the custom options available in filtering
technique?

Department of Computer Science and Engineering GEC Page 25


Program 3: Creating a presentation using MSPowerpoint

Description: Microsoft PowerPoint is a presentation program. It features an intuitive


interface with a wide variety of presentation themes, styles and types. These tool could be
used for paper presentations, conference papers, business seminars etc.

Open Microsoft PowerPoint 2007:


Click on Start button, goto All programs, click on Microsoft Office,select
Microsoft Office Power Point2007.
Creating a New Presentation:
After opening MS PowerPoint 2007, select “Blank Presentation”and click on
Create.

Saving your Presentation:


To save your document, simply click on the MS 2007 logo in the top left‐hand corner

and the menu bar that you see on your right here will drop down giving you various
options, including saving.
When you save a workbook in MS Excel 2007, it automatically saves with “.pptx”as its
extension.
However, you can also save your presentation so that it’s readable by earlier versions of
MS PowerPoint (97 – 2003) by simply selecting “PowerPoint 97‐2003 Presentation”when
choosing what file type to save as (please see the screenshot below).

Inserting a Slide:
To insert a new slide…
–From the Hometab, select “New Slide”
‐As well, you can right‐click between any 2 slides in the preview frame located on the
left‐hand side of the Normal view and select “New Slide”
Deleting a Slide:
To delete a slide…
–Select the slide you’d like to delete (eg. by highlighting it) and selecting “Delete
“from the Hometab –As well, you can right‐click on any slide in the preview frame
located on the left‐hand side of the Normal view and select “Delete Slide”
Inserting Pictures:
To insert pictures…
–From the Inserttab you can insert pictures from your computer, clip art, shapes, etc…
–Also, when you have a blank slide (or parts of a blank slide), you can also click on
options within these blank compartments to insert pictures.

Inserting Charts:
Department of Computer Science and Engineering GEC Page 26
To insert charts…
–From the Inserttab you can insert pictures from your computer, clip art, shapes, etc…
–Also, when you have a blank slide (or parts of a blank slide), you can also click on options
within these blank compartments to insert charts
•Once you’ve selected to insert a chart, it will ask you choose what kind of chart you’d like
to insert.
•After you’ve picked a type, the screen will split in half with the chart on one side and an
excel spreadsheet on the other
–To modify the chart, make changes in the data presented in the spreadsheet
– Charts from Excel can also be copied and pasted into PowerPoint
Inserting Tables:
To insert a table…
–From the Insert tab, select “Table “and a box will drop down giving you more options
for inserting a table. –Also, when you have a blank slide (or parts of a blank slide), you
can also click on options w/n these blank compartments to insert tables

•Normal viewis the default view


•Slide Sorter displays all the slides in consecutive order as thumbnails.
•Notes Page is similar to the normal view except the slides are reduced in size
and located in the upper portion of the screen with the lower portion of the
screen being allocated for adding notes to the slide.
•Slide show is the presentation where the slides are enlarged to fit the entire screen.
Animations
•All sorts Animation Effects can be added to your slides and the best way to find
out what works best for your presentation is to play around and experiment with
all the different options.
•But first, a cautionary note. Many bad presentations will overly rely on animations (and
other stylistic effects) to keep the audience interested (probably because the presentation
itself was less than riveting). The best presentations are the ones that effectively use
animations as an aid in presenting information, rather than distracting from it. In short, a
good rule of thumb to remember would be “Less is more.”
•Animating objects on a slide…
1. Go to the Animationstab.
2. Select/Highlight an object on the slide (this could be the title, the main body of text, a
textbox, a picture, a shape, a chart, a table, a bullet point, etc…
3. Click on “Custom Animation”

Department of Computer Science and Engineering GEC Page 27


OUTPUT

Viva Questions:

1. What is the importance of MS Powerpoint?


2. How do you insert figures in a powerpoint slide?
3. What is the difference between MS Word and Powerpoint in presentation?

Department of Computer Science and Engineering GEC Page 28


Program 4 :Write a C program to calculate the area of triangle using the formula
area = ( − )( − )( − ) , where s= (a+b+c)/2.

Description: The area of a triangle depends on the size of all the three sides of the triangle.
We use the mathematical function “sqrt” for performing the square root operation using the
mathematical header file <math.h>

Algorithm
1. Start.
2. Read three sides of a triangle a,b,c.
3. Calculate s= (a+b+c)/2.
4. Display Area=sqrt(s*(s-a)*(s-b)*(s-c)).
5. Display area of triangle.
6. Stop.
Flowchart

start

Read three sides


of a triangle a,b,c

calculate s= (a+b+c)/2

Display Area=sqrt(s*(s-a)*(s-b)*(s-c))

Display area of
triangle Area

Stop

Program

Department of Computer Science and Engineering GEC Page 29


#include<stdio.h>
#include<math.h>
int main()
{
floata,b,c;
floats,area;
clrscr();
printf("Enter size of each sides of triangle");
scanf("%f%f%f",&a,&b,&c);
s = (a+b+c)/2;
area = sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area of triangle is: %.3f",area);
return 0;
}

Output
1. Enter size of each sides of triangle 4.5 2.5 5
Area of triangle is: 5.612
2. Enter size of each sides of triangle 5 6 3
Area of triangle is: 7.483

Viva questions:

1) Name any four mathematical operations supported by <math.h> file?


2) How do you calculate the perimeter of a triangle?
3) What is the difference between the output format %f and %.3f?
4) What is a pre-processor directive?

Department of Computer Science and Engineering GEC Page 30


Program 5:Write a C program to find the largest of three numbers using ternary
operator.

Description: A ternary operator is a special operator that can check for two conditions in a
single statement. If the condition is true, a sequence of instructions is executed. Else,
another sequence is executed.

Algorithm
1. Start.
2. Read three numbers a,b,c .
3. If a>b goto step 3.1 otherwise goto step 4
3.1.If a>c goto step 3.2 otherwise goto step 3.3
3.2.Display a is largest of three numbers ,goto step 5
3.3.Display c is largest of three numbers ,goto step 5
4. If b>c goto step 4.1 otherwise gotobstep 4.2
4.1.Display b is largest of three numbers goto step 5
4.2.Display c is largest of three numbers
5. Stop.

Flowchart start

Read three numbersa,b,c .

T F
a>b ?

TT a>c ? F b>c ?
F

Display a is Display c is Display b is Display c is


largest of largest of largest of largest of
threenumbers. threenumbers. threenumbers. threenumbers.

Stop

Department of Computer Science and Engineering GEC Page 31


Program
#include<stdio.h>
int main()
{
inta,b,c,big;
clrscr();
printf("\nEnter 3 numbers:");
scanf("%d %d %d",&a,&b,&c);
big=a>b?a>c?a:c:b>c?b:c;
printf("\nThe biggest number is: %d",big);
return 0;
}
Output
1. Enter 3 numbers:345 29 2
The biggest number is: 345
2. Enter 3 numbers: 8 10 9
The biggest number is: 10

Viva questions:

1) <conio.h> file is used to support which operations?


2) How the ternary operator is different from others?
3) What are relational operators?
4) If possible, specify the same program logic without using ternary operator?

Department of Computer Science and Engineering GEC Page 32


Program 6: Implement a C Program tofind the roots of a quadratic equation.

Description:

Nature of roots of quadratic equation can be known from the determinant d = b 2-4ac
if b2-4ac >0 then roots are real and unequal
if b2-4ac =0 then roots are real and equal
if b2-4ac <0 then roots are imaginary

Algorithm:

Step 1: start
Step 2: read the a,b,c value
Step3: assign pow((b*b-4*a*c),0.5) value to d
Step4: if d==0 compute r1=-b/(2*a) , r2=-b/(2*a) go to step 7 otherwise go to step5
Step5: if d<0 print roots are imaginaryand compute real and imaginary parts for r1 and r2
goto step 8 otherwise go to step6
Step6: compute r1 = ((-b+d) / (2*a)) r2 = ((-b-d) / (2*a)) go to step 7
Step7: print r1, r2
Step8: stop

Department of Computer Science and Engineering GEC Page 33


Flowchart:

Start

Read a,b,c

d = pow((b*b-4*a*c),0.5)

F T
d= = 0 d< 0

T F
r1 = ((-b+d) / (2*a)) Display
r1=r2= r2 = ((-b-d) / (2*a)) roots are
-b / (2 * a ) imaginary

Display
r1, r2

Stop

Department of Computer Science and Engineering GEC Page 34


Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
inta,b,c;
double r1,r2,d;
clrscr();
printf("Enter the values of a,b,c:");
scanf("%d%d%d",&a,&b,&c);
d=((b*b)-(4*a*c));
if(d==0)
{
r1=r2=(-b/(2*a));
printf("Roots are Equal:%lf,%lf",r1,r2);
}
else if(d<0)
printf("Roots are Imaginary");
else
{
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("Roots are:%lf,%lf",r1,r2);
}
getch();
}

Output:

Enter the values of a,b,c:1 6 9


Roots are Equal:-3.000000,-3.000000

Enter the values of a,b,c:2 7 6


Roots are:-6.750000,-7.250000

Enter the values of a,b,c:1 2 3


Roots are Imaginary

Viva Questions:
1) What are various types of loop statements?
2) What is the difference between while and do-while statements?
3) List out the C features?

Department of Computer Science and Engineering GEC Page 35


Program 7: Implement a C Program to read two integer operands and one operator
from the user, perform the operation and then print the result. (Consider the
operators +,-,*, /, % and use Switch Statement)

Description:
Take two integer operands and one operator from user and perform some arithmetic
operations by using the following operators like +,-,*, /, %
Ex: 2+3=5

Algorithm:

Step 1: Start

Step 2: Read the values of op1,op2 and operator op

Step 3: if the operator is ‘+’ then


Print op1+op2 go to step 4
if the operator is ‘-‘ then
Print op1-op2go to step 4
if the operator is ‘*‘ then
Print op1*op2 go to step 4
if the operator is ‘/‘ then
Print op1/op2go to step 4
if the operator is ‘%‘ then
Print op1%op2 go to step 4
if operator is not in the above list
print “Invalid arithmetic operator”
Step 4: stop

Department of Computer Science and Engineering GEC Page 36


Flowchart:

start

Read op1,op2 and


opeartor

op?
‘-’
‘+’‘*‘ ‘/’ ‘%’
display display display display displaya% display
a+b a-b a*b a/b value b value “Invalid
value value value operato
r”

start

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int op1,op2;
char op;
clrscr();
printf("\nOperations Performed are: + - * / %");
printf("\nEnter 2 Integers & an Operator:");
scanf("%d %d %c",&op1,&op2,&op);
switch(op)
{
case '+': printf("ADDITION -- Sum is %d",op1+op2);
break;
case '-': printf("SUBTRACTION -- Difference is %d",op1-op2);
break;
case '*': printf("MULTIPLICATION -- Product is %d",op1*op2);
break;
case '/': printf("DIVISION -- Quotient is %d",op1/op2);
break;
case '%': printf("MODULUS -- Remainder is %d",op1%op2);
break;
default: printf("Invalid operation");
break;
}
getch();
}

Department of Computer Science and Engineering GEC Page 37


Output:

Operations Performed are: + - * / %


Enter 2 Integers & an Operator:2
4
*
MULTIPLICATION -- Product is 8

Operations Performed are: + - * / %


Enter 2 Integers & an Operator:2
4
+
ADDITION -- Sum is 6

Operations Performed are: + - * / %


Enter 2 Integers & an Operator:2 4 -
SUBTRACTION -- Difference is -2

Operations Performed are: + - * / %


Enter 2 Integers & an Operator:2
4
/
DIVISION -- Quotient is 0

Operations Performed are: + - * / %


Enter 2 Integers & an Operator:2
4
%
MODULUS -- Remainder is 2

VIVA QUESTIONS:

1) What are the various types of arithmetic operators?


2) What are the types of relational operators?
3) What are the types of logical operators?
4) How do you implement a switch statement?

Department of Computer Science and Engineering GEC Page 38


Program 8: Implement a C Program to display first N natural numbers.

Description:
A natural number is a number that occurs commonly and obviously in nature. As
such, it is a whole, non-negative number. The set of natural numbers, denoted N, can be
defined in either of two ways: N = {0, 1, 2, 3,}

Algorithm

1. Start.
2. Read the value of n.
3. Initialize the variable i=1.
4. Repeat.
4.1.Display i value.
4.2. i is incremented by 1 in each case
Until the condition is failed i.e. i<=n.
5. stop.

Flowchart

start

Read range value (n)

i=1

F
i<=n

Display i value

i+=1

Stop

Department of Computer Science and Engineering GEC Page 39


Program
#include<stdio.h>
int main()
{
inti,n;
clrscr();
printf("\n Enter range of natural numbers ");
scanf("%d",&n);
i=1;
while(i<=n)
{
printf("%d\t", i);
i++;
}
return 0;
}
Output
1) Enter range of natural numbers 10
1 2 3 4 5 6 7 8 9 10
2) Enter range of natural numbers 5
1 2 3 4 5

Viva Questions:
1) What is a natural number?
2) Any program execution in C language starts from which function?
3) How does the printf and scanf functions work?

Department of Computer Science and Engineering GEC Page 40


Program 9: Implement a C Program to Check whether given number is Prime (or)
not.

Description:
A prime number is the one which has only the factors 1 and itself. We check for
the total number of factors for the given number. If the given number has only two factors,
it’s a prime number. Else, it’s not a prime number.

Algorithm:
Step 1: Start
Step 2: initialize c=0
Step 3: Read input ‘n’ from user
Step 4: initialize i=1
Step 5: check whether n%i==0 if true increment c
Step 6: increment i
Step 7: repeat steps 4 and 5 until n>i
Step 8:check c=2 if true print number is prime
Step 9: else print number not prime
Step 10: stop

Department of Computer Science and Engineering GEC Page 41


Flowchart:
START

READ INPUT
‘n’FROM USER

Initialize i=1

F i<=n
?
T

If(n%i
F
=0)

T
Increment count
c

Increment i

c= =2
? Number is
prime

Number is not prime STOP

Department of Computer Science and Engineering GEC Page 42


Program

#include<stdio.h>
void main()
{
int c=0,n,i;
printf("enter a number");
scanf("%d",&n);
i=1;
do
{
if(n%i==0)
c++;
}while(i<=n);
if(c==2)
printf("given number %d is prime",n);
else
printf("given number %d is not prime",n);
getch();
}

Output
1. enter a number21
given number 21 is not prime

2. enter a number251
given number 251 is prime

Viva Questions:
1) What is a composite number?
2) How does a for loop work?
3) State the skeleton for simple if-else statement?
4) What is meant by access specifier?

Department of Computer Science and Engineering GEC Page 43


Program 10: Implement a C Program to Search whether the given element is in the
array.

Description: We declare a one-dimensional array of some elements. In order to search


whether the given element is present in an array or not, we begin from the zero th position
of that array. We traverse throughout the array until the entire list is searched or the
corresponding element is found.

Algorithm

1. Start.
2. Read number of elements in an array (n)
3. Read n array elements
4. Read search key
5. Initialize i=0
6. If i<n do as follows if not goto step 7
6.1.check if array element equal to key then goto step 6.2 otherwise goto 6.3
6.2.Display search key is available in the array ,goto step 8
6.3.Increment i
7. if i=n display key is not found
8. Stop

Department of Computer Science and Engineering GEC Page 44


Flowchart

start

Read number
ofelementsin an array(n)

Initialize i=0

i<n ?
?
T F
Read array elementsa[i]

i++

Read search key k

i<n
i=0
i++

F T F
a[i]==
k
T
key is available

F i==n
?
T

key is not found

Stop
Department of Computer Science and Engineering GEC Page 45
Program:

main()
{
int a[20],i,n,key;
clrscr();
printf("Enter no of elements\n");
scanf("%d",&n);
printf("Enter %d elements",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter key");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
printf("element is found at %d thlocation",i);
break;
}
}
if(i==n)
printf("key not found");

Output:

1) Enter no of elements 5
Enter 5 elements5
4
3
2
1
Enter key2
element is found at 3 th location

2) Enter no of elements 8
Enter 8 elements14 2 54 896 47 25 14 7
Enter key100
key not found

Viva Questions:
1) How an array is efficient in storing and organizing of data?
2) If the total elements of an array are n, then the maximum possible value of index is?
Why?
3) How the search operation does takes place in an array?
Department of Computer Science and Engineering GEC Page 46
Program 11: Implement a C Program to Perform Addition and multiplication of two
matrices.

Implement a C Program to Perform Addition of two matrices.

Description:A matrix is a collection of numbers arranged into a fixed


number of rows and columns. Usually the numbers are real numbers. In
general, matrices can contain complex numbers but we won't see those
here. Here is an example of a matrix with three rows and three
columns:

The format is : rows X columns. We perform addition operations on a


matrix by checking for compatibility conditions

Algorithm:

Step 1: start
Step 2: read the size of matrices A,B – m,n
Step 3: read the elements of matrix A
Step 4: read the elements of matrix B
Step 5: perform the additionoperation place the result in C matrix
Step 6: print the elements of matrix C
Step 7: Stop

Department of Computer Science and Engineering GEC Page 47


Flowchart:
start

Read number of rows


r1 and number of
columns c1 of matrix 1

Read number of rows


r2 and number of
columns c2 of matrix 2

If r1!=r2 T
and c1!=c2

F
Display
Read matrices A and B invalid order
of matrices

F i=0 i++
i<r1

j=0 j<c1 F
j++

c[i][j]=a[i][j]+b[i][j];

Print result
matrix

stop

Department of Computer Science and Engineering GEC Page 48


Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int r1,c1,r2,c2,i,j,k;
clrscr();
printf("Enter Number of rows & columns for Matrix1:");
scanf("%d %d",&r1,&c1);
printf("Enter Number of rows & columns for Matrix2:");
scanf("%d %d",&r2,&c2);
if(r1==r2 && c1==c2)
{
printf("Enter elements for Mat1:");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}
printf("Enter elements for Mat2:");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d\t",c[i][j]);
}
}
}
else
printf("Matrix addition is not possible");
getch();

Department of Computer Science and Engineering GEC Page 49


Output:
1)
Enter Number of rows & columns for Matrix1:2
2
Enter Number of rows & columns for Matrix2:2
2
Enter elements for Mat1:2
2
2
2
Enter elements for Mat2:2
2
2
2

4 4
4 4

2)
Enter Number of rows & columns for Matrix1:2
3
Enter Number of rows & columns for Matrix2:3
2
Matrix addition is not possible

Department of Computer Science and Engineering GEC Page 50


Implement a C Program to Perform Multiplication of two matrices.

Description:A matrix is a collection of numbers arranged into a fixed


number of rows and columns. Usually the numbers are real numbers. In
general, matrices can contain complex numbers but we won't see those
here. Here is an example of a matrix with three rows and three
columns:

The format is : rows X columns. We perform multiplication operations


on a matrix by checking for compatibility conditions

Algorithm:
Step 1: start
Step 2: read the size of matrices A,B
Step 3: check compatibility of matrices for multiplicationi.e, number of columns
in the first matrix should be equal to number of rows in the second matrix.
Step 4: read the elements of matrix A
Step 5: read the elements of matrix B
Step 6: perform the multiplication operation by storing the resulting values into
matrix C.
Step 7: print the resultant matrix C.
Step 8: Stop

Department of Computer Science and Engineering GEC Page 51


Flowchart :
start

Read number of rows


r1 and number of
columns c1 of matrix

Read number of rows


r2 and number of
columns c2 of matrix B

If c1!=r2 T
F
Read matrices A and B

Display
i=0 i++ invalid order
F i<r1 of matrices

j=0 j<c2
j++

c[i][j]=0
F
k=0 k<c1
k++

T
c[i][j]=c[i][j]+(a[i][k]*b[k

Print result matrix

stop

Department of Computer Science and Engineering GEC Page 52


Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int r1,c1,r2,c2,i,j,k;
clrscr();
printf("Enter Number of rows & columns for Matrix1:");
scanf("%d %d",&r1,&c1);
printf("Enter Number of rows & columns for Matrix2:");
scanf("%d %d",&r2,&c2);
if(c1==r2)
{
printf("Enter elements for Mat1:");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}
printf("Enter elements for Mat2:");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
}
printf("result");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<c1;k++)
c[i][j]+=a[i][k]*b[k][j];
printf("%d\t",c[i][j]);
}
}
}
else
printf("\n Matrix multiplication is not possible");
getch();

Department of Computer Science and Engineering GEC Page 53


Output:

1)
Enter Number of rows & columns for Matrix1:2
3
Enter Number of rows & columns for Matrix2:3
2
Enter elements for Mat1:2
2
2
2
2
2
Enter elements for Mat2:2
2
2
2
2
22
result
12 12
12 12

2)
Enter Number of rows & columns for Matrix1:2
3
Enter Number of rows & columns for Matrix2:2
2

Matrix multiplication is not possible

VIVA QUESTIONS:

1) What is a matrix? How are they useful in representing data?


2) What is condition for performing matrix addition?

3) We have two matrices A of order 3*3, B of order 2*2. Can we perform


multiplication operation? Justify.

Department of Computer Science and Engineering GEC Page 54


Program 12:Implement a C Program to find the factorial of a given integer using
recursive function.

Description:

Factorial of a number is nothing but the multiplication of numbers from a given


number to 1
Ex: 5! =5*4*3*2*1= 120

Algorithm:

main program

Step 1: start
Step 2: read n
Step 3: call sub program
Step4: f=fact(n)
Step 5: print f value
Step 6: stop

Sub program:

Step 1: initialize the f


Step 2: if n= = 0 return 1 to main program if not goto step 3
Step 3: return n*fact(n-1) go to main program

Department of Computer Science and Engineering GEC Page 55


Flowchart:

Start

Read n

Call subprogram

f = fact(n)

print f

Stop

Sub program

fact(int n)

false
n==0

true
Return 1 Return
n*fact(n-1)

Return to main
program

Department of Computer Science and Engineering GEC Page 56


Program:

#include<stdio.h>
#include<conio.h>
int fact(int n)
{
int f;
if((n==0)||(n==1))
return 1;
else
f=n*fact(n-1);
return(f);
}
void main()
{
int n;
clrscr();
printf("enter the number :");
scanf("%d",&n);
printf("factorial of number %d is %d",n,fact(n));
getch();
}

Output:

1)
enter the number :5
factorial of number 5 is 120

2)
enter the number :9
factorial of number 9 is 362880

Viva Questions:

1) Define recursion,
2) Differences between normal functions and recursion.
3)Define library function?
4)Define formal parameters?

Department of Computer Science and Engineering GEC Page 57


Program 13:Implement a C Program that uses functions to exchange (Swap) values of
two integers using call by reference.

Description

This program interchanges values of two variables using functions and sending addresses.

Algorithm:

Step 1 : Start
Step 2 : Read num1,num2.
Step 3 : Display before swapping num1,num2.
Step 4 : Calling swap function.
Step 5 : Display after swapping.
Step 6 : Stop

Algorithm for swap:


Step 1 : start
Step 2 : assign t = *a;
Step 3 : assign *a =*b
Step 4 : assign *b = t
Step 5 : stop

Department of Computer Science and Engineering GEC Page 58


Flowchart:
start

Read num1, num2

Before Swapping
Print num1,num2

Swap(&num1,&
num2)

After Swaping Print


num1,num2

Stop
Subprogram:

void swap(int *a,int *b)

temp=*a
*a=*b,
*b=temp

main()

Department of Computer Science and Engineering GEC Page 59


Program
#include<stdio.h>
#include<conio.h>
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void main()
{
int num1,num2;
clrscr();
printf("Enter 2 Numbers to Swap:");
scanf("%d%d",&num1,&num2);
printf("\nBefore Swapping: Two Numbers
are:Num1=%d\tNum2=%d",num1,num2);
swap(&num1,&num2);
printf("\nAfter Swapping:\nTwo Numbers
are:Num1=%d\tNum2=%d",num1,num2);
getch();
}

Output:
Enter 2 Numbers to Swap:10 20
Before Swapping:
Two Numbers are:Num1=10 Num2=20
After Swapping:
Two Numbers are:Num1=20 Num2=10
Enter 2 Numbers to Swap:55 66
Before Swapping:
Two Numbers are:Num1=55 Num2=66
After Swapping:
Two Numbers are:Num1=66 Num2=55

Viva Questions:

1) What is the difference between call by value and call by reference?


2) What is a pointer?
3) Explain about chaining of pointers?

Department of Computer Science and Engineering GEC Page 60


Program 14:Implement a C Program To Illustrate string handling functions-strlen(),
strcmp(), strcat(), strcpy(), strrev()

Description:

Apply some of the predefined functions on given strings. These are included in string.h
header file.

Algorithm

Step 1 : start
Step 2: read s1,s2,s3
Step 3: compute l1= strlen(s1)
Step 4 : print l1
Step 5 : compute s=strcpy(s3,s1)
Step 6 : print s
Step7: compute e=strcmp(s1,s2)
Step 7 : if e==0 go to step 8 otherwise go to step 9
Step 8 : display “strings are equal”
Step 9 : display “Strings are not equal “
Step 10 : display “reverse of 1st string is “, strrev(s1)
Step 11 : display “after concatenation of two strings s1 and s2 is “ strcat(s1,s2)
Step 12 : stop

Department of Computer Science and Engineering GEC Page 61


Flowchart:
start

Read strings
s1,s2,s3

l1=strlen(s1)

Display l1

s=strcpy(s3,s1)

Display s

e=strcmp(s1,s2)

r=strrev(s1) c=strcat(s1,s2)
false
If
e==0

true

Display s1,s2 Display s1,s2


are equal are not equal Display r Display c

stop

Department of Computer Science and Engineering GEC Page 62


Program

#include<string.h>
#include<stdlib.h>
void main()
{
char s1[25],s2[25],s3[25];
int l1,l2,e;
clrscr();
printf("\n Enter three strings ");
gets(s1);
gets(s2);
gets(s3);
l1 = strlen(s1);
printf("\n length of s1 is %d ",l1);
printf("\n After copying 1st string into 3rd string is %s",strcpy(s3,s1));
e=strcmp(s1,s2);
if(e==0)
printf("\n The two strings s1 and s2 are equal ");
else
printf("\n The two Strings s1 and s2 are not equal ");
printf("\n Reverse of first string is %s ",strrev(s1));
printf("\n After adding first two strings %s",strcat(s1,s2));
getch();
}
Output:
1)
Enter three strings sagf
gec
gudlavalleru

length of s1 is 4
After copying 1st string into 3rd string is sagf
The two Strings s1 and s2 are not equal
Reverse of first string is fgas
After adding first two strings fgasgec
2)
Enter three strings gec
gec
gudlavalleru
length of s1 is 3
After copying 1st string into 3rd string is gec
The two strings s1 and s2 are equal
Reverse of first string is ceg
After adding first two strings ceggec

Department of Computer Science and Engineering GEC Page 63


VIVA QUESATIONS:

1) What is a string?
2) Which command is used to combine two strings?
3) Which command is used to copy the strings?
4) Which header file/s is used to implement string functions?
5) What is the difference between a character and a string?
6) What is a sub-string?
7) What do the functions gets(),puts() do?

Department of Computer Science and Engineering GEC Page 64


Program 15: Write a C Program to implement a structure to read and display the
Name, date of Birth and salary of n Employees.

Description:

Structure is a user defined data type available in C programming, which allows you
to combine data items of different kinds.Structures help to organize complex data in a
meaningful way.

Algorithm

1. Start.
2. Declare structure members with tag name emp
3. Read number of employees n
4. Read n employees information.(employee name ,date of birth, salary)
5. Display n employees information.( employee name ,date of birth, salary)
6. Stop

Department of Computer Science and Engineering GEC Page 65


Flowchart
start

Read number
ofemployees(n)

Read n employees name,date


of birth , salary

i=0 i<n
i++

F T
Display n employees name,
date of birth , salary of emp[i]

Stop

Department of Computer Science and Engineering GEC Page 66


Program:
structemp
{
char name[10];
intsal;
struct
{
intdate,year;
char month[10];

}dob;
};

void main()
{
int i;
structemp e[10];
for(i=0;i<10;i++)
{
printf("enter name sal,date,month,year of emp[%d]",i+1);
scanf("%s%d%d%s%d",e[i].name,&e[i].sal,&e[i].dob.date,e[i].dob.month,&e[i].dob.year);
}
for(i=0;i<10;i++)
{
printf("employee %d details\n",i+1);
printf("name:%s\nsal:%d\ndob:%d %s
%d\n",e[i].name,e[i].sal,e[i].dob.date,e[i].dob.month,e[i].dob.year);
}
getch();
}

Output:

enter name sal,date,month,year of emp[1]hari


20000
12
12
2014
enter name sal,date,month,year of emp[2]gopi
21000
12
4
2012
enter name sal,date,month,year of emp[3]lalitha
22000
15
4
2011
enter name sal,date,month,year of emp[4]murari
23000
23

Department of Computer Science and Engineering GEC Page 67


4
2010
enter name sal,date,month,year of emp[5]bhaskar
22000
12
3
1990
enter name sal,date,month,year of emp[6]satish
23000
23
4
1999
enter name sal,date,month,year of emp[7]gopi
20000
12
4
2000
enter name sal,date,month,year of emp[8]vinni
30000
12
4
2000
enter name sal,date,month,year of emp[9]mani
28000
23
5 2000
enter name sal,date,month,year of emp[10]harsha
30000
12
3
1998
Employee1 details
name:hari
sal: 20000
dob:12122014
employee2 details
name:gopi
sal: 21000
dob:1242012

employee 3 details
name:
sal:
dob:
employee4 details
name:
sal:
dob:
employee 5 details
name:bhaskar
sal:22000

Department of Computer Science and Engineering GEC Page 68


dob:12 3 1990
employee 6 details
name:satish
sal:23000
dob:23 4 1999
employee 7 details
name:gopi
sal:20000
dob:12 4 2000
employee 8 details
name:vinni
sal:30000
dob:12 4 2000
employee 9 details
name:mani
sal:28000
dob:23 5 2000
employee 10 details
name:harsha
sal:30000
dob:12 3 1998

VIVA QUESTIONS:

1) What is a Structure?
2) Difference between Structure and Union?
3) How do you access Structure Members?

Department of Computer Science and Engineering GEC Page 69


Program 16:Implement a C Program to count the number of characters, words and
lines in a file.

Description:
In this program, we have to count the number of lines, words and characters in a given
program or given text by using the string function. We check for conditions new line,
empty space for updating the above values.
Algorithm:

Step 1: Start
Step 2:initializenoc=0,now=0,nol=0.
Step 3: open filefname in read mode
Step 4: if fp is NULL, then print unable to open files, go to step 10 otherwise go
to step5
Step 5: read a character from file to c
Step 6: if c !=EOF then goto step 6.1otherwise go to step 7
6.1 increment noc by 1
6.2 if ch=’ ‘ or c=’\t’ then increment now by 1
6.3 if ch=\n the increment nol and now by 1
Step 7: increment nol and now by 1
Step 8:print the noc ,now and nol
Step 9:Close the file.
Step 10: Stop

Department of Computer Science and Engineering GEC Page 70


Flowchart : start

Initialize nol,now,noc to
0
Read file name fname1

fp=fopen(fname1,"r")

F fp==NULL?

T
Unable to open the
filefile

ch=fgetc(p)

ch!=EOF ?

noc++

ch ==’ ‘ || \t’
T F
now++

F ch==’\n’ ?

T
nol++

now++

Display nol+1

Department of Computer Science and Engineering GEC Page 71


Display now+1

Display noc

fclose(fp)

stop

Department of Computer Science and Engineering GEC Page 72


Program:

#include<stdio.h>
#include<conio.h>
void main()
{
intnoc=0,now=0,nol=0;
FILE *fw,*fr;
charfname[20],ch;
clrscr();
printf("\n enter the source file name");
gets(fname);
fr=fopen(fname,"r");
if(fr==NULL)
{
printf("\n error \n");
exit(0);
}
do
{
ch=fgetc(fr);
noc++;
if(ch==' ')
now=1+now;
if(ch=='\n')
{
nol++;
now++;
}
}while(ch!=EOF);
fclose(fr);
printf("\n total no of character=%d",noc);
printf("\n total no of words=%d",now+1);
printf("\n total no of lines=%d",nol+1);
getch();
}

Department of Computer Science and Engineering GEC Page 73


Output:
1)
enter the source file namea.c
total no of character=13
total no of words=1
total no of lines=1

2)
enter the source file namerealloc.c
total no of character=431
total no of words=50
total no of lines=25

VIVA QUESTIONS:

1) What is use of strlen() ?


2) What is the difference betweengetc() and getchar() functions ?
3) What are the null characters used in the above program?

Department of Computer Science and Engineering GEC Page 74

You might also like