CPL Lab Record 2023
CPL Lab Record 2023
EX.NO : 1 (A)
DATE : 17.04.2021
PROGRAM
#include<stdio.h>
#define PI 3.1415
float area(float r)
{
return PI*r*r;
}
float perimeter(float r)
{
return 2*PI*r;
}
int main()
{
float radius;
printf("Enter the radius of the circle?\n");
scanf("%f",&radius);
printf("area of circle=%.2f",area(radius));
printf("\nCircumference of circle=%.2f",perimeter(radius));
return 0;
}
OUTPUT
Enter the radius of the circle?
12
area of circle=452.38
Circumference of circle=75.40
RESULT:
PROGRAM
#include <stdio.h>
int main()
{
int a,b,c,big;
printf("\nEnter three numbers to find largest:\n");
scanf("%d %d %d",&a, &b, &c);
if(a>b && a>c)
big=a;
else if(b>c)
big=b;
else
big=c;
printf("The Largest number is = %d",big);
return 0;
}
OUTPUT
Enter three numbers to find largest:
10 34 21
The Largest number is = 34
RESULT:
PROGRAM
#include<stdio.h>
void main()
{
char ename[30];
float basic,pf,tax,da,hra,gross,net;
printf("\nEnter Employee Name :");
gets(ename);
printf("\nEnter the basic pay : ");
scanf("%f",&basic);
pf=(15/100.0)*basic;
da=(38/100.0)*basic;
tax=(15/100.0)*basic;
hra=(10/100.0)*basic;
gross=basic+da+hra;
net=gross-(pf+tax);
printf("\n DA : %.2f",da);
printf("\n HRA : %.2f",hra);
printf("\n Gross Pay : %.2f",gross);
printf("\n\n PF : %.2f",pf);
printf("\n Tax : %.2f",tax);
printf("\n Net Pay : %.2f",net);
return 0;
}
OUTPUT
Enter Employee Name :Ram Kumar D
Enter the basic pay : 50000
DA : 19000.00
HRA : 5000.00
Gross Pay : 74000.00
PF : 7500.00
Tax : 7500.00
Net Pay : 59000.00
RESULT:
OUTPUT
Enter the Celsius value?
45
RESULT:
PROGRAM
#include<stdio.h>
int main()
{
int n;
printf("Input the number?\n");
scanf("%d",&n);
if(n%2==0)
printf("The given number is Even");
else
printf("The given number is Odd");
return 0;
}
OUTPUT
Input the number?
31
The given number is Odd
RESULT:
PROGRAM
#include<stdio.h>
int main()
{
int n;
printf("Input the number?\n");
scanf("%d",&n);
if (n>0)
printf("The given number is Positive");
else if(n==0)
printf("The given number is Zero");
else
printf("The given number is Negative");
return 0;
}
OUTPUT
Input the number?
32
The given number is Positive
RESULT:
OUTPUT
WELCOME TO SIMPLE CALCULATOR
--------------------------------------------------
Press Ctrl + C to terminate program
Enter firstNumber [+ - * / ^] secondNnumber
23+11.5
34.500000
RESULT:
PROGRAM
#include<stdio.h>
#include<math.h>
int main()
{
int n,x,p=0,sum=0,r;
printf("Enter the number: ");
scanf("%d",&n);
x=n;
while(x!=0)
{
p++;
x=x/10;
}
x=n;
while(x!=0)
{
r=x%10;
sum=sum+pow(r,p);
x=x/10;
}
if(n==sum)
printf("\n%d is armstrong",n);
else
printf("\n%d is not armstrong",n);
return 0;
}
OUTPUT
RESULT:
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>
PROGRAM
#include<stdio.h>
#include<math.h>
typedef struct
{
int number,weight;
} SumWeight;
int main()
{
static SumWeight w [50],tmp;
int n,i,j,isPrime,cube_root;
printf("How many numbers your want to read?\n");
scanf("%d",&n);
printf("input %d number one by one:\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&w [i].number);
cube_root = pow(w [i].number, 1.0 / 3.0);
if (cube_root * cube_root * cube_root == w [i].number)
w [i].weight=5;
isPrime=1;
for(j=2;j<w [i].number-1;j++)
if(w [i].number%j==0)
{
isPrime=0;
break;
}
if(isPrime==1)
w [i].weight+=3;
OUTPUT
How many numbers your want to read?
4
input 4 number one by one:
10
11
12
27
numbers based on it weights:
<10,0>,<11,3>,<12,4>,<27,5>
RESULT:
PROGRAM
#include<stdio.h>
int main()
{
int n,i,count=0;
float height[50],avgheight=0;
printf("Enter how many persons? ");
scanf("%d",&n);
printf("Enter persons height in CM:\n");
for(i=0;i<n;i++)
{
printf("Person %d: ",i+1);
scanf("%f",&height[i]);
avgheight += height[i];
}
avgheight = avgheight / n;
printf("\naverage height = %f",avgheight);
for(i=0;i<n;i++)
if(height[i]>avgheight)
count += 1;
printf("\ntotal number of persons above average = %d",count);
return 0;
}
OUTPUT
Enter how many persons? 6
Enter persons height in CM:
Person 1: 167.5
Person 2: 170
Person 3: 188
Person 4: 155
Person 5: 160
Person 6: 157
average height = 166.250000
total number of persons above average = 3
RESULT:
PROGRAM
#include<stdio.h>
#include<math.h>
int main()
{
int n,i;
float WtHt[50][2],BMI;
printf("Enter how many persons? ");
scanf("%d",&n);
printf("Enter persons weight in KG, height in CM:\n");
for(i=0;i<n;i++)
{
printf("Person %d: ",i+1);
scanf("%f",&WtHt[i][0]);
scanf("%f",&WtHt[i][1]);
}
RESULT:
PROGRAM
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
int i,j;
char s[60],ch;
printf("Enter String?\n");
gets(s);
for(i=0,j=strlen(s)-1;i<j;i++,j--)
{
while(!isalnum(s[i]) && i<j)
i++;
while(!isalnum(s[j]) && i<j)
j--;
if(i>=j)
break;
ch=s[i];
s[i]=s[j];
s[j]=ch;
}
OUTPUT
Enter String?
a@gh%;j
RESULT:
while(n!=0)
{
b[i]=set[n%2];
n=n/2;
i++;
}
b[i]='\0';
strrev(b);
}
int main()
{
long dec;
char bin[20];
printf("Enter Decimal Number to convert to Binary?\n");
scanf("%ld",&dec);
Dec_To_Bin(dec,bin);
printf("\nThe binary equivalent is %s",bin);
return 0;
}
OUTPUT
Enter Decimal Number to convert to Binary?
20
while(n!=0)
{
o[i]=set[n%8];
i=i+1;
n=n/8;
}
o[i]='\0';
strrev(o);
}
int main()
{
long dec;
char oct[20];
printf("Enter Decimal Number to convert to Octal?\n");
scanf("%ld",&dec);
Dec_To_Oct(dec,oct);
printf("\nThe Octal equivalent is %s",oct);
return 0;
}
OUTPUT
Enter Decimal Number to convert to Octal?
105
while(n!=0)
{
h[i++]=set[n%16];
n=n/16;
int main()
{
long dec;
char hex[20];
printf("Enter Decimal Number to convert to Hexadecimal?\n");
scanf("%ld",&dec);
Dec_To_Hex(dec,hex);
printf("\nThe Hex equivalent is %s",hex);
return 0;
}
OUTPUT
Enter Decimal Number to convert to Hexadecimal?
109
RESULT:
PROGRAM (A)
/* find total number of words in a given paragraph */
#include<stdio.h>
#include<ctype.h>
int main()
{
int word_count=1,i=0;
char s[500];
printf("Enter the paragraph:\n");
gets(s);
while(s[i]!='\0')
{
if((s[i]==' ' || s[i]=='\t' || s[i]=='\n') &&
(tolower(s[i+1])>='a' && tolower(s[i+1])<='z'))
word_count++;
i++;
}
printf("\nTotal number of words in a given paragraph is %d",word_count);
return 0;
}
OUTPUT
Enter the paragraph:
This is first sentence. This is second sentence. Last sentence.
PROGRAM (B)
/* make first character of every sentence upper case */
#include<stdio.h>
#include<ctype.h>
int main()
{
int i=2;
char s[500];
printf("Enter the paragraph:\n");
gets(s);
PROGRAM (C)
/* find and replace */
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main()
{
int i=0;
char s[500],find[20],replace[20],result[500];
char *p1,*p2;
printf("Enter the paragraph:\n");
gets(s);
printf("Enter the text to find:\n");
gets(find);
printf("Enter the text to replace:\n");
gets(replace);
p1=s;
while(*p1!='\0')
{
p2=strstr(p1,find);
if(p2!=0)
{
OUTPUT
Enter the paragraph:
she sells sea shells in the sea shore
Enter the text to find:
sea
Enter the text to replace:
ocean
RESULT:
PROGRAM
#include<stdio.h>
#include<ctype.h>
#include<string.h>
void hanoi(int disks,char source,char auxiliary,char target)
{
if(disks==1)
{
printf("\nMove disk 1 from rod %c to rod %c",source,target);
return;
}
int main()
{
int ndisks;
printf("Input how many number of disks?\n");
scanf("%d",&ndisks);
hanoi(ndisks, 'A', 'B', 'C');
return 0;
}
OUTPUT
Input how many number of disks?
3
Move disk 1 from rod A to rod C
Move disk 2 from rod A to rod B
Move disk 1 from rod C to rod B
Move disk 3 from rod A to rod C
Move disk 1 from rod B to rod A
Move disk 2 from rod B to rod C
Move disk 1 from rod A to rod C
RESULT:
PROGRAM
#include<stdio.h>
void sortNumbers(int a[],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if( a[i] > a[j] )
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
int main()
{
int i,arr[50],n;
printf("How many number you want to read:\n");
scanf("%d",&n);
printf("Enter %d numbers to sort:\n",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
sortNumbers(arr,n);
printf("numbers after sorting:\n");
for(i=0;i<n;i++)
printf("%-8d",arr[i]);
return 0;
}
OUTPUT
How many number you want to read:
7
Enter 7 numbers to sort:
32 3 65 93 71 12 1
numbers after sorting:
1 3 12 32 65 71 93
RESULT:
PROGRAM
#include<stdio.h>
typedef struct
{
char ename[30],desig[30];
float basic;
} Employee;
int main()
{
int n,i;
float pf,hra,da,gross,tax,net;
Employee *ptr;
printf("\nEnter How many Employees?\n");
scanf("%d",&n);
ptr=(Employee*) malloc( sizeof(Employee) * n );
for(i=0;i<n;i++)
{
printf("Enter Information of Employee %d:\n",i+1);
printf("Name : ");
fflush(stdin);
gets((ptr+i)->ename);
printf("Designation: ");
fflush(stdin);
gets((ptr+i)->desig);
printf("Basic Pay :");
scanf("%f",&(ptr+i)->basic);
}
printf("\n=====================================================");
printf("\nName Designation Basic DA HRA Gross PF Tax NetPay");
printf("\n=====================================================");
for(i=0;i<n;i++)
{
pf = (15/100.0) * (ptr+i)->basic;
da = (38/100.0) * (ptr+i)->basic;
tax = (15/100.0) * (ptr+i)->basic;
hra = (10/100.0) * (ptr+i)->basic;
OUTPUT
Enter How many Employees?
3
Enter Information of Employee 1:
Name : Lakshmi
Designation: Professor
Basic Pay : 76000
Enter Information of Employee 2:
Name : Radha
Designation: Assoc Professor
Basic Pay : 45000
Enter Information of Employee 3:
Name : Sudha
Designation: Asst Professor
Basic Pay : 34000
=================================================================
Name Designation Basic DA HRA Gross PF Tax NetPay
=================================================================
Lakshmi Professor 76000 28880 7600 112480 11400 11400 89680
Radha Assoc Professor 45000 17100 4500 66600 6750 6750 53100
Sudha Asst Professor 34000 12920 3400 50320 5100 5100 40120
=================================================================
RESULT:
PROGRAM
#include<stdio.h>
#include<math.h>
typedef struct
{
char regno[15],sname[30];
int subjects[5][4];
int internals[5];
} Student;
char subjectcode[5][7]={"HS8151","MA8151","PH8151","CY8151","GE8151"};
printf("\n====================================================");
printf("\nRegisterNo :%s Student Name:%s",st[i].regno,st[i].sname);
printf("\n====================================================");
printf("\nInternal marks for 5 subjects(out of 20)");
printf("\n====================================================");
for(j=0;j<5;j++)
{
st[i].internals[j]=0;
for(k=0;k<4;k++)
{
st[i].internals[j] += st[i].subjects[j][k];
}
st[i].internals[j] = round(st[i].internals[j]/400.0*20);
printf("\n%s : %3d",subjectcode[j],st[i].internals[j]);
}
printf("\n=================================================\n\n");
}
}
int main()
OUTPUT
RegisterNo :211420104001 Student Name:Arthi.S
=====================================================
Internal marks for 5 subjects(out of 20)
=====================================================
HS8151 : 14
MA8151 : 18
PH8151 : 17
CY8151 : 18
GE8151 : 17
=====================================================
RegisterNo :211420104002 Student Name:Barathi.K
=====================================================
Internal marks for 5 subjects(out of 20)
=====================================================
HS8151 : 17
MA8151 : 20
PH8151 : 17
CY8151 : 18
GE8151 : 16
=====================================================
RESULT:
Telephone a;
FILE *fp,*fp2;
char name[15];
void add()
{
fp=fopen("tele.dat","a");
printf("\n\tName : ");
fflush(stdin);
gets(a.name);
printf("\n\tTelephone No : ");
fflush(stdin);
gets(a.telno);
fwrite(&a,sizeof(Telephone),1,fp);
printf("\n\tRecord added successfully ");
fclose(fp);
}
void update()
{
int found=0;
fp=fopen("tele.dat","r+");
printf("\n\tEnter the Name to update phone : ");
fflush(stdin);
gets(name);
while(!feof(fp))
{
fread(&a,sizeof(Telephone),1,fp);
if(strcmpi(name,a.name)==0)
{
printf("\n\tEnter new Telephone No : ");
void deletePhone()
{
int found=0;
fp=fopen("tele.dat","r");
fp2=fopen("copy.dat","w");
printf("\n\tEnter the Name to delete record : ");
fflush(stdin);
gets(name);
while(!feof(fp))
{
strcpy(a.telno," ");
fread(&a,sizeof(Telephone),1,fp);
if(strcmp(a.telno," ")==0)
break;
if(strcmpi(name,a.name)==0)
found=1;
else
fwrite(&a,sizeof(Telephone),1,fp2);
}
if(found==1)
printf("\n\trecord deleted successfully");
else
printf("\n\trecord not found for %s",name);
fclose(fp);
fclose(fp2);
remove("tele.dat");
rename("copy.dat","tele.dat");
}
void display()
{
PANIMALAR ENGINEERING COLLEGE
CS8261 - C PROGRAMMING LABORATORY
fp=fopen("tele.dat","r");
printf("\n\tTelephone Directory");
printf("\n\t================================================");
printf("\n\tName \t\t Telephone No");
printf("\n\t================================================");
while(!feof(fp))
{
strcpy(a.telno," ");
fread(&a,sizeof(Telephone),1,fp);
if(strcmp(a.telno," ")!=0)
printf("\n\t%-15s\t%-15s",a.name,a.telno);
}
printf("\n\t================================================");
fclose(fp);
}
int main()
{
int opt;
A1:
printf("\n\t Telephone Directory");
printf("\n\t==============================");
printf("\n\t1.Add record");
printf("\n\t2.Update record");
printf("\n\t3.Delete record");
printf("\n\t4.Display record");
printf("\n\t5.Exit");
printf("\n\t==-===========================");
printf("\n\tYour option: ");
scanf("%d",&opt);
switch(opt)
{
case 1: add();
break;
case 2: update();
break;
case 3: deletePhone();
break;
case 4: display();
break;
}
if(opt!=5) goto A1;
return 0;
}
Your option: 1
Name : Krishnan
Telephone No : 1212121212
Your option: 2
Enter the Name to update phone : Ram Kumar
Enter new Telephone No : 9000888877
Your option: 4
Telephone Directory
=====================================================
Name Telephone No
=====================================================
Ram Kumar 9000888877
Krishnan 1212121212
=====================================================
Your option: 3
Enter the Name to delete record : Ram Kumar
RESULT:
BankAccount a;
int main()
{
FILE *fp;
int n,i, count=0;
float minimumBalance=1000;
fp=fopen("bank.dat","w");
printf("\nHow many account details would you like to add?\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter account holder %d informations:\n",i+1);
printf("Account Number? ");
fflush(stdin);
gets(a.accno);
printf("Name? ");
fflush(stdin);
gets(a.name);
printf("Initial Balance? ");
scanf("%f",&a.balance);
fwrite(&a,sizeof(a),1,fp);
}
fclose(fp);
fp=fopen("bank.dat","r");
printf("\n=================================================");
printf("\nAccount Number\tAccount Holder Name\tBalance");
printf("\n=================================================");
while(!feof(fp))
{
strcpy(a.accno," ");
if(strcmp(a.accno," ")!=0)
{
printf("\n%-15s\t%-20s\t%10.2f",a.accno,a.name,a.balance);
if(a.balance < minimumBalance)
count++;
}
}
printf("\n===================================================");
fclose(fp);
printf("\nNumber of account holder having less than minimum balance is %d",count);
return 0;
}
OUTPUT
How many account details would you like to add?
3
Enter account holder 1 informations:
Account Number? 12612
Name? Kajendran
Initial Balance? 5000
Enter account holder 2 informations:
Account Number? 12613
Name? Rajendran
Initial Balance? 930
Enter account holder 3 informations:
Account Number? 12614
Name? Jeyachendran
Initial Balance? 600
==========================================================
Account Number Account Holder Name Balance
==========================================================
12612 Kajendran 5000.00
12613 Rajendran 930.00
12614 Jeyachendran 600.00
==========================================================
Number of account holder having less than minimum balance is 2
RESULT: