Cs3271 C Lab Manual Ucetk
Cs3271 C Lab Manual Ucetk
NAME :
REGISTER NO:
BRANCH :
YEAR/SEM : /
BONAFIDE CERTIFICATE
Register Number
(REGULATION 2021)
AS PER ANNA UNIVERSITY SYLLABUS
SYLLABUS
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.
TOTAL: 60 PERIODS
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.
TABLE OF CONTENT
LIST OF EXPERIMENTS
SOURCE CODE:
#include <stdio.h>
void main()
{
int a, b, c,d,e;
float f;
clrscr();
printf("Input two integers\n");
scanf("%d%d", &a, &b);
c = a + b; //addition
d=a-b; //subtraction
e=a*b; //multiplication
f=a/b; // division
printf("(%d) +(%d) = (%d)\n", a, b, c);
printf("(%d) -(%d) = (%d)\n", a, b, d);
printf("(%d) *(%d) = (%d)\n", a, b, e);
printf("(%d) + (%d) = (%f)\n", a, b, f);
getch();
}
OUTPUT:
SOURCE CODE:
#include <stdio.h>
void main()
{
int a, b;
float f;
clrscr();
printf("Input two integers\n");
scanf("%d%d", &a, &b);
if(a>b)
{
printf(“%d is larger ”, a);
}
else
{
printf(“%d is larger ”, b);
}
getch();
}
OUTPUT:
Write a program to find whether the given year is leap year or Not?
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
void main()
{
int year;
clrscr();
printf("Enter the Year (YYYY) : ");
scanf("%d",&year);
if(year%4==0 && year%100!=0 || year%400==0)
printf("\nThe Given year %d is a Leap Year");
else
printf("\nThe Given year %d is Not a Leap Year");
getch();
}
OUTPUT:
#include <stdio.h>
#include <conio.h>
void main()
{
int fn,sn,c;
float r;
char c1;
clrscr();
printf("Enter the First Number : ");
scanf("%d",&fn);
printf("\nEnter the Second Number : ");
scanf("%d",&sn);
do
{
printf("\nWhich operations you want to perform (+,-,*,/,s)\n");
c=getch();
switch(c)
{
case '+':
r = fn+sn;
break;
case '-':
r = fn-sn;
break;
case '*':
r = fn*sn;
break;
case '/':
r = fn/(float)sn;
break;
case 's':
r = fn*fn;
break;
} // switch
if(c=='/')
printf("\nResult = %f\n",r);
else
printf("\nResult = %.f\n",r); printf("\ndo
you want continue (y/n) :"); c1 = getch();
} while (c1=='y' || c1=='Y');
}
OUTPUT:
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
long int num,result;
long int armstrong(long);
int base;
clrscr();
printf("Enter the given number : ");
scanf("%ld",&num);
result = armstrong(num);
if(result==num)
printf("\nThe given number %ld is an Armstrong number",num);
else
printf("\nThe given number %ld is not an Armstrong number",num);
getch();
} // main
long int armstrong(long int num)
{
long int num1=num,res;
double r,count=0; double
sum=0.0;
for(;num1!=0;count++,num1/=10);
num1=num;
while(num1!=0)
{
r = num1%10;
sum = sum+pow(r,count);
num1 = num1/10;
}
res = sum;
return res;
}
OUTPUT:
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>
SOURCE CODE:
#include <stdio.h>
#include <math.h>
void main()
{
int nArray[50],wArray[50],nelem,sq,i,j,t;
clrscr();
printf("\nEnter the number of elements in an array : ");
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;
}
//Calculate the weight
for(i=0; i<nelem; i++)
{
wArray[i] = 0;
// sq =(int) sqrt(nArray[i]);
if(percube(nArray[i]))
wArray[i] = wArray[i] + 5;
if(prime(nArray[i]))
wArray[i] = wArray[i] + 3;
OUTPUT:
Populate an array with height of persons and find how many persons are
above the average height.
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,n,sum=0,count=0,height[100];
float avg;
clrscr();
//Read Number of persons
printf("Enter the Number of Persons : ");
scanf("%d",&n);
//Read the height of n persons
printf("\nEnter the Height of each person in centimeter\n");
for(i=0;i<n;i++)
{
scanf("%d",&height[i]);
sum = sum + height[i];
}
avg = (float)sum/n;
//Counting
for(i=0;i<n;i++)
if(height[i]>avg)
count++;
//display
printf("\nAverage Height of %d persons is : %.2f\n",n,avg);
printf("\nThe number of persons above average : %d ",count);
getch();
}
OUTPUT:
Populate a two dimensional array with height and weight of persons and
compute the Body Mass Index of the individuals.
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
void main()
{
int stu[100][2];
int index[100];
int i,n;
float h;
clrscr();
printf("Enter the number of students : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter the Height(cm) and Weight(kg) of student %d :",i+1);
scanf("%d%d",&stu[i][0],&stu[i][1]);
h = (float)(stu[i][0]/100.0);
index[i] = (float)stu[i][1]/(float)(h*h);
} printf("\nStu.No.\tHeight\tWeight\tBMI\tResult\n");
for(i=0;i<n;i++)
{
printf("\n%d\t%d\t%d\t%d\t",i+1,stu[i][0],stu[i][1],index[i]);
if(index[i]<15)
printf("Starvation\n");
else if(index[i]>14 && index[i] < 18)
printf("Underweight\n");
else if(index[i] > 17 && index[i] < 26)
printf("Healthy\n");
else if(index[i] > 25 && index[i] < 31)
printf("Over weight\n");
else if(index[i] > 30 && index[i] < 36)
printf("Obese\n");
else
printf("Severe Obese\n");
} // for loop
getch();
}
OUTPUT:
SOURCE CODE:
#include <stdio.h>
#include <string.h>
#include <conio.h>
void swap(char *a, char *b)
{
char t;
t = *a;
*a = *b;
*b = t;
}
// Main program
void main()
{
char str[100];
// Function Prototype
void reverse(char *);
int isAlpha(char);
void swap(char *a ,char *b);
clrscr();
printf("Enter the Given String : ");
scanf("%[^\n]s",str);
reverse(str);
printf("Reverse String : %s",str);
getch();
}
else if(!isAlpha(str[r]))
r--;
else
{
swap(&str[l], &str[r]);
l++;
r--;
}
}
}
int isAlpha(char x)
{
return ( (x >= 'A' && x <= 'Z') ||
(x >= 'a' && x <= 'z') );
}
OUTPUT:
Convert the given decimal number into binary, octal and hexadecimal
numbers using user defined functions.
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
void swap(char *s1, char *s2)
{
char temp;
temp = *s1;
*s1 = *s2;
*s2 = temp;
}
void reverse(char *str, int length)
{
int start = 0;
int end = length -1;
while (start < end)
{
swap(&str[start], &str[end]);
start++;
end--;
}
}
char* convert(int num, char str[100], int base)
{
int i = 0;
if (num == 0)
{
str[i++] = '0';
str[i] = '\0';
return str;
}
while (num != 0)
{
int rem = num % base;
str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';
num = num/base;
}
str[i] = '\0'; // Append string terminator
// Reverse the string
reverse(str, i);
return str;
}
void main()
{
char str[100];
int n;
clrscr();
printf("Enter the given decimal number : ");
scanf("%d",&n);
printf("The Binary value : %s\n",convert(n,str,2));
printf("The Octal value : %s\n",convert(n,str,8));
printf("The Hexa value : %s\n",convert(n,str,16));
getch();
}
OUTPUT:
SOURCE CODE:
#include <stdio.h>
#include <string.h>
void main()
{
char s[200];
int count = 0, i;
OUTPUT:
Using built-in functions capitalize the first word of each sentence in a paragraph.
SOURCE CODE:
#include <stdio.h>
#define MAX 100
int main()
{
char str[MAX]={0};
int i;
clrscr();
//input string
printf("Enter a string: ");
scanf("%[^\n]s",str); //read string with spaces
return 0;
}
OUTPUT:
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
count_data();
void main()
{
// calling function
count_data();
getch();
}
count_data()
{
FILE *fp,*fp_rep;
char ch,ch1,temp_str[50],rep_str[10],new_str[10];
int count=0; clrscr();
fp=fopen("c:\\windows\\desktop\\input.txt","r");
fp_rep=fopen("c:\\windows\\desktop\\input1.txt","w");
printf("\nEnter String to find:");
scanf("%s",rep_str);
printf("\nEnter String to replace:");
scanf("%s",new_str);
while((ch=getc(fp))!=EOF)
{
if(ch==' ')
{
temp_str[count]='\0';
if(strcmp(temp_str,rep_str)==0)
{
printf("Do u want to replace(y/n):");
ch1=getche();
if(ch1=='y')
{
fprintf(fp_rep," %s",new_str);
count=0;
}
else
{ fprintf(fp_rep," %s",temp_str);count=0;}
}
else
{
fprintf(fp_rep," %s",temp_str);
count=0;
}
}else
{
temp_str[count++]=ch;
}
}
if(strcmp(temp_str,rep_str)==0)
{
OUTPUT:
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
void towerofhanoi(int n, char from, char to, char aux)
{
if (n == 1)
{
printf("\n Move disk 1 from Peg %c to Peg %c", from, to);
return;
}
towerofhanoi(n-1, from, aux, to);
printf("\n Move disk %d from Peg %c to Peg %c", n, from, to);
towerofhanoi(n-1, aux, to, from);
}
int main()
{
int n;
clrscr();
printf("Enter the number of disks : ");
scanf("%d",&n); // Number of disks
towerofhanoi(n, 'A', 'C', 'B'); // A, B and C are names of peg
getch();
return 0;
}
OUTPUT:
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
void main()
{
int n,a[100],i;
void sortarray(int*,int);
clrscr();
printf("\nEnter the Number of Elements in an array : ");
scanf("%d",&n);
printf("\nEnter the Array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sortarray(a,n);
printf("\nAfter Sorting....\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
OUTPUT:
SOURCE CODE:
#include<stdio.h>
#include<dos.h>
struct employee
{
int NO;
char NAME[10];
int DESIGN_CODE;
int DAYS_WORKED;
}EMP[12]={
{1,"GANESH",1,25},
{2,"MAHESH",1,30},
{3,"SURESH",2,28},
{4,"KALPESH",2,26},
{5,"RAHUL",2,24},
{6,"SUBBU",2,25},
{7,"RAKESH",2,23},
{8,"ATUL",2,22},
{9,"DHARMESH",3,26},
{10,"AJAY",3,26},
{11,"ABDUL",3,27},
{12,"RASHMI",4,29}
};
void main()
{
int EMPNO;
void gen_payslip(int);
clrscr();
struct date D;
char DESG[10];
float NETPAY,BASIC,PF,PAYRATE,PTAX=200;
getdate(&D);
printf("\n\n\t\t\tSHREE KRISHNA CHEMISTS AND DRUGGIST");
printf("\n\t\t\t\tSALARY MONTH %d %d\n",D.da_mon,D.da_year);
printf("\n\n\tEMP.NO.: %d\t\tEMP.NAME: %s",EMPNO,EMP[EMPNO-1].NAME);
switch(EMP[EMPNO-1].DESIGN_CODE)
{
case 1: PAYRATE=400;
printf("\tDESIGNATION: CLERK");
break;
case 2: PAYRATE=300;
printf("\tDESIGNATION: SALESMEN");
break;
case 3: PAYRATE=250;
printf("\tDESIGNATION: HELPER");
break;
case 4: PAYRATE=350;
printf("\tDESIGNATION: COMP.OPTR");
break;
}
BASIC=PAYRATE*EMP[EMPNO-1].DAYS_WORKED;
PF=BASIC/10;
printf("\n\n\tDAYS WORKED: %d",EMP[EMPNO-1].DAYS_WORKED);
printf("\t\tPAY RATE: %.0f\t\tGEN.DATE:%d/%d/%d
",PAYRATE,D.da_day,D.da_mon,D.da_year);
printf("\n\t ");
printf("\n\n\tEARNINGS\tAMOUNT(RS.)\t\tDEDUCTIONS\tAMOUNT(RS.)");
printf("\n\t ");
printf("\n\n\tBASIC PAY\t%.0f\t\t\tP.F.\t\t%.0f",BASIC,PF);
printf("\n\n\t\t\t\t\t\tPROF.TAX\t%.0f",PTAX);
printf("\n\n\t ");
printf("\n\n\tGROSS EARN.\t%.0f\t\t\tTOTAL DEDUCT.\t%.0f",BASIC,PF+PTAX);
NETPAY=BASIC-(PF+PTAX);
printf("\n\t\t\t\t\t\tNET PAY\t\t%.0f",NETPAY);
printf("\n\t ");
}
OUTPUT:
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
struct student
{
int rl;
char nm[20];
int m1;
int m2;
int m3;
int m4;
intm5;
int t;
float per;
};
void main()
{
struct student a;
clrscr();
printf(" Enter RollNo, Name amd five sub marks\n"); scanf("%d%s%d%d
%d",&a.rl,&a.nm,&a.m1,&a.m2,&a.m3,&a.m4,&a.m5);
a.t=a.m1+a.m2+a.m3+a.m4+a.m5;
a.per=a.t/5.0; printf("rollno=
%d\n",a.rl); printf("Name=
%sk\n",a.nm); printf("m1=
%d\n",a.m1); printf("m2=
%d\n",a.m2);
printf("m3=%d\n",a.m3);
printf("m4=%d\n",a.m4);
printf("m5=%d\n",a.m5);
printf("total=%d\n",a.t);
printf("per=%f\n",a.per);
getch();
}
OUTPUT:
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
#include <string.h>
struct
person{ char
name[20]; long
telno;
};
void appendData(){
FILE *fp;
struct person obj;
clrscr();
fp=fopen("data.txt","a");
printf("*****Add Record****\n");
printf("Enter Name : ");
scanf("%s",obj.name);
printf("Enter Telephone No. : ");
scanf("%ld",&obj.telno);
fprintf(fp,"%20s %7ld",obj.name,obj.telno);
fclose(fp);
}
void showAllData(){
FILE *fp;
struct person obj;
clrscr();
fp=fopen("data.txt","r");
printf("*****Display All Records*****\n");
printf("\n\n\t\tName\t\t\tTelephone No.");
printf("\n\t\t=====\t\t\t===============\n\n");
while(!feof(fp))
{
fscanf(fp,"%20s %7ld",obj.name,&obj.telno);
printf("%20s %30ld\n",obj.name,obj.telno);
}
fclose(fp);
getch();
void findData(){
FILE *fp;
struct person obj;
char name[20];
int totrec=0;
clrscr();
fp=fopen("data.txt","r");
printf("*****Display Specific Records*****\n");
printf("\nEnter Name : ");
scanf("%s",&name);
while(!feof(fp))
{
fscanf(fp,"%20s %7ld",obj.name,&obj.telno);
if(strcmpi(obj.name,name)==0){
printf("\n\nName : %s",obj.name);
printf("\nTelephone No : %ld",obj.telno);
totrec++;
}
}
if(totrec==0)
printf("\n\n\nNo Data Found");
else
printf("\n\n===Total %d Record found===",totrec);
fclose(fp);
getch();
}
void main()
{ char choice;
while(1)
{ clrscr();
printf("*****TELEPHONE DIRECTORY*****\n\n");
printf("1) Append Record\n");
printf("2) Find Record\n");
printf("3) Read all record\n");
printf("4) Exit\n");
printf("Enter your choice : ");
fflush(stdin);
choice = getche();
switch(choice){
case'1' : //call append record
appendData();
break;
case'2' : //call find record
findData();
break;
OUTPUT:
Count the number of account holders whose balance is less than the
minimum balance using sequential access file.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#define MINBAL 500
struct Bank_Account
{
char no[10]; char
name[20]; char
balance[15];
};
struct Bank_Account acc;
void main()
{
long int pos1,pos2,pos;
FILE *fp;
char *ano,*amt;
char choice;
int type,flag=0;
float bal;
do
{
clrscr();
fflush(stdin);
printf("1. Add a New Account Holder\n");
printf("2. Display\n");
printf("3. Deposit or Withdraw\n");
printf("4. Number of Account Holder Whose Balance is less than the Minimum
Balance\n");
printf("5. Delete All\n");
printf("6. Stop\n");
printf("Enter your choice : ");
choice=getchar();
switch(choice)
{
case '1' : fflush(stdin);
fp=fopen("acc.dat","a");
break;
}
} 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)
printf("\nA/c Number Not exits... Check it again");
fclose(fp);
break;
case '4' :
fp=fopen("acc.dat","r");
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);
break;
case '5' :
remove("acc.dat");
break;
case '6' :
fclose(fp);
exit(0);
}
printf("\nPress any key to continue....");
getch();
} while (choice!='6');
}
OUTPUT:
Mini project
SOURCE CODE:
#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;
void booking()
{
printf("enter your details");
printf("\nname:");
scanf("%s",s.name);
printf("\nphonenumber:");
scanf("%d",&s.phoneno);
printf("\naddress:");
scanf("%s",s.address);
printf("\nticketnumber only 1-10:");
scanf("%d",&s.ticketno);
}
void availability()
{
int c;
printf("availability cheking");
printf("\n1.first class\n2.second class\n3.thired class\n");
printf("enter the option");
scanf("%d",&c);
switch(c)
{
case 1:if(first>0)
{
printf("seat available\n");
first--;
}
else
{
printf("seat not available");
}
break;
case 2: if(second>0)
{
printf("seat available\n");
second--;
}
else
{
printf("seat not available");
}
break;
case 3:if(thired>0)
{
printf("seat available\n");
thired--;
}
else
{
printf("seat not available");
}
break;
default:
break;
}
}
void cancel()
{
int c;
printf("cancel\n");
printf("which class you want to cancel");
printf("\n1.first class\n2.second class\n3.thired class\n");
printf("enter the option");
scanf("%d",c);
switch(c)
{
case 1:
first++;
break;
case 2:
second++;
break;
case 3:
thired++;
break;
default:
break;
}
printf("ticket is canceled");
}
main()
{
int n;
clrscr();
printf("welcome to railway ticket reservation\n");
printf("1.booking\n2.availability cheking\n3.cancel\nenter your option:");
scanf("%d",&n);
switch(n)
{
case 1: booking();
break;
case 2: availability();
break;
case 3: cancel();
break;
default:
break;
}
getch();
}
OUTPUT: