cs8261 Lab Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

DHANALAKSHMI COLLEGE OF ENGINEERING

Tambaram, Chennai – 601 301

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

II SEMESTER - R 2017

CS8261 – C PROGRAMMING LABORATORY

LABORATORY MANUAL

Name : _______________________________________

Register No : _______________________________________

Section : _______________________________________

CS8261 DCE Page 1 of 44


DHANALAKSHMI COLLEGE OF ENGINEERING

VISION

Dhanalakshmi College of Engineering is committed to provide highly disciplined,


conscientious and enterprising professionals conforming to global standards
through value based quality education and training.

MISSION
 To provide competent technical manpower capable of meeting requirements
of the industry
 To contribute to the promotion of Academic excellence in pursuit of
Technical Education at different levels
 To train the students to sell his brawn and brain to the highest bidder but to
never put a price tag on heart and soul

CS8261 DCE Page 2 of 44


PROGRAM EDUCATIONAL OBJECTIVES

1. FUNDAMENTALS
To impart students with fundamental knowledge in Mathematics, Science
and fundamentals of engineering that will mould them to be successful
professionals

2. CORE COMPETENCE
To provide students with sound knowledge in engineering and
experimental skills to identify complex software problems in industry and to
develop practical solutions for them

3. BREADTH
To provide relevant training and experience to bridge the gap between
theory and practice this enables them to find solutions for real time problems in
industry and organization, and to design products requiring interdisciplinary skills

4. PROFESSIONALISM SKILLS
To bestow students with adequate training and provide opportunities to
work as team that will build up their communication skills, individual leadership
and supportive qualities, and to enable them to adapt and work in ever changing
technologies

5. LIFELONG LEARNING

To develop the ability of students to establish themselves as professionals


in Computer Science and Engineering and to create awareness about the need
for lifelong learning and pursuing advanced degrees

CS8261 DCE Page 3 of 44


PROGRAM OUTCOMES

a) To apply the basic knowledge of Mathematics, Science and engineering


fundamentals in Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret
data and apply the same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient
practical solution
e) To create, select and apply appropriate techniques, resources, modern
engineering and IT tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic
constraints such as economical, environmental, social, political, ethical, safe
and sustainable
h) To communicate effectively and make others understand exactly what they
are trying to convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique
contributions and work with coordination
j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments

CS8261 DCE Page 4 of 44


CS8261 – C PROGRAMMING LAB
SYLLABUS

COURSE OBJECTIVES

1. To develop programs in C using basic constructs.


2. To develop applications in C using strings, pointers, functions, structures.
3. To develop applications in C using file processing.

LIST OF EXPERIMENTS:

1. Programs using I/O statements and expressions.


2. Programs using decision-making constructs.
3. Write a program to find whether the given year is leap year or Not? (Hint:
not every centurion year is a leap. For example 1700, 1800 and 1900 is
not a leap year)
4. Design a calculator to perform the operations, namely, addition,
subtraction, multiplication, division and square of a number.
5. Check whether a given number is Armstrong number or not?
6. Given a set of numbers like <10, 36, 54, 89, 12, 27>, find sum of weights
based on the following conditions.
if it is a perfect cube.
4 if it is a multiple of 4 and divisible by 6.
3 if it is a prime number.
Sort the numbers based on the weight in the increasing order as shown
below <10,its weight>,<36,its weight><89,its weight>
7. Populate an array with height of persons and find how many persons are
above the average height.
8. Populate a two dimensional array with height and weight of persons and
compute the Body Mass Index of the individuals.
9. Given a string ―a$bcd./fg‖ find its reverse without changing the position
of special characters.

(Example input:a@gh%;j and output:j@hg%;a)


10. Convert the given decimal number into binary, octal and hexadecimal
numbers using user defined functions.
11. From a given paragraph perform the following using built-in functions:
a. Find the total number of words.
b. Capitalize the first word of each sentence.
c. Replace a given word with another word.
12. Solve towers of Hanoi using recursion.
13. Sort the list of numbers using pass by reference.
14. Generate salary slip of employees using structures and pointers.

CS8261 DCE Page 5 of 44


15. Compute internal marks of students for five different subjects using
structures and functions.
16. Insert, update, delete and append telephone details of an individual or a
company into a telephone directory using random access file.
17. Count the number of account holders whose balance is less than the
minimum balance using sequential access file.

Mini project
18. Create a ―Railway reservation system with the following modules
Booking
Availability checking
Cancellation
Prepare chart

COURSE OUTCOMES

1. Develop C programs for simple applications making use of basic


constructs, arrays and strings.
2. Develop C programs involving functions, recursion, pointers, and
structures.
3. Design applications using sequential and random access file processing.

CS8261 DCE Page 6 of 44


CS8261 – C PROGRAMMING LAB
CONTENTS

Sl. Page
Name of the Experiment
No. No.

1. Programs using I/O statements and expressions. 9


2. Programs using decision-making constructs 11
Write a program to find whether the given year is leap year or
Not? (Hint: not every centurion year is a leap. For example
3. 13
1700, 1800 and 1900 is not a leap year)

Design a calculator to perform the operations, namely,


addition, subtraction, multiplication, division and square of a
4. 15
number.

Check whether a given number is Armstrong number or not?


5. 17
Given a set of numbers like <10, 36, 54, 89, 12, 27>, find sum
of weights based on the following conditions.
 5 if it is a perfect cube.
 4 if it is a multiple of 4 and divisible by 6.
6. 19
 3 if it is a prime number.
Sort the numbers based on the weight in the increasing order
as shown below <10,its weight>,<36,its weight><89,its
weight>
Populate an array with height of persons and find how many
7. persons are above the average height. 21

Populate a two dimensional array with height and weight of


8. persons and compute the Body Mass Index of the individuals. 23

Given a string ―a$bcd./fg‖ find its reverse without changing


9. the position of special characters. 25
(Example input:a@gh%;j and output:j@hg%;a)
Convert the given decimal number into binary, octal and
10. hexadecimal numbers using user defined functions. 27
From a given paragraph perform the following using built-in
functions:
a. Find the total number of words.
11. 29
b. Capitalize the first word of each sentence.
c. Replace a given word with another word.

CS8261 DCE Page 7 of 44


Solve towers of Hanoi using recursion.
12. 31
Sort the list of numbers using pass by reference.
13. 33
Generate salary slip of employees using structures and
14. pointers. 35

Compute internal marks of students for five different subjects


15. using structures and functions. 37

Insert, update, delete and append telephone details of an


individual or a company into a telephone directory using
16. random access file. 39

Count the number of account holders whose balance is less


17. than the minimum balance using sequential access file. 41

Mini project
1. Create a ―Railway reservation system with the
following modules
Booking
18. 43
Availability checking
Cancellation
Prepare chart

CS8261 DCE Page 8 of 44


Ex No. 1 PROGRAMS USING I/O STATEMENTS AND EXPRESSIONS

Aim:
To write a C program that uses I/O statements and expressions.

Algorithm:

1. Start the program.


2. Define constant pi= 3.14
3. Read the value of radius
4. Calculate area using formulae pi*radius2
5. Calculate circumference using formulae 2*pi*radius
6. Print area and circumference
7. Stop

Program:

CS8261 DCE Page 9 of 44


Sample Output:
Enter radius of the circle : 8
Area is 200.96 and circumference is 50.2

Result:
Thus a C program that uses I/O statements and expressions is executed
successfully and the output is verified.

CS8261 DCE Page 10 of 44


Ex No. 2 PROGRAM USING DECISION-MAKING CONSTRUCTS

Aim:
To write a C program using decision making constructs.

Algorithm:

1. Declare three integer variables


2. Read the 3 inputs
3. Compare first two numbers and go to Step4
4. If first number is greater than second number then compare first
number with third number else go to step 6
5. If first number is greater than third number print first number as
biggest number else print third number as biggest
6. Compare second number with third number
7. If second number is greater than third number print second number as
biggest number else print third number as biggest

Program:

CS8261 DCE Page 11 of 44


Sample Output:
Enter 3 Numbers
5
9
2
The Second Number 9(b) is Biggest

Result:
Thus a C program that uses decision making constructs is executed
successfully and the output is verified.

CS8261 DCE Page 12 of 44


Ex.No. 3 PROGRAM FOR LEAP YEAR

Aim:
To write a C program to check whether the given year is a leap year or not
using if statement.

Algorithm:
1) Start
2) Read the value of year
3) If year divisible by 400 then print "Leap year"
3.1) Else if year divisible by 4 and not divisible by 100 then print "Leap
year"
3.2) Else print "Not a leap year"
4) Stop

Program:

CS8261 DCE Page 13 of 44


Sample Output:
Enter the year : 2000
2000 is a Leap year
Enter the year : 1800
1800 is not a Leap year
Enter the year : 2004
2004 is a Leap year

Result:
Thus the C program to find the given year is a leap year or not is executed
successfully and the output is verified.

CS8261 DCE Page 14 of 44


Ex.No.:4 PROGRAM FOR CALCULATOR

Aim:
To write a C program to design a calculator to perform the operations,
namely, addition, subtraction, multiplication, division and square of a number.

Algorithm:
1) Start
2) Display calculator menu options
3) Read the operator symbol and operands n1, n2
4) If operator = + then calculate result = n1 + n2
4.1) Else if operator = – then calculate result = n1 – n2
4.2) Else if operator = * then calculate result = n1 * n2
4.3) Else if operator = / then calculate result = n1 / n2
4.4) Else if operator = % then calculate result = n1 % n2
4.2) Else print "Invalid operator" and go to step 6
5) Print result
6) Stop

Program:

CS8261 DCE Page 15 of 44


Sample Output:

Simple Calculator
1. Summation
2.Difference
3.Product
4. Quotient
5. Remainder
Enter the case : 1
Enter operand1 and operand2 : 2 4
2+4=6

Result:
Thus the C program for designing a calculator to perform the operations,
namely, addition, subtraction, multiplication, division and square of a number is
executed successfully and the output is verified.

CS8261 DCE Page 16 of 44


Ex.No.:5 PROGRAM FOR ARMSTRONG NUMBER

Aim:
To write a C program to find if a number is Armstrong or not.

Algorithm:

1. Start
2. Read number
3. Set sum=0 and duplicate=number
4. Reminder=number%10
5. Sum=sum+(reminder*reminder*reminder)
6. Number=number/10
7. Repeat steps 4 to 6 until number > 0
8. If sum = duplicate
9. Display number is armstrong
10. Else
11. Display number is not armstrong
12. Stop

Program:

CS8261 DCE Page 17 of 44


Sample Output:

Enter a number please:153


153 is an armstrong number

Result:
Thus the C program for finding an Armstrong number is executed
successfully and the output is verified.

CS8261 DCE Page 18 of 44


Ex.No.:6 PROGRAM TO FIND THE SUM OF WEIGHTS

Aim:
To write a C program to find the sum of weights based on the given
conditions.
Given a set of numbers like <10, 36, 54, 89, 12, 27>, find sum of weights based
on the following conditions.
 5 if it is a perfect cube.
 4 if it is a multiple of 4 and divisible by 6.
 3 if it is a prime number.
Sort the numbers based on the weight in the increasing order as shown below
<10,its weight>,<36,its weight><89,its weight>

Algorithm:

1. Start
2. create dynamic array for size n
3. set all weightage to zero
4. check and find the weightage
5. sort number in ascending
6. print the weightage
7. stop

