Programming in C Lab Manual
Programming in C Lab Manual
LABORATORY MANUAL
1
ARASU ENGINEERING COLLEGE, KUMBAKONAM
Vision
To reach the levels of Teaching-Learning process with societal concerns, disseminate
technical knowledge, to uplift women with moral values and enhance the role of educands with
ethical awareness.
Mission
Concentrated attention towards the lagging student Sect.
Commitment with a stress on ethical and moral values in shaping the
individual for the technical needs of the nation.
Dissemination of technical knowledge with a stress on pragmatic values.
To alleviate the gap between the learner and the teacher.
Commitment towards the societal values.
2
Program Educational Objectives
(PEOs) PEO 1 (Domain Expertise):
To enable graduates to pursue higher education and research, or have a successful career
in industries associated with Computer Science and Engineering, or as entrepreneurs.
PEO 2 (Enthusiasm for Continuous Quality learning):
To ensure that graduates will have the ability and attitude to adapt to emerging
technological changes.
PEO 3 (Ethical Values):
To Exhibit professional attitude in team work by practicing code of ethics.
3
CS3271 PROGRAMMING IN C LABORATORY LTPC
0042
COURSE OBJECTIVES:
To familiarise with C programming constructs.
To develop programs in C using basic constructs.
To develop programs in C using arrays.
To develop applications in C using strings, pointers, functions.
To develop applications in C using structures.
To develop applications in C using file processing.
LIST OF EXPERIMENTS:
Note: The lab instructor is expected to design problems based on the topics listed. The Examination
shall not be restricted to the sample experiments designed.
COURSE OUTCOMES:
Upon completion of the course, the students will be able to
CO1: Demonstrate knowledge on C programming
constructs. CO2: Develop programs in C using basic
constructs.
CO3: Develop programs in C using arrays.
CO4: Develop applications in C using strings, pointers,
functions. CO5: Develop applications in C using structures.
CO6: Develop applications in C using file processing.
CO PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PS02
1 1 3 3 1 1 1 - - 2 1 2 2 2 2
2 2 3 3 2 1 1 - - 2 1 2 2 2 3
3 2 2 2 1 1 2 - - 2 - 2 2 2 2
4 2 2 2 2 1 2 - - 3 - 3 3 3 2
5 2 2 3 2 3 2 - - 3 - 3 3 3 3
6 2 2 3 2 1 2 - - 2 1 2 2 2 2
Avg. 2 2 3 2 1 2 - - 2 1 2 2 2 2
4
EX 1
PROGRAM USING I/O STATEMENTS AND EXPRESSIONS
DATE:
AIM:
ALGORITHM:
Step 1: Start
Step 2: Declare and initialize the variables for name, address, date of birth, mobile
number and age
Step 3: Display name, address, date of birth, mobile number and age
Step 4: End
PROGRAM:
#include<stdio.h>
void main()
{
char name[20]= "SAI RAM";
char address[80]= "west
tambharam,chennai"; int date=20;
int month=10;
int year=1990;
int mobile=987456321;
int age=25;
printf("\n=====================");
printf("\n NAME: %s",name);
printf("\n ADDRESS:%s",
address);
printf("\n DOB:%d:%d:%d", date , month,
year); printf("\n MOBILE NUMBER:%d",
mobile); printf("\n AGE:%d", age); printf("\
n=====================");
}
5
OUTPUT:
RESULT
Thus the C Program to display the personal details has been executed and the output was
verified.
6
AIM:
ALGORITHM:
Step 1: Start
Step 2: Declare the variables for name, address, date, month, year, mobile number,
age.
Step 3: Read values of name, address , date, month, year, mobile number, age
from the user.
Step 4: Display name, address, date, month, year, mobile number, age.
Step 5: End
PROGRAM:
#include<stdio.h>
#include<conio.h
>
#include<string.h>
int main()
{
char name[20];
char address[80];
int date;
int month;
int year;
long int mobile;
char gender[20];
int age;
printf("\n ENTER YOUR NAME:=");
gets(name);
printf("\nENTER YOUR ADDRESS=");
gets(address);
printf("\nENTER YOUR
date/month/year=");
scanf("%d/%d/%d",&date,&month,&year);
printf("\n ENTER YOUR AGE=");
scanf("%d",&age);
printf("\n ENTER YOUR GENDER(MALE/FEMALE)=");
scanf("%s",gender);
printf("\nENTER YOUR MOBILE NUMBER=");
scanf("%ld" ,&mobile);
7
printf("\n=====================");
printf("\n NAME: %s",name);
printf("\n ADDRESS:%s",
address);
printf("\n DOB:%d:%d:%d", date , month,
year); printf("\n AGE:%d", age);
printf("\n GENDER:%s", gender);
printf("\n MOBILE NUMBER:%d",
mobile); printf("\
n=====================");
return 0;
}
OUTPUT:
=====================
NAME: karthikeyan
ADDRESS:west tambharam,chennai.
DOB:3:12:1992
AGE:28
GENDER:MALE
MOBILE NUMBER:987654321
========================
RESULT:
Thus the C Program to read and display the user details has been executed and the
output was verified.
8
EX 2A
PROGRAM TO DISPLAY BIGGEST OF TWO NUMBERS
DATE:
AIM:
ALGORITHM:
Step 1:Start
Step 8:end
PROGRAM:
#include <stdio.h>
void main()
{
int A,B,C;
printf("Enter 3 integer number \n");
scanf("%d",&A);
scanf("%d",&B);
scanf("%d",&C);
if(A>B){
if(A>C){
printf(" %d is the Greatest Number \n",A);
}
else{
printf("%d is the greatest Number \n",C);
}
}
else{
if(B>C){
9
10
printf("%d is the greatest Number \n",B );
}
else{
printf("%d is the greatest Number \n", C);
}
}
}
OUTPUT:
RESULT:
Thus the C Program to display the personal details has been executed and the output was
verified.
11
EX 2B PROGRAM TO CHECK WHETHER THE ENTERED
CHARACTER IS VOWEL OR NOT(USE SWITCH CASE)
DATE:
AIM:
ii] Program to check whether the entered character is vowel or not(Use switch case)
ALGORITHM:
Step 1: Start
Step 2: Declare and initialize the variables
Step 3: Get the input from the user and compare with each
cases Step 4: if match found, print vowel otherwise print
consonant Step 5: End
PROGRAM:
#include<stdio.h>
#include<conio.h
> int main()
{
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
//condition to check character is alphabet or not
if((ch>='A' && ch<='Z') || (ch>='a' &&
ch<='z'))
{
switch(ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("%c is a VOWEL.\n",ch);
12
13
break;
default:
printf("%c is a CONSONANT.\n",ch);
}
}
else
{
printf("%c is not an alphabet.\n",ch);
}
return 0;
}
OUTPUT:
Enter a character
E is a vowel
Enter a character
X is a
consonant Enter
a character
+ is not an alphabet
RESULT:
Thus the C Program check whether the entered character is vowel or not (Use switch
case) has been executed and the output was verified.
14
EX 3 PROGRAM TO FIND WHETHER THE GIVEN YEAR IS LEAP
YEAR ORNOT
DATE:
AIM:
To write a C Program to find whether the given year is leap year or not.
ALGORITHM:
Step 1: Start
Step 3: Check if year is divisible by 400 then DISPLAY "is a leap year"
Step 4: Check if year is not divisible by 100 AND divisible by 4 then DISPLAY "is
a leap year"
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main()
{
int year;
printf("Enter a year :\n");
scanf("%d", &year);
if ((year % 400) == 0)
printf("%d is a leap year \
n",year); else
if ((year % 100) != 0 && (year % 4) == 0)
printf("%d is a leap year \n",year);
else
printf("%d is not a leap year \n",year);
}
15
OUTPUT:
Enter a year:
2000
2000 is a leap year
Enter a year:
1900
1900 is not a leap year
RESULT:
Thus the C Program to find whether the given year is leap year or not has been
executed successfully and the output was verified.
16
EX 4 PROGRAM TO PERFORM THE CALCULATOR OPERATIONS,
NAMELY, ADDITION, SUBTRACTION, MULTIPLICATION,
DATE: DIVISION AND SQUARE OF ANUMBER
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
// functions
// main
function int
main()
18
scanf("%d %d", &num1, &num2);
printf(“%d^%d=%d\n”,num1,num1,square( num1));
return 0;
int result;
result = n1 + n2;
return result;
int result;
result = n1 - n2;
return result;
19
int result;
result = n1 *
n2; return
result;
int result;
result = n1 / n2;
return result;
int result;
result =
n1*n1; return
result;
20
OUTPUT:
20 + 5 = 25
20 – 5 = 15
20 * 5 = 100
20 / 5 = 4
20^20 = 400
RESULT
21
EX 5 PROGRAM TO CHECK WHETHER A GIVEN NUMBER
IS ARMSTRONGNUMBER OR NOT?
DATE:
EX 5:
AIM:
ALGORITHM:
Step 1: Start
Step 5: Repeat Until num>=0 5.1 sum=sum + cube of last digit i.e [(num%10)*(num
%10)*(num%10)] 5.2 num=num/10
Step 7: Stop
PROGRAM:
#include<stdio.h>
int main()
{
int
num,copy_of_num,sum=0,rem;
printf("\nEnter a number:");
scanf("%d",&num);
while (num != 0)
{
rem = num % 10;
sum = sum +
(rem*rem*rem); num = num
/ 10;
}
if(copy_of_num == sum)
printf("\n%d is an Armstrong
Number",copy_of_num); else
printf("\n%d is not an Armstrong Number",copy_of_num);
return(0);
}
22
23
OUTPUT:
RESULT:
Thus the C Program to check whether a given number is Armstrong or not has been
executed and the output was verified.
24
EX 6 PROGRAM TO CHECK WHETHER A GIVEN NUMBER IS ODD
OR EVEN
DATE:
AIM:
ALGORITHM:
Step 4: If the number is even check the condition as n%2 ==0 else it is even.
PROGRAM:
#include
<stdio.h> int
main()
int number;
scanf("%d", &number);
else
printf("%d is odd.",
number); return 0;
}
25
26
OUTPUT:
Enter an integer: -7
-7 is odd.
Enter an integer : 8
8 is even
RESULT
Thus the C Program to find whether the given number is odd or even number has been
successfully executed and verified
27
EX 7
PROGRAM TO FIND FACTORIAL OF A GIVEN NUMBER
DATE:
AIM:
ALGORITHM:
i←i+1 Step 7.
Display factorial
PROGRAM:
int main()
int n, i; longfactorial = 1;
printf("Enter an integer: ");
scanf("%d",&n);
// show error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't
exist."); else
29
factorial *= i;
// factorial = factorial*i;
}
printf("Factorial of %d = %lu", n, factorial);
}
return 0;
}
OUTPUT:
Enter an integer: 10
Factoriaof 10 = 3628800
RESULT
Thus the C Program to find the factorial of a given number has been successfully
executed and verified.
30
EX 8
PROGRAM TO FIND OUT THE AVERAGE OF 4 INTEGERS
DATE:
AIM:
To find average of 4 integers
ALGORITHM:
Step 1. Start
Step 4. Calculate
PROGRAM:
#include<stdio.h>
void main()
inti,n,sum=0,nu[100];
float avg;
clrscr();
for(i=0;i<3;i++)
scanf("%d",&nu[i]);
sum = sum + nu[i];
avg = (float)sum/n;
31
printf("\nAverage is : %.2f\n",n,avg);
getch();
}
OUTPUT:
Enter the
numbers 32
45
54
22
Average is 38.25
RESULT
Thus the C Program to find the average of 4 numbers has been executed and verified.
32
EX 9
PROGRAM TO DISPLAY ARRAY ELEMENTS USING 2D ARRAYS
DATE:
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
int main(){
/* 2D array declaration*/
int disp[2][3];
int i, j;
for(j=0;j<3;j++)
33
for(i=0; i<2; i++) {
34
for(j=0;j<3;j++) {
[j]); if(j==2){
printf("\n");
return 0;
OUTPUT:
123
456
RESULT:
Thus the C Program to display the array elements of the 2D array has been executed
and the result was verified
35
EX 10
PROGRAM TO PERFORM SWAPPING USING FUNCTIONS
DATE:
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
void main()
void swap(int,int);
inta,b,r;
clrscr();
printf("enter value for a&b:
"); scanf("%d%d",&a,&b);
swap(a,b);
getch();
void swap(inta,int
b) int temp;
temp=a;
a=b;
36
b=temp;
printf("after swapping the value for a & b is : %d %d",a,b);
OUTPUT:
RESULT:
Thus the C Program to swap two numbers using functions has been executed and
verified
37
EX 11
PROGRAM TO DISPLAY ALL PRIME NUMBERS BETWEEN
DATE: TWO INTERVALS USING FUNCTIONS
AIM:
ALGORITHM:
Step 3: Find and Display the prime numbers ie., the numbers that are divisible by 1
and itself between the intervals
PROGRAM:
#include <stdio.h>
/* Function declarations
printf("Enter the lower and upper limit to list primes: "); scanf("%d
printPrimes(lowerLimit, upperLimit);
return 0;
38
}
/* Print all prime numbers between lower limit and upper limit*/
if(isPrime(lowerLimit))
lowerLimit++;
int i;
39
{
prime*/ if(num % i == 0)
return 0;
return 1;
OUTPUT:
1 100
All prime number between 1 100 are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
RESULT:
Thus the C Program to find the prime numbers between two intervals has been
executed and verified.
40
EX 12
PROGRAM TO REVERSE A SENTENCE USING RECURSION
DATE:
AIM:
ALGORITHM:
Step 1: Start
Step 2: Declare the function
reverse Step 3: Call the reverse
function
Step 4: Get the sentence from the user and reverse it
recursively Step 5: stop the execution.
PROGRAM:
#include <stdio.h>
void reverseSentence();
int main() {
printf("Enter a sentence: ");
reverseSentence();
return 0;
}
void reverseSentence() {
char c;
scanf("%c", &c);
if (c != '\n') {
reverseSentence();
printf("%c", c);
}
}
OUTPUT:
Enter a sentence: margorp
emosewa awesome PROGRAM:
RESULT
Thus the C Program to reverse a sentence has been executed and verified
41
42
EX 13
PROGRAM TO GET THE LARGEST ELEMENT OF AN ARRAY
DATE: USINGFUNCTION
AIM:
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <conio.h>
max(int [],int);
void main()
int a[]={10,5,45,12,19};
int n=5,m;
clrscr();
m=max(a,n);
printf("\nmaximum number is
%d",m); getch();
max(int x[],int k)
43
int t,i;
t=x[0];
for(i=1;i<k;i++)
if(x[i]>t)
t=x[i];
return(t);
OUTPUT:
Maximum number is 45
RESULT:
Thus the C Program to display the largest number in an array using function has been
executed and verified.
44
EX 14
PROGRAM TO CONCATENATE TWO STRINGS
DATE:
AIM:
ALGORITHM:
Step1: Start
Step 2: Get the two Strings to be concatenated.
Step 3: Declare a new String to store the concatenated
String. Step 4: Insert the first string in the new string.
Step 5: Insert the second string in the new
string. Step 6: Print the concatenated string.
Step 7: Stop
PROGRAM:
#include <stdio.h>
#include <string.h>
int main()
{
OUTPUT
:
RESULT:
45
Thus the C Program to concatenate two strings has been executed and the result was
verified.
46
EX 15
PROGRAM TO FIND THE LENGTH OF STRING
DATE:
AIM:
string ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <string.h>
int main()
{
char a[100];
int length;
printf("\n Enter a string to calculate its
length="); gets(a);
length = strlen(a);
printf("\nLength of the string = %d\n", length);
return 0;
}
#include <stdio.h>
#include<string.h>
int main()
{
char i=0;a[100];
int length;
printf("\nEnter a string to calculate its
length="); scanf(“%s”,str);
while(string1[i] !='\0') {
i++;
}
length=i;
printf (“\n Length of the string = %d\n",length);
return 0;
}
OUTPUT:
RESULT:
Thus the C Program to find the length of the string has been executed and verified.
EX 16
PROGRAM TO FIND THE FREQUENCY OF A
CHARACTER IN A STRING
DATE:
AIM:
ALGORITHM:
PROGRAM:
#include <stdio.h>
int main() {
char str[1000], ch;
int count = 0;
OUTPUT:
frequency: e Frequency of e = 4
RESULT:
Thus the C Program to find the frequency of a character in a string has been executed
and verified.
EX 17
PROGRAM TO STORE STUDENT INFORMATION IN STRUCTURE
DATE: AND DISPLAY IT
AIM:
ALGORITHM:
Step 1: START
Step 2: Read student details like name,
mark1,2,3 Step 3: Calculate total, and average
Step 4: Display the grade
Step 5: STOP
PROGRAM:
#include<stdio.h>
struct student
{
int roll_no, mark1, mark2, mark3,
total; float average;
char name[10],grade;
};
void struct_funct_student(struct student stu);
int main()
{
struct student stud; printf("\
nRoll No.=");
scanf("%d",&stud.roll_no);
printf("Name=");
scanf("%s",stud.name);
printf("Mark1=");
scanf("%d",&stud.mark1);
printf("Mark2=");
scanf("%d",&stud.mark2);
printf("Mark3=");
scanf("%d",&stud.mark3);
struct_funct_student(stud)
; return 0;
}
void struct_funct_student( struct student stu)
{
stu.total = stu.mark1 + stu.mark2 + stu.mark3;
stu.average = stu.total / 3;
if(stu.average >= 90)
stu.grade='S';
else if(stu.average >=
80) stu.grade='A';
else if(stu.average >=
70) stu.grade='B';
else if(stu.average >=
60) stu.grade='C';
else if(stu.average >=
50) stu.grade='D';
else
stu.grade='F';
printf("\n ROLL NO. \t NAME \t TOTAL \t AVG \t
GRADE \n");
printf("%d \t %s \t %d \t %f \t %c",
stu.roll_no,stu.name,stu.total,stu.average,stu.grade);
}
OUTPUT:
Roll No.= 1
Name= a
Mark1= 95
Mark2= 94
Mark3= 96
1 a 285 95.000000 S
RESULT:
Thus the C Program to store and display student details using structures has been
executed and the result was verified.
EX 18
PROGRAM TO READ THE STUDENT DATA AND
DATE: CALCULATE THETOTAL MARKS
AIM:
ALGORITHM:
student
Step 4 : Calculate the student who got the highest total marks
PROGRAM:
#include<stdio.h>
struct student
int sub1;
int sub2;
int sub3;
int sub4;
int sub5;
};
void main()
int i,total=0;
clrscr();
for(i=0;i<=9;i++)
scanf("%d%d%d",& s[i].sub1,&s[i].sub2,&s[i].sub3,&s[i].sub4,&s[i].sub5);
total=s[i].sub1+s[i].sub2+s[i].sub3+s[i].sub4+s[i].sub5;
getch();
OUTPUT:
80 70 90 80 98
RESULT:
Thus the C Program to print the student details has been executed and the result was
verified.
EX :19
AIM :
To insert, update, delete and append telephone details of an individual or a company into a
telephone directory using random access file.
ALGORITHM :
Step 1: Create a random access file
Step 2: call the respective procedure to insert, update, delete or append based on user choice
Step 3: Access the random access file to make the necessary changes and save
PROGRAM
#include "stdio.h"
#include "string.h"
#include<stdlib.h>
#include<fcntl.h>
struct dir
{
char name[20];
char
number[10];
};
int record = 0;
int main(void) {
int choice = 0;
FILE *fp = fopen( "telephone.dat", "rb+" );
if (fp == NULL ) perror ("Error opening
file"); while (choice != 6)
{
printf("\n1 insert\t 2 update\
n"); printf("3 delete\t 4 display\
n");
printf("5 search\t 6 Exit\n Enter choice:");
scanf("%d", &choice);
switch(choice)
{
case 1: insert(fp); break;
case 2: update(fp); break;
case 3: del(fp); break;
case 4: display(fp);
break; case 5: search(fp);
break; default: ;
}
}
fclose(fp);
return 0;
}
OUTPUT:
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 4
Telephone
directory
Name Number
******************************
* bb 11111
*******************************
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice:
5 Enter name:
bb
bb 11111
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 1
Enter individual/company name:
aa Enter telephone number:
222222
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice:
2 Enter name:
aa
Enter number: 333333
Updated successfully
1 insert 2 update
3 delete 4 display
5 search 6
Exit Enter choice:
Telephone directory
Name Number
******************************
* bb 11111
aa 333333
******************************
* 1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 3
Enter name: aa
1 Deleted successfully
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 4
Telephone directory
Name Number
******************************
* bb 11111
*******************************
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 6
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 was successfully
written and executed.
EX :20
PROGRAM TO COUNT THE NUMBER OF ACCOUNT HOLDERS
WHOSEBALANCE IS LESS THAN THE MINIMUM BALANCE USING
DATE:
SEQUENTIAL ACCESS FILE
AIM:
To count the number of account holders whose balance is less than the minimum
balance using sequential access file.
ALGORITHM :
PROGRAM:
#include <stdio.h>
void insert();
void count();
int main(void)
{
int choice = 0;
while (choice != 3)
{
printf("\n1 insert records\n");
printf("2 Count min balance holders\n");
printf("3 Exit\n");
printf("Enter choice:");
scanf("%d", &choice);
switch(choice)
{
case 1: insert(); break;
case 2: count(); break;
}
}
}
void insert()
{
unsigned int
account,i; char
name[30]; double
balance; FILE* cfPtr;
void count()
{
unsigned int account;
char name[30];
double balance;
float minBal = 5000.00;
int count = 0;
FILE *cfPtr;
if ((cfPtr = fopen("clients.dat", "r")) == NULL)
printf("File could not be opened");
else
{
printf("%-10s%-13s%s\n", "Account", "Name",
"Balance"); fscanf(cfPtr, "%d%29s%lf", &account, name,
&balance);
while (!feof(cfPtr))
{
if (balance < minBal)
{
printf("%-10d%-13s%7.2f\n", account, name, balance);
count++;
}
fscanf(cfPtr, "%d%29s%lf", &account, name, &balance);
}
fclose(cfPtr);
printf("The number of account holders whose balance is less than the minimum balance:
%d", count);
}
}
OUTPUT:
1 insert records
2 Count min balance
holders 3 Exit
Enter choice:1
Enter the No. of records 2
Enter the account, name, and balance.1001 A
10000 Enter the account, name, and balance.1002
B 300
1 insert records
2 Count min balance
holders 3 Exit
Enter choice:2
Account Name
Balance 1002 B
300.00
The number of account holders whose balance is less than the minimum balance: 1
1 insert records
2 Count min balance
holders 3 Exit
Enter choice:
RESULT :
Thus the C Program to count the number of account holders whose balance is less
than the minimum balance using sequential access file