Professional Documents
Culture Documents
VENKATESWARA NAGAR,
TRICHY-MADURAI MAIN ROAD (NH 45 B),
EDAMALAIPATTI PUDHUR(POST), PANJAPPUR,
THIRUCHIRAPPALLI � 620012
CS 8261
PROGRAMMING IN C LABORATORY
LAB MANUAL
BATCHELOR OF ENGINEERING
0 0 4 2
OBJECTIVES:
LIST OF EXPERIMENTS:
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)
6. Given a set of numbers like <10, 36, 54, 89, 12, 27>, find sum of weights based
on the
following conditions
A. 5 if it is a perfect cube
B. 4 if it is a multiple of 4 and divisible by 6
C. 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.
15. Compute internal marks of students for five different subjects using structures
and
functions.
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
TOTAL: 60 PERIODS OUTCOMES: Upon completion of the course, the students will
be able to:.
Ex. No
Page No
11
Armstrong Number
15
17
21
8
Body Mass Index of the Individuals
24
27
10
31
11
String Operations
36
12
42
13
45
14
48
15
51
16
Telephone Directory
55
17
Banking Application
65
18
69
Ex: No: 01 Programs Using I/O Statements
And Expressions
Algorithm:
Program:
#include<stdio.h>
void main()
intage,bir_year,cur_year;
clrscr();
scanf("%d",&bir_year);
scanf("%d",&cur_year);
age=cur_year-bir_year;
Input
Expected output
Actual output
Status
Ex: No: 02 Programs Using Decision-Making
Constructs
Algorithm:
Program:
#include<stdio.h>
void main()
int n;
clrscr();
printf("*************************\n");
printf("POSITIVE OR NEGATIVE\n");
printf("*************************\n");
scanf("%d", &n);
if(n>0)
else if(n<0)
printf("The given number %d is negative",n);
else
getch();
Test case:
Input
Expected output
Actual output
Status
Ex: No: 03 Program To Find Whether the
Aim: To write a C Program to find whether the given year is leap year or not.
Algorithm:
2. Declare variables
6. If the condition at step 5.a becomes true, then print the ouput as �It is a leap
year�.
7. If the condition at step 5.b becomes true, then print the ouput as �It is not a
leap year�.
8. If the condition at step 5.c becomes true, then print the ouput as �It is a leap
year�.
9. If neither of the condition becomes true, then the year is not a leap year and
print the same.
Program:
#include<stdio.h>
void main()
int n;
clrscr();
printf("********************\n");
scanf("%d",&n);
if(n%100==0)
else if(n%4==0)
else
getch();
Test case:
Input
Expected output
Actual output
Status
Ex: No: 04 Menu Driven Calculator
Algorithm:
2. Declare variables .
Program:
#include<stdio.h>
void main()
intch,a,b,c;
clrscr();
printf("**********************\n");
printf("**********************\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
scanf("%d %d",&a,&b);
c=a+b;
break;
case 2:
scanf("%d %d",&a,&b);
c=a-b;
break;
case 3:
scanf("%d %d",&a,&b);
c=a*b;
break;
case 4:
c=a/b;
break;
case 5:
scanf("%d",&a);
c=a*a;
break;
default:
printf("Invalid option");
getch();
}
Test case:
Input
Expected output
Actual output
Status
Ex: No: 05 Armstrong Number
Algorithm:
2. Declare variables.
Program:
#include<stdio.h>
void main()
intsum,rem,dup,n;
sum=0;
clrscr();
printf("******************\n");
printf("Armstrong number\n");
printf("******************\n");
scanf("%d",&n);
dup=n;
while(n>0)
rem=n%10;
sum=sum+(rem*rem*rem);
n=n/10;
if(dup==sum)
else
getch();
Test case:
Input
Expected output
Actual output
Status
Ex: No: 06 Sort the Numbers Based on the Weight
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
Algorithm:
2. Declare variables .
Program:
#include <stdio.h>
void main()
intnArray[50],wArray[50],nelem,sq,i,j,t;
clrscr();
scanf("%d",&nelem);
printf("\nEnter %d elements\n",nelem);
for(i=0;i<nelem;i++)
scanf("%d",&nArray[i]);
// Sorting an array
for(i=0;i<nelem;i++)
for(j=i+1;j<nelem;j++)
if(nArray[i] >nArray[j])
t = nArray[i];
nArray[i] = nArray[j];
nArray[j] = t;
wArray[i] = 0;
// sq=(int) sqrt(nArray[i]);
if(percube(nArray[i]))
wArray[i] = wArray[i] + 5;
if(nArray[i]%4==0 &&nArray[i]%6==0)
wArray[i] = wArray[i] + 4;
if(prime(nArray[i]))
wArray[i] = wArray[i] + 3;
printf("<%d,%d>", nArray[i],wArray[i]);
getch();
int prime(intnum)
int flag=1,i;
for(i=2;i<=num/2;i++)
if(num%i==0)
flag=0;
break;
}
return flag;
intpercube(intnum)
inti,flag=0;
for(i=2;i<=num/2;i++)
if((i*i*i)==num)
flag=1;
break;
return flag;
Test case:
Input
Expected output
Actual output
Status
Ex: No: 07 Average Heights of Persons
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:
2. Declare variables
Program:
#include<stdio.h>
void main()
int A[5],avg,count,n,i=0;
clrscr();
printf("**********************\n");
printf("**********************\n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&A[i]);
avg=avgheight(A,n);
count=aboveavg(A,n,avg);
getch();
intaboveavg(int A[],intn,intavg)
inti=0,c=0;
for(i=0;i<n;i++)
if(A[i]>avg)
c++;
return c;
intavgheight(int A[],int n)
int total=0,i=0,mean;
for(i=0;i<n;i++)
total=total+A[i];
}
printf("The sum of the heights are:%d\n",total);
mean=total/n;
return mean;
Test case:
Input
Expected output
Actual output
Status
Ex: No: 08 Body Mass Index of the
Individuals
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:
2. Declare variables
Program:
#include<stdio.h>
void main()
float A[2][10],H[10],W[10],B[10];
inti=0,k=0,j=0,n;
clrscr();
printf("********************\n");
printf("BMI CALCULATION\n");
printf("********************\n");
scanf("%d",&n);
for(i=0;i<2;i++)
if(i==0)
else
for(j=0;j<n;j++)
scanf("%f",&A[i][j]);
i=0;
while(i<1)
for(j=0;j<n;j++)
H[k]=A[i][j]*A[i][j];
k++;
i++;
k=0;
for(j=0;j<n;j++)
W[k]=A[i][j];
k++;
for(i=0;i<n;i++)
B[i]=W[i]/H[i];
printf("%f\n",B[i]);
getch();
Test case:
Input
Expected output
Actual output
Status
Ex: No: 09 Reverse of a Given String
Algorithm:
2. Declare variables .
3. Read a String.
7. Display the output of the reverse string without changing the position of
special characters.
Program:
#include<stdio.h>
#include<string.h>
void main()
char C[50],R[50];
intlen,i=0,temp;
clrscr();
scanf("%s",&C);
len=strlen(C);
printf("The length of the string is:%d\n",len);
strcpy(R,C);
for(i=0;i<len/2;i++)
temp=R[len-i-1];
R[len-i-1]=R[i];
R[i]=temp;
swap(R,C,len);
getch();
inti=0;
char *C1;
char *C2;
C1=C;
C2=R;
while(i<len)
if(!isalpha(C[i]))
C1++;
}
else if(!isalpha(*C2))
C2++;
i--;
else
C[i]=*C2;
C2++;
i++;
return 0;
}
Test case:
Input
Expected output
Actual output
Status
Ex: No: 10 Conversion of Decimal Number into Other Bases
Aim: To write a C Program to convert the given decimal number into binary, octal
and
hexadecimal numbers using user defined functions.
Algorithm:
2. Declare variables.
4. Develop the procedure for conversion of different base by modulus and divide
operator.
Program:
#include<stdio.h>
void main()
int n;
clrscr();
printf("******************************\n");
printf("******************************\n");
scanf("%d",&n);
bin(n);
hex(n);
oct(n);
getch();
int bin(int n)
int B[50],i=0,len;
while(n>0)
B[i]=n%2;
n=n/2;
i++;
len=i;
for(i=0;i<len;i++)
printf("%d",B[len-i-1]);
return 0;
int hex(int n)
inti=0,len,rem;
char H[50];
while(n>0)
rem=n%16;
if(rem>9)
H[i]=55+rem;
else
H[i]=48+rem;
n=n/16;
i++;
len=i;
for(i=0;i<len;i++)
printf("%c",H[len-1-i]);
return 0;
intoct(int n)
{
int O[50],i=0,len;
while(n>0)
O[i]=n%8;
n=n/8;
i++;
len=i;
for(i=0;i<len;i++)
printf("%d",O[len-1-i]);
return 0;
}
Test case:
Input
Expected output
Actual output
Status
Ex: No: 11 String Operations
Aim: To write a C Program to perform string operations on a given paragraph for the
Algorithm:
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
char choice.str[200];
int i, words;
gets(str);
do
printf("4. Stop\n");
choice=getchar();
switch(choice)
case '1' : i = 0;
while(str[i] != '\0')
words++;
i++;
}
printf("\nTotal number of words = %d", words); break;
case '2' :
while(str[i] != '\0')
sentence. (! . ?) */
i++;
putchar (toupper(str[++i]));
i++;
i++;
break;
case '3' :
fflush(stdin);
gets(s_string);
fflush(stdin);
gets(r_string);
puts(str);
break;
getch();
while(choice!=.4.);
return 0; }
char buffer[200];
return;
//copy all the content to buffer before the first occurrence of the search string
strncpy(buffer, str, ch-str);
buffer[ch-str] = 0;
str[0] = 0;
Input
Expected output
Actual output
Status
Ex: No: 12 Towers of Hanoi Using Recursion
Aim:
Algorithm:
2. Declare variables
Program:
#include <stdio.h>
void main()
int num;
clrscr();
printf("*******************\n");
printf("Towers of Hanoi\n");
printf("*******************\n");
getch();
if (num == 1)
return;
else
printf("\n Move disk %d from peg %c to peg %c", num, s_peg, d_peg);
}
Test case:
Input
Expected output
Actual output
Status
Ex: No: 13 Sorting Using Pass by Reference
Aim:
Algorithm:
5. Compare the elements in each pass till all the elements are sorted.
Program:
#include<stdio.h>
void main()
int A[50],n,i=0;
clrscr();
printf("***************************\n");
printf("***************************\n");
scanf("%d",&n);
scanf("%d",&A[i]);
sort(A,n);
getch();
inti=0,j=1,temp;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(A[i]>A[j])
temp=A[i];
A[i]=A[j];
A[j]=temp;
printf("%d\n",A[i]);
}
return 0;
Test case:
Input
Expected output
Actual output
Status
Ex: No: 14 Salary Slip of Employees
Aim:
Algorithm:
2. Declare variables
6. Display the output of the Pay slip calculations for each employee.
Program:
#include<stdio.h>
#include<conio.h>
int empno ;
} ;
void main()
{
int I,n=0;
int more_data = 1;
clrscr() ;
current_ptr = head_ptr;
while (more_data)
scanf("%s",& current_ptr->name) ;
scanf("%d %d %d", & current_ptr ->bpay, & current_ptr ->allow, & current_ptr
>ded) ;
e[i].npay = e[i].bpay + e[i].allow - e[i].ded ; n++;
scanf("%s", answer);
if (answer!= 'Y')
else
{ current_ptr->next = (struct emp *) malloc (sizeof(struct emp));
current_ptr = current_ptr->next;
getch() ;
Test case:
Input
Expected output
Actual output
Status
Ex: No: 15 Internal Marks of Students
Aim:
Algorithm:
2. Declare variables
5. Calculate internal mark by i=total of three test marks / 3 for each subject per
student.
Program:
#include<stdio.h>
#include<conio.h>
struct stud
char name[20];
int marks[5,3];
int i[5];
students[10];
void calcinternal(int);
int main()
int a,b,j,n;
clrscr();
scanf("%d",&n);
for(a=0;a<n;++a)
clrscr();
scanf("%s", students[a].name);
scanf("%ld", &students[a].rollno);
total=0;
for(b=0;b<=4;++b)
for(j=0;j<=2;++j)
scanf("%d", &students[a].marks[b,j]);
}
}
calcinternal(n);
for(a=0;a<n;++a)
clrscr();
printf("\n\n\t\t\t\tMark Sheet\n");
printf("\n------------------------------------------------------------------------"
);
for(b=0;b<5;b++)
printf("\n\n-----------------------------------------------------------------------
-\n");
getch();
return(0);
void calcinternal(int n)
int a,b,j,total;
for(a=1;a<=n;++a)
for(b=0;b<5;b++)
{
total=0;
for(j=0;j<=2;++j)
total += students[a].marks[b,j];
students[a].i[b]=total/3;
Test case:
Input
Expected output
Actual output
Status
Ex: No: 16 Telephone Directory
Aim:
To write a C Program to add, delete, display, Search and exit options for telephone
details of
an individual into a telephone directory using random access file.
Algorithm:
6. Call the procedure (Add, delete, display, Search and exit) for user chosen
option.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char FirstName[20];
char LastName[20];
char PhoneNumber[20];
} phone;
void AddEntry(phone * );
void DeleteEntry(phone * );
void PrintEntry(phone * );
void SearchForNumber(phone * );
int counter = 0;
char FileName[256];
FILE *pRead;
FILE *pWrite;
phone *phonebook;
int iSelection = 0;
if (phonebook == NULL)
return 1;
else {}
do
printf("\n\t\t\tPhonebook Menu");
printf("\n\n\t(1)\tAdd Friend");
printf("\n\t(2)\tDelete Friend");
printf("\n\t(5)\tExit Phonebook");
scanf("%d", &iSelection);
if (iSelection == 1)
AddEntry(phonebook);
if (iSelection == 2)
DeleteEntry(phonebook);
if (iSelection == 3)
PrintEntry(phonebook);
if (iSelection == 4)
SearchForNumber(phonebook);
if (iSelection == 5)
{
return 0;
if ( pWrite == NULL )
exit(EXIT_FAILURE);
else
counter++;
realloc(phonebook, sizeof(phone));
scanf("%s", phonebook[counter-1].FirstName);
scanf("%s", phonebook[counter-1].LastName);
printf("Phone Number (XXX-XXX-XXXX): ");
scanf("%s", phonebook[counter-1].PhoneNumber);
fclose(pWrite);
int x = 0;
int i = 0;
char deleteFirstName[20];
char deleteLastName[20];
scanf("%s", deleteFirstName);
scanf("%s", deleteLastName);
if (strcmp(deleteFirstName, phonebook[x].FirstName) == 0)
if (strcmp(deleteLastName, phonebook[x].LastName) == 0)
{
for ( i = x; i < counter - 1; i++ )
strcpy(phonebook[i].FirstName,phonebook[i+1].FirstName);
strcpy(phonebook[i].LastName,phonebook[i+1].LastName);
strcpy(phonebook[i].PhoneNumber, phonebook[i+1].PhoneNumber);
--counter;
return;
int x = 0;
if ( pRead == NULL)
exit(EXIT_FAILURE);
}
else
printf("\n(%d)\n", x+1);
printf("Name: %s %s\n",
phonebook[x].FirstName, phonebook[x].LastName);
fclose(pRead);
int x = 0;
char TempFirstName[20];
char TempLastName[20];
printf("\nPlease type the name of the friend you wish to find a number for.");
printf("\n\nFirst Name: ");
scanf("%s", TempFirstName);
scanf("%s", TempLastName);
if (strcmp(TempFirstName, phonebook[x].FirstName) == 0)
if (strcmp(TempLastName, phonebook[x].LastName) == 0)
{
Test case:
Input
Expected output
Actual output
Status
Ex: No: 17 Banking Application
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:
Program:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
struct Bank_Account
char no[10];
char name[20];
char balance[15]; };
struct Bank_Account acc;
void main()
FILE *fp;
char *ano,*amt;
char choice;
int type,flag=0;
float bal;
do
clrscr();
fflush(stdin);
printf("2. Display\n");
printf("4. Number of Account Holder Whose Balance is less than the Minimum
Balance\n");
printf("5. Stop\n"); printf("Enter your choice : ");
choice=getchar();
switch(choice)
gets(acc.no);
gets(acc.balance);
fseek(fp,0,2);
fwrite(&acc,sizeof(acc),1,fp);
fclose(fp);
break;
case '2' :
fp=fopen("acc.dat","r");
if(fp==NULL)
printf("\nFile is Empty");
else
printf("%-10s\t\t%-20s\t%s\n",acc.no,acc.name,acc.balance);
fclose(fp);
break;
case '3' :
:");
gets(ano);
for(pos1=ftell(fp);fread(&acc,sizeof(acc),1,fp)==1;
pos1=ftell(fp))
{
if(strcmp(acc.no,ano)==0)
scanf("%d",&type);
fflush(stdin);
gets(amt);
if(type==1)
else
if(bal<0)
flag++;
break;
if(flag==1)
{
pos2=ftell(fp);
pos = pos2-pos1;
fseek(fp,-pos,1);
sprintf(amt,"%.2f",bal);
strcpy(acc.balance,amt);
fwrite(&acc,sizeof(acc),1,fp);
else if(flag==0)
fclose(fp);
break;
flag=0;
while(fread(&acc,sizeof(acc),1,fp)==1)
bal = atof(acc.balance);
if(bal<MINBAL)
flag++;
printf("\nThe Number of Account Holder whose Balance less than the Minimum
Balance :
%d",flag);
fclose(fp);
getch();
while (choice!='5');
Test case:
Input
Expected output
Actual output
Status
Ex: No: 18 Railway Reservation System
Aim:
Algorithm:
2. Declare variables
Program:
#include<stdio.h>
#include<conio.h>
int first=5,second=5,thired=5;
struct node
int ticketno;
int phoneno;
char name[100];
char address[100];
}s[15];
int i=0;
void booking()
printf("\nname:");
scanf("%s",s[i].name);
printf("\nphonenumber:");
scanf("%d",&s[i].phoneno);
printf("\naddress:");
scanf("%s",s[i].address);
scanf("%d",&s[i].ticketno);
i++;
void availability()
int c;
printf("availability cheking");
scanf("%d",&c);
switch(c)
{
case 1:
if(first>0)
printf("seat available\n");
first--;
else
break;
case 2:
if(second>0)
printf("seat available\n");
second--;
else
printf("seat available\n");
thired--;
else
break;
default:
break;
void cancel()
int c;
printf("cancel\n");
switch(c)
case 1:
first++;
break;
case 2:
second++;
break;
case 3:
thired++;
break;
default:
break;
printf("ticket is canceled");
void chart()
{ int c; for(c=0;c<I;c++)
printf(�%d\t%s\n�,s[c].ticketno,s[c].name)
main()
int n;
clrscr();
while(1)
{
printf("1.booking\n2.availability cheking\n3.cancel\n4.Chart \n5. Exit\nenter your
option:");
scanf("%d",&n);
switch(n)
case 1: booking();
break;
case 2: availability();
break;
case 3:
cancel();
break;
case 4:
chart();
break;
case 5:
getch();
exit(0);
default:
break;
getch(); }
Test case:
Input
Expected output
Actual output
Status