Program:

CS8261 DCE Page 19 of 44


Sample Output:

5
10
36
54
89
12
<10,2><12,6><36,11><54,2><89,2>

Result:
Thus the C program for finding the sum of weights is executed
successfully and the output is verified.

CS8261 DCE Page 20 of 44


Ex.No.:7 PROGRAM WITH ARRAYS

Aim:
To write a C program to Populate an array with height of persons and find
how many persons are above the average height.

Algorithm:

1. Start
2. Set avgheight=0
3. Declare array ‘a’ with ten values
4. For i =0 till ten, perform the following
i. if a[i] is between 150.0 and 165.0 , then print the height
ii. Increment avgheight
5. Print avgheight
6. Stop

Program:

CS8261 DCE Page 21 of 44


Sample Output:
150.00
156.00
158.00
159.00
153.50
162.50
total number of persons with average height is 6

Result:

Thus the C program to Populate an array with height of persons and to


find how many persons are above the average height is executed successfully
and the output is verified.

CS8261 DCE Page 22 of 44


Ex.No.:8 PROGRAM WITH 2D ARRAY

Aim:
To write a C program to Populate a two dimensional array with height and
weight of persons and compute the Body Mass Index of the individuals.

Algorithm:

1. Start
2. Get the input height from the user
3. Get the input weight from the user
4. Calculate the BMI with formula w/(h*h)
5. Print the result
6. Stop

Program:

CS8261 DCE Page 23 of 44


Sample Output:
Enter value for h[0][0]:1.51
Enter value for w[0][0]:57
bmi calculation:
24.998905

Result:
Thus the C program to Populate a two dimensional array with height and weight
of persons and compute the Body Mass Index of the individuals is executed
successfully and the output is verified.

CS8261 DCE Page 24 of 44


Ex.No.:9 PROGRAM FOR REVERSING A STRING

Aim:
To write a C program to reverse a string without changing the position of
special characters. (Given a string ―a$bcd./fg‖ find its reverse without changing
the position of special characters. )

Algorithm:

1. Start
2. Let input string be 'str[]' and length of string be 'n'
3. l = 0, r = n-1
4. While l is smaller than r, do following
a) If str[l] is not an alphabetic character, do l++
b) Else If str[r] is not an alphabetic character, do r--
c) Else swap str[l] and str[r]
5. Stop

Program:

CS8261 DCE Page 25 of 44


Sample Output:
input:a@gh%;j
output:j@hg%;a

Result:
Thus the C program to reverse a string without changing the position of
special characters is executed successfully and the output is verified.

CS8261 DCE Page 26 of 44


Ex.No.:10 PROGRAM TO CONVERT DECIMAL INTO BINARY, OCTAL AND
HEXADECIMAL USING USER DEFINED FUNCTIONS

Aim:
To write a C program to Convert the given decimal number into binary,
octal and hexadecimal numbers using user defined functions.

Algorithm:

1. Start
2. Take a decimal number as input.
3. Divide the input number by 2,8,16 and obtain its remainder and quotient.
Store the remainder in the array.
4. Repeat the step 2 with the quotient obtained. Do this until the quotient
becomes zero.
5. Print the array in the reverse order to get the output.
6. stop

Program:

CS8261 DCE Page 27 of 44


Sample Output:
Enter decimal number to be converted:
555
Select conversion
1. Decimal to binary
2. Decimal to octal
3. Decimal to hexadecimal
4. Exit
Enter choice here :1
Converted Binary equivalent value of 555 is 1000101011
Enter choice here :2
Converted octal equivalent value of 555 is 1053
Enter choice here :3
Converted hexadecimal equivalent value of 555 is 22b
Enter choice here :4

Result:
Thus the C program to Convert the given decimal number into binary,
octal and hexadecimal numbers using user defined functions is executed
successfully and the output is verified.

CS8261 DCE Page 28 of 44


Ex.No.:11 PROGRAM USING BUIILT-IN FUNCTIONS

Aim:
To write a C program to perform the following from a given paragraph
using built-in functions,
a. Find the total number of words.
b. Capitalize the first word of each sentence.
c. Replace a given word with another word.

Algorithm:

1. Start
2. Take a string as input.
3. Using for loop search for a empty space in between the words in the
string.
4. Consecutively increment a variable. This variable gives the count of
number of words
5. Check first character is lowercase alphabet if then capitalize ,Continue
the loop ,all the other should be in lowercase.
6. to traverse the original string and count the number of times old word
occurs in the string.
7. Now make a new string of sufficient size so that new word can be
replaced.
8. Now copy original string to new string with replacement of word.

Program:

CS8261 DCE Page 29 of 44


Sample Output:
enter the string:
HELLO FRIENDS HOW ARE YOU?
number of words in given string are: 5
Capitalize string is: Hello Friends How Are You?
New String: Geeks FRIENDS HOW ARE YOU?

Result:
Thus the C program for built-in functions is executed successfully and the
output is verified.

CS8261 DCE Page 30 of 44


Ex.No.:12 PROGRAM FOR TOWER OF HANOI USING RECURSION

Aim:
To write a C program to solve tower of Hanoi using recursion.

Algorithm:

1. Start
2. Read the number of disks
3. Move n-1 disks from source to aux
4. Move nth disk from source to dest
5. Move n-1 disks from aux to dest
6. Stop

Program:

CS8261 DCE Page 31 of 44


Sample Output:

Enter the number of disks : 2


The sequence of moves involved in the Tower of Hanoi are :

Move disk 1 from peg A to peg B


Move disk 2 from peg A to peg C
Move disk 1 from peg B to peg C

Result:
Thus the C program to solve tower of Hanoi using recursion is executed
successfully and the output is verified.

CS8261 DCE Page 32 of 44


Ex.No.:13 PROGRAM TO SORT THE NUMBERS USING PASS BY
REFERENCE

Aim:
To write a C program to sort the list of numbers using pass by reference.

Algorithm:

1. Start
2. Starting at array index 0, search the entire array to find the smallest value
3. Swap the smallest value found in the array with the value at index 0
4. Repeat steps 1 & 2 starting from the next index
5. Stop

Program:

CS8261 DCE Page 33 of 44


Sample Output:
Data items in original order
2 6 4 8 10 12 89 68 45 37
Data items in ascending order
2 4 6 8 10 12 37 45 68 89

Result:
Thus the C program to sort the list of numbers using pass by reference is
executed successfully and the output is verified.

CS8261 DCE Page 34 of 44


Ex.No.:14 PROGRAM USING STRUCTURES AND POINTERS

Aim:
To write a C program to generate salary slip of employees using
structures and pointers.

Algorithm:

1. Start
2. Structure to store employee salary details
3. Prints payslip for the requested employee
4. Get the number of employees from the user
5. Dynamically allocate memory to store employee salary details
6. Get the employee salary details from the customer
7. Gross and net salary calculation
8. Printing payslip for the given employee id

Program:

CS8261 DCE Page 35 of 44


Sample Output:
Enter the number of employees:2
Enter your input for every employee:
Employee ID:101
Employee Name:Sam
Basic Salary, HRA:5000 500
DA, Medical Allowance:300 500
PF and Insurance:1000 400
Employee ID:102
Employee Name:Ram
Basic Salary, HRA:3000 200
DA, Medical Allowance:300 500
PF and Insurance:800 200
Enter employee ID to get payslip:102
Salary Slip of Ram:
Employee ID: 102
Basic Salary: 3000
House Rent Allowance: 200
Dearness Allowance: 300
Medical Allowance: 500
Gross Salary: 33000.00 Rupees
Deductions:
Provident fund: 800
Insurance: 200
Net Salary: 32000.00 Rupees
Do You Want To Continue(1/0):0

Result:
Thus the C program to generate salary slip of employees using structures
and pointers is executed successfully and the output is verified.

CS8261 DCE Page 36 of 44


Ex.No.:15 PROGRAM USING STRUCTURES AND FUNCTIONS

Aim:
To write a C program to Compute internal marks of students for five
different subjects using structures and functions.

Algorithm:

1. Start
2. structure, student is created.
3. This structure has three members: name (string), roll (integer) and marks
(float).
4. A structure variable s is created to store information and display the
internal marks on the screen.
5. Stop

Program:

CS8261 DCE Page 37 of 44


Sample Output:
Enter student's name: Kevin Amla
Enter roll number: 149
Enter Marks for S1:78
Enter Marks for S2:75
Enter Marks for S3:76
Enter Marks for S4:76
Enter Marks for S5:90

Output
Name: Kevin Amla
Roll: 149
Internal mark for S1:15.6
Internal mark for S2:15
Internal mark for S3:15.2
Internal mark for S4:15.2
Internal mark for S5:18

Result:
Thus the C program to Compute internal marks of students for five
different subjects using structures and functions is executed successfully and the
output is verified.

CS8261 DCE Page 38 of 44


Ex.No.:16 PROGRAM USING RANDOM ACCESS FILE

Aim:
To write a C program to Insert, update, delete and append telephone
details of an individual or a company into a telephone directory using random
access file.

Algorithm:

1. Start
2. Create a function to add data to a file
3. Create a function to update data
4. Create a function to delete the data
5. Create a function to alter the data
6. stop

Program:

CS8261 DCE Page 39 of 44


Sample Output:
Telephone directory
1) Insert
2) Update
3) Delete
4) Append
5) Exit
Enter your choice :1
Enter a name: Jack
Enter no: 44475445
Enter your choice :5

Result:
Thus the C program to Insert, update, delete and append telephone
details of an individual or a company into a telephone directory using random
access file is executed successfully and the output is verified.

CS8261 DCE Page 40 of 44


Ex.No.:17 PROGRAM USING SEQUENTIAL ACCESS FILE

Aim:
To write a C program to Count the number of account holders whose
balance is less than the minimum balance using sequential access file.

Algorithm:

1. Start
2. The data to be stored is Account number, Name, Balance in account.
3. Assume maximum of 05 customers in the bank.
4. Create a function to read all customers details and call it in main.
5. Program must print the Account number and name and balance of each
customer.
6. Stop

Program:

CS8261 DCE Page 41 of 44


Sample Output:
Number of customer records you want to enter? : 3
Enter data for Record #1
Enter account_no : 100
Enter name : Alex
Enter data for Record #2
Enter account_no : 101
Enter name : Monica
Enter data for Record #3
Enter account_no : 103
Enter name : Javed
Press 1 to deposit amount.
Press 2 to withdraw amount.
Press 0 to exit
Enter choice(0-2) : 1
A/c No Name Balance
100 Alex 0
101 Monica 0
103 Javed 0

Result:
Thus the C program to Count the number of account holders whose
balance is less than the minimum balance using sequential access file is
executed successfully and the output is verified.

CS8261 DCE Page 42 of 44


Ex.No.:18 MINI PROJECT

Aim:
To write a C program Create a Railway reservation system with the
following modules Booking, Availability checking, Cancellation, Prepare chart.

Algorithm:

1. Start
2. The data to be stored is train no ,train availability status , cancellation
details , booking details and preparation chart.
3. Create a function to read all train details and call it in main.
4. Program must print the train no and booking status of each customer.
5. Stop

Program:

CS8261 DCE Page 43 of 44


Sample Output:

Result:
Thus the C program to Count the number of account holders whose
balance is less than the minimum balance using sequential access file is
executed successfully and the output is verified.

CS8261 DCE Page 44 of 44

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy