PROGRAM FOR CALL BY VALUE
#include<stdio.h>
int main()
{
void swap (int a,int b);
int a,b;
printf("Enter the two numbers to Swap:");
scanf("%d %d",&a,&b);
swap(a,b);
return(0);
}
void swap (int a,int b)
{
int t;
t=a;
a=b;
b=t;
printf("After Swapping : %d %d\n",a,b);
}
OUTPUT:
Enter the two numbers to Swap:
15 87
After Swapping : 87 15
PROGRAM FOR FACTORIAL FINDING NCR:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int fact(int k);
int n,r,ncr;
printf("\n Enter value to n and r:");
scanf("%d %d", &n,&r);
ncr=fact(n)/(fact(r)*fact(n-r));
printf("\n Value of ncr = %d \n",ncr);
}
int fact(int k)
{
int i, p=1;
for(i=1;i<=k;i++)
p=p*i;
return(p);
}
OUTPUT:
Enter value to n and r: 4 2
Value of ncr = 6
PROGRAM TO FIND ADDRESS OF VARIABLE
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=22;
int *b;
int **c;
b=&a;
c=&b;
printf("\n value of a is %d",a);
printf("\n value of a is %d",*(&a));
printf("\n value of a is %d",*b);
printf("\n value of a is %d",**c);
printf("\n value of b and address of a=%u",b);
printf("\n value of c and address of b=%u",c);
printf("\n address of a=%u",&a);
printf("\n address of b=%u",&b);
printf("\n address of a=%u",*c);
printf("\n address of b=%u",&b);
printf("\n address of b=%u",c);
printf("\n address of c=%u",&c);
return(0);
}
OUTPUT:
value of a is 22
value of a is 22
value of a is 22
value of a is 22
value of b and address of a=3215158316
value of c and address of b=3215158312
address of a=3215158316
address of b=3215158312
address of a=3215158316
address of b=3215158312
address of b=3215158312
address of c=3215158308
PROGRAM FOR SWAPPING OF NUMBER USING CALL BY REFERENCE:
#include<stdio.h>
int main()
{
void swap (int*a,int*b);
int a,b;
printf("Enter a,b");
scanf("%d %d",&a,&b);
swap(&a,&b);
printf("After swapping %d %d\n",a,b);
return(0);
}
void swap (int*a,int*b)
{
int t;
t=*a;
*a=*b;
*b=t;
}
OUTPUT:
Enter a,b 30 11
After swapping 11 30
PROGRAM TO PRINT THE STRING IN REVERSE ORDER USING POINTER:
#include<stdio.h>
#include<string.h>
int main()
{
char a[50];
void reverse(char *);
printf("\n Enter the String: ");
scanf("%s",a);
reverse(a);
printf(" \n After reversing the String is : \n");
puts(a);
}
void reverse(char *string)
{
char *lp = string; /*left pointer*/
char *rp = &string[strlen(string)-1]; /*right pointer*/
char tmp;
while(lp < rp)
{
tmp = *lp;
*lp = *rp;
*rp = tmp;
lp++;
rp--;
}
}
OUTPUT:
Enter the String: kumaran
After reversing the String is :
naramuk
PROGRAM TO ALTER ALLOCATED MEMORY:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
char *p;
p=(char *)malloc(6);
strcpy(p,"MADRAS");
printf("memory contains:%s\n",p);
p=(char *)realloc(p,7);
strcpy(p,"CHENNAI");
printf("memory now contains:%s\n",p);
free(p);
}
OUTPUT:
memory contains:MADRAS
memory now contains:CHENNAI
PROGRAM TO PRINT VARIABLE FROM MEMORY ADDRESS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int *a, *n,size;
printf("enter the size...");
scanf("%d", &size);
n =(int*)malloc(size*sizeof(int));
printf("Address of the first byte is ....%u\n", n);
printf("Enter the values....");
for(a=n;a<n+size; a++)
scanf("%d", a);
printf("Printing the values....\n");
for(a=n+size-1;a>=n;a--)
printf("%d is stored in address %u \n", *a,a);
return(0);
}
OUTPUT:
enter the size...2
Address of the first byte is ....144461832
Enter the values....54
21
Printing the values....
21 is stored in address 144461836
54 is stored in address 144461832
PROGRAM TO MULTIPLY TWO MATRICES:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *a[20], *b[20], *c[20];
int i, j,r1, c1, r2, c2;
void get();
void display();
void mul();
printf(" Enter size of matrix A:\n");
scanf("%d %d", &r1, &c1);
printf(" Enter size of matrix B:\n");
scanf("%d %d", &r2, &c2);
if(c1 != r2)
{
printf("\n Multiplication not Possible \n");
printf("Column of matrix A are not equal to Rows of matrix B \n");
}
else
{
printf("\n Multiplication Possible \n");
for(i=0,j=0;i<r1,j<r2;i++,j++)
{
a[i]=(int*)malloc(c1*sizeof(int));
b[j]=(int*)malloc(c2*sizeof(int));
c[i]=(int*)malloc(c2*sizeof(int));
}
printf("Enter matrix A(%d X %d) elements \n", r1, c1);
get(a,r1,c1);
display(a,r1,c1);
printf("\n");
printf("Enter matrix B(%d X %d) elements \n", r2, c2);
get(b,r2,c2);
display(b,r2,c2);
printf("\n");
mul(a,b,c,r1,c1,c2);
printf(" The Product of matrix C(%d X %d) \n", r1, c2);
display(c,r1,c2);
}
return (0);
}
void get(x,r,c)
int *x[10],r,c;
{
int i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",x[i]+j);
}
void display(x,r,c)
int *x[10],r,c;
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%5d",*(x[i]+j));
printf("\n");
}
}
void mul(a,b,c,row,m,col)
int *a[10],*b[10],*c[10],row,m,col;
{
int i,j,k;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
*(c[i]+j)=0;
for(k=0;k<m;k++)
*(c[i]+j)=*(c[i]+j)+(*(a[i]+k))*(*(b[k]+j));
}
}
}
OUTPUT:
Enter size of matrix A:
3
3
Enter size of matrix B:
3
3
Multiplication Possible
Enter matrix A(3 X 3) elements
9
8
7
6
5
4
3
2
1
987
654
321
Enter matrix B(3 X 3) elements
1
2
3
4
5
6
7
8
9
123
456
789
The Product of matrix C(3 X 3)
90 114 138
54 69 84
18 24 30
PROGRAM TO CREATE A FILE AND READ THE CONTENT:
#include<stdio.h>
int main()
{
char empname[25];
int empno;
char deptname[25];
FILE *fptr;
fptr = fopen("employee.txt","w");
printf("Enter the Employee Name, Number and hisDepartment Name \n");
scanf("%s %d %s",empname,&empno,deptname);
fprintf(fptr,"%s%d%s", empname,empno,deptname);
fptr=fopen("employee.txt","r");
fscanf(fptr,"%s %d %s",empname,&empno, deptname);
printf("\nEmployee Details");
printf("\n---------------------");
printf("\n Employee Name : %s",empname);
printf("\n Employee Number : %d",empno);
printf("\n Department Name : %s \n \n", deptname);
printf(“--------------------------------------------”);
return(0);
}
OUTPUT:
Enter the Employee Name, Number and hisDepartment Name
kumaran 4777 cse
Employee Details
-------------------------------------
Employee Name : kumaran
Employee Number : 4777
Department Name : cse
--------------------------------------
PROGRAM TO COPY FILE CONTENT FROM ONE FILE TO ANOTHER:
#include<stdio.h>
int main()
{
FILE *fp1, *fp2;
int ch;
char fname1[30], fname2[30];
printf("Enter Source File:");
scanf("%s", fname1);
printf("Enter Destination File:");
scanf("%s", fname2);
fp1 = fopen(fname1,"r");
fp2 = fopen(fname2,"w");
if(fp1==NULL)
{
printf("Cannot Open the file %s for reading \n", fname1);
}
else if(fp2==NULL)
{
printf("Cannot Open the file %s for writing \n",
fname2);
}
else
{
ch=getc(fp1);
while(ch != EOF)
{
putc(ch, fp2);
ch = getc(fp1);
}
fclose(fp1);
fclose(fp2);
printf("Files Sucessfully Copied \n");
}
return(0);
}
OUTPUT:
Enter Source File:ref.c
Enter Destination File:cse a
Files Sucessfully Copied
PROGRAM TO DELETE A FILE:
#include<stdio.h>
int main()
{
char file[80];
printf("File to Delete : ");
scanf("%s",file);
if(remove(file) == 0)
printf("Removed %s. \n", file);
else
printf("Not able to remove, File not Found \n");
return 0;
}
OUTPUT:
File to Delete : cse a
Removed cse.
PROGRAM TO RENAME A FILE:
#include<stdio.h>
int main()
{
char oldname[80], newname[80];
printf("File to Rename: ");
scanf("%c",oldname);
printf("New Name : ");
scanf("%c",newname);
if(rename(oldname,newname) == 0)
printf("Renamed %s to %s.\n",
oldname,newname);
else
printf("Not able to rename, File not Found \n");
return 0;
}
OUTPUT:
File to Rename:cse a
New name:cse b
Renamed cse a to cse b
No comments:
Post a Comment