0% found this document useful (0 votes)
3 views7 pages

C Programs Final

The document contains multiple C programming examples covering various topics such as arithmetic operations using pointers, string manipulation, employee details, memory allocation, file operations, and sorting strings. Each section includes code snippets demonstrating specific functionalities like addition, subtraction, string length calculation, and file management. The document serves as a comprehensive reference for basic to intermediate C programming concepts.

Uploaded by

santhikala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

C Programs Final

The document contains multiple C programming examples covering various topics such as arithmetic operations using pointers, string manipulation, employee details, memory allocation, file operations, and sorting strings. Each section includes code snippets demonstrating specific functionalities like addition, subtraction, string length calculation, and file management. The document serves as a comprehensive reference for basic to intermediate C programming concepts.

Uploaded by

santhikala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

4.

Program to perform the arithmetic int regno,m1,m2,m3,total;


operations using pointer. float avg;
char name[25];
#include<stdio.h>
int *p1,*p2,*p3;
int main()
{ p1=&m1;
int a,b,*p,*j; p2=&m2;
printf("enter the value of a and b\n"); p3=&m3;
scanf("%d%d",&a,&b); printf("\n Enter student name");
p=&a; scanf("%s",name);
j=&b; puts("Enter regno");
printf("\n Addition a+b =%d",*p+b);
scanf("%d",&regno);
printf("\n Subtraction a-b =%d",*p-b);
printf("\n Product a*b =%d",*p**j); printf("Enter the marks for 3 subjects");
printf("\n Division a/b =%d",*p / *j); scanf("%d\n%d\n%d",&m1,&m2,&m3);
} total=*p1+*p2+*p3;
avg=total/3;
5. Program to find the length of a string printf("\n Student details \n");
including and excluding space using pointers printf("Name :%s\n",name);
printf("Reg no:%d\n",regno);
#include<stdio.h>
#include<string.h> printf("Subject mark 1:%d\n",*p1);
int main() printf("Subject mark 2:%d\n",*p2);
{ printf("Subject mark 3:%d\n",*p3);
char str[20],*s; printf("Total:%d\n",total);
int p=0,q=0; printf("Average:%f\n",avg);
printf("Enter the String :"); }
gets(str);
s=str;
while(*s!='\0') 7. Employee Details
{
printf("%c",*s); #include<stdio.h>
p++; main()
s++; {
if(*s==32) /* ASCII Equivalent of space int empno;
is 32*/
char empname[25], dept[25];
q++;
} FILE *ptr;
printf("\nLength of string including ptr=fopen("emp1.txt","w");
spaces :%d",p); printf("enter name,no,dept");
printf("\nLength of string excluding scanf("%s%d%s",empname,&empno,dept);
spaces :%d",p-q); fclose(ptr);
} ptr=fopen("emp1.txt","r");
fscanf(ptr,"%s%d%s",empname,&empno,dept);
6. Student Details Using pointers
printf("emp name : %s\n",empname);
printf("emp no : %d\n",empno);
Source Code:
printf("dept: %s\n",dept);
#include <stdio.h>
}
main()
{
8. Print Value and Address of Variable mul(a,b);
Using Pointer break;
case 4:
#include<stdio.h> div(a,b);
main() break;
{ default:
int a=32; ` printf("\n Wrong value");
int *b; exit(0);
b=&a; }
printf("\n The value of a=%d",a); }
printf("\n The value of a=%d",*(&a)); }
printf("\n The value of a=%d",*b);
int add(int p, int q)
printf("\n Address of a=%u",&a);
{
printf("\n Address of a=%u",b);
int r;
printf("\n Address of b=%u",&b);
r=p+q;
printf("\n value of a=%d address of a=
%u",*b,b); printf("\n Answer is %d\n",r);
} }
int sub(int p, int q)
Functions {
9. Menu Driven Arithmetic Operations using int r;
Functions r=p-q;
printf("\n Answer is %d\n",r);
#include<stdio.h> }
main() int mul(int p, int q)
{ {
int ch,a,b,c; int r;
printf("\n Perform Arit6hemetic operations"); r=p*q;
printf("\n Enter the values of a and b \n"); printf("\n Answer is %d\n",r);
scanf("%d\n%d",&a,&b); }
while(1)
{ 10. Swap using call by value
printf("\n 1.Addition \n2. Subtraction \
#include<stdio.h>
n3.Multiplication \n4. Division\n"); int main ()
printf("\n Enter your choice"); {
scanf("\n%d",&ch); int a , b ;
switch(ch) int swap (int a, int b);
{ printf("Enter the value of A : ");
case 1: scanf("%d", &a);
printf("\nEnter the value of B : ");
add(a,b);
scanf("%d", &b);
break; printf("\nBefore Swapping \nA = %d, B =
case 2: %d", a, b);
sub(a,b); swap(a, b);
break; printf("\nAfter Swappig \nA = %d, B = %d",
case 3: a, b);
} printf("Enter a +ve Number");
int swap (int x, int y) }
{ else
int temp; {
temp = x; f=fact(n);
x = y; }
y = temp; printf("\nFactorial is = %d",f);
printf("\nWithin Swap Function\nA = }
%d, B = %d", x, y); int fact(int n)
} {
if(n==1)
11. Swap using call by reference {
return 1;
#include<stdio.h> }
int swap(int *x, int *y); else
{
int main() return n*fact(n-1);
{ }
int A, B; }
printf("Enter the value of A : "); 13. Program to transfer structure to
scanf("%d", &A); function using pointer
printf("\nEnter the value of B : ");
scanf("%d", &B); #include<stdio.h>
printf("\nBefore Swapping \nA = %d,
typedef struct
B = %d", A, B);
swap(&A, &B);
{
printf("\nAfter Swapping \nA = %d, char *name;
B = %d", A, B); int no;
} int age;
}record;
int swap(int *x, int *y) main()
{ {
int tmp; void fun(record *pt);
tmp=*x; static record std={"xxxx",6,17};
*x=*y; printf("\n %s\t %d\t
*y=tmp;
%d",std.name,std.no,std.age);
printf("\nWithin Swap Function\nA =
%d, B = %d", *x, *y);
fun(&std);
} printf("\n %s\t%d\t
%d",std.name,std.no,std.age);
12. Factorial using recursion }
void fun(record *pt)
#include<stdio.h> {
int fact(int); pt->name="YYYY";
int main() pt->no=29;
{ pt->age=18;
int n,f; }
printf("Enter the Number");
scanf("%d",&n);
if(n<0)
{
14. E-BILL calculation 15. Program to allocate memory to
pointer variable using malloc
#include<stdio.h>
int present,previous,consumed,srlno; #include<stdio.h>
char name[20], rcptno[10], billmonth[15], int main()
date[12]; {
float total; int k,*p,j=0;
puts(" Enter the number of
int main() elements");
{ scanf("%d",&k);
int calculate(int c); puts("\n Enter the Number");
p=(int*)malloc(k*sizeof(int));
printf("Enter serial no and Bill No\ for(j=0;j<k;j++)
n"); {
scanf("%d%s",&srlno,&rcptno); scanf("%d",p+j);
}
printf("Enter bill month & date\ for(j=0;j<k;j++)
n"); {
scanf("%s%s",&billmonth,&date); printf("\n%d =\t %d",p+j,*(p+j));
}
printf("Present and Previous }
readings:\n");
scanf("%d 16. Program to allocate memory to
%d",&present,&previous); pointer variable using calloc

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]) ;
{ }

You might also like