C Programs Final
C Programs Final
consumed=present-previous; #include<stdio.h>
calculate(consumed); int main()
} {
int calculate(int consumed) int k,*p,j=0;
{ puts(" Enter the number of
if(consumed >=200) elements");
total=consumed*2.50; scanf("%d",&k);
else puts("\n Enter the Number");
total=consumed*1.50; p=(int*)calloc(k*sizeof(int),2);
printf("----------------------"); for(j=0;j<k;j++)
printf("\nElectricity Bill"); {
printf("\n---------------------"); scanf("%d",p+j);
printf("\nSC. No :\t %d\n Recipt }
No: \t %s\n",srlno,rcptno); for(j=0;j<k;j++)
printf("Bill month: \t%s\n Date of {
Bill: \t%s\n", billmonth, date); printf("\n%d =\t %d",p+j,*(p+j));
printf("Total units consumed: \t }
%d\n @ Rate of: \t%f", consumed,total); }
}
17. File Copy Operation 18. File Merge Operation
#include <stdio.h>
#include<fcntl.h> int main()
char buffer[2048]; {
main(argc,argv) char c ;
FILE *fptr1, *fptr2, *fptr3 ;
int argc; fptr1 = fopen("FIRST.txt","r") ;
char *argv[]; fptr2 =
{ fopen("SECOND.txt","r") ;
int fdold,fdnew; fptr3 =
if(argc !=3) fopen("MERGE.txt","w") ;
{ while((c = fgetc(fptr1)) != EOF)
printf("NEED ARGUMENTS FOR COPY fputc(c, fptr3) ;
PROGRAM"); fclose(fptr1) ;
exit(1); while((c = fgetc(fptr2)) != EOF)
} fputc(c,fptr3) ;
printf("argument %s\n",argv[0]); fclose(fptr3) ;
printf("file %s\n",argv[1]); }
printf("new file %s\n",argv[2]);
fdold=open(argv[1],O_RDONLY); 19. FILE MANAGEMENT
if(fdold==-1) #include<stdio.h>
{ void main()
printf("CANNOT OPEN FILE %s\ {
n",argv[1]); FILE *fptr;
exit(1); char ch,fn[20];
} int op;
fdnew=creat(argv[2],0666); do
if(fdnew==-1) {
{ printf(" FILE MANAGEMENT
printf("CANNOT CREATE FILE %s \ OPERATIONS \n");
n",argv[2]); printf(" 1. Create \n");
exit(1); printf(" 2. Display \n");
} printf(" 3. Update \n\n");
copy(fdold,fdnew); printf(" Enter the choice :");
exit(0); scanf("%d",&op);
} switch(op)
{
copy(old,new) case 1:
int old,new; printf("Enter the file name to be create\n");
{ scanf("%s",fn);
int count; fptr=fopen(fn,"w");
while((count=read(old,buffer,sizeof(buffer)) printf("Enter the data to be stored in the file\
)>0) n");
write(new,buffer,count); scanf("%c",&ch);
} while(ch!='$')
{
fprintf(fptr,"%c",ch); int i,n,j,a[10],b[10];
scanf("%c",&ch); printf(" \n Enter the N numbers :");
} scanf("%d",&n);
fclose(fptr); for(i=0;i<n;i++)
break; {
case 2: scanf("%d",&a[i]);
fptr=fopen(fn ,"r"); }
while(!feof(fptr)) printf(" Original Array \t \t Duplicate Array\
{ n");
ch=getc(fptr); for(i=0;i<n;i++)
if(ch=='\t') {
printf("\t"); b[i]=*(a+n-1-i);
else if(ch=='\n') printf("\n %d \t \t \t \t %d",a[i],b[i]);
printf("\n"); }
else }
printf("%c",ch); 21. Program to sort the given strings in
} alphabetical order
fclose(fptr); #include <stdio.h>
break; # include <string.h>
case 3: char str[10][20], temp[20] ;
fptr=fopen(fn,"a"); int n, i, j ;
printf("Enter the updated data to be stored in int main()
the file\n"); {
scanf("%c",&ch); void sort(int n);
while(ch!='$') printf("Enter the number of strings : ") ;
{ scanf("%d", &n) ;
fprintf(fptr,"%c",ch); sort(n);
scanf("%c",&ch); }
} void sort(int n)
fclose(fptr); {
break; printf("\nEnter the strings : \n\n") ;
default: for(i = 0 ; i < n ; i++)
printf("Illegal number\n"); scanf("%s", str[i]) ;
break; for(i = 0 ; i < n - 1 ; i++)
} for(j = 0 ; j < n - 1; j++)
} if(strcmp(str[j], str[j + 1]) > 0)
while(op>=1 && op<=3); {
} strcpy(temp, str[j]) ;
20. Program to copy the array from strcpy(str[j], str[j + 1]) ;
another array order of the elements of strcpy(str[j + 1], temp) ;
second array should be opposite to the }
first array. printf("\nThe sorted order of strings are :\n\
n") ;
#include<stdio.h> for(i = 0 ; i < n ; i++)
int main() printf("%s \n", str[i]) ;
{ }