0% found this document useful (0 votes)
42 views

C Programs - Unix

This document contains examples of various C programming concepts like functions, arguments, return values, dynamic memory allocation, file handling, structures, and more. It demonstrates how to define and call functions, allocate memory dynamically, read from and write to files, define structures and use them, and other programming techniques. The examples include functions to find the largest number, dynamically allocate memory, reverse a string, perform binary search, calculate factorials, Taylor series for cosine, matrix multiplication, and more.

Uploaded by

anand787
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

C Programs - Unix

This document contains examples of various C programming concepts like functions, arguments, return values, dynamic memory allocation, file handling, structures, and more. It demonstrates how to define and call functions, allocate memory dynamically, read from and write to files, define structures and use them, and other programming techniques. The examples include functions to find the largest number, dynamically allocate memory, reverse a string, perform binary search, calculate factorials, Taylor series for cosine, matrix multiplication, and more.

Uploaded by

anand787
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

FUNCTION WITH NO ARGUMENTS AND NO RETURN VALUES

#include<stdio.h>
void sum(void);
main()
{
sum();
}
void sum(void)
{
int num1, num2, num3;
printf("Enter two numbers: \n");
scanf("%d%d", &num1, &num2);
num3=num1+num2;
printf("Summation of %d and %d is %d \n", num1, num2, num3);
}

OUTPUT
Enter two numbers:
10
20
Summation of 10 and 20 is 30

FUNCTION WITH ARGUMENTS AND RETURN VALUES


#include<stdio.h>
#include<conio.h>
int largest(int, int);
void main()
{
int a,b,big;
printf("Enter two numbers : \n");
scanf("%d%d", &a, &b);
big=largest(a,b);
printf("Largest Element = %d", big);
}
int largest(int a1, int b1)
{
if(a1>b1)
return a1;
else
return b1;
}

OUTPUT
Enter two numbers
15
25
Largest Element = 25

DYNAMIC ALLOCATION OF MEMORY


#include<stdio.h>
main()
{
int *p;
p=(int *)malloc(sizeof(int));
if(p==0)
{
printf(" ERROR : Out of Memory \n");
return 1;
}
*p=5;
printf("Value of P = %d \n", *p);
printf("Address of P = %u \n", p);
free(p);
return 0;
}

OUTPUT
Value of P = 5
Address of P =134518200

ALLOCATE SPACE FOR A STRING DYNAMICALLY AND


PRINT THE STRING BACKWARDS
#include<stdio.h>
#include<string.h>
int main()
{
char *s;
register int i;
s=malloc(80);
if(!s)
{
printf("Memory request failed................. \n");
exit(1);
}
scanf("%s",s);
for(i=strlen(s)-1; i>=0; i--)
printf("%c",s[i]);
free(s);
return 0;
}

OUTPUT
Hai
iaH

BINARY SEARCH USING FUNCTION


#include<stdio.h>
#include<math.h>
int main()
{
int bsearch(int x[],int n,int s);
int x[20],i,n,s;
printf("How many numbers?");
scanf("%d",&n);
printf("Enter all numbers in the list");
for(i=0;i<n;i++)
scanf("%d",&x[i]);
printf("Enter the number to be searched:");
scanf("%d",&s);
if(bsearch(x,n,s))
printf("The number %d is present in the list",s);
else
printf("The number %d is not present in the list",s);
return 0;
}
int bsearch(int x[],int n, int s)
{
int i,j,flag=0,start,mid,end;
start=0;
end=n;
while(start < end && flag==0)
{
mid = (start + end)/2;
if(x[mid] >s)

end = mid;
else
if(x[mid]<s)
start = mid+1;
else
flag=1;
}
return(flag);
}

OUTPUT
How many numbers? 10
Enter all the numbers in the list
8
15
23
25
36
45
50
62
65
78
Enter the number to be searched: 36
The number 36 is present in the list
Enter the number to be searched: 42
The number 42 is not present in the list

FACTORIAL IN FINDING ncr USING FUNCTION


#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",ncr);
}
int fact(int k)
{
int i, p=1;
for(i=1;i<=k;i++)
p=p*i;
return(p);
}

OUTPUT
Enter the values of n and r: 5 3
Value of ncr = 10
Enter the values of n and r: 10 5
Value of ncr = 252

COSINE SERIES USING FUNCTION


#include<stdio.h>
#include<math.h>
int main()
{
float cosine(float x);
float x=0;
printf("\n x in degrees cos(x) ");
while (x<=180)
{
printf("\n \t %6.0f %6.2f", x,cosine(x));
x=x+30;
}
return 0;
}
float cosine(float x)
{
float s, term;
int i,k;
x=x*3.14/180;
s=0;
term=1;
i=0;
for(k=1;k<=15;k++)
{
s=s+term;
term=term*x*x*(-1) / ((i+1) * (i+2));
i=i+2;
}

return(s);
}

OUTPUT
---------------------------------------------------------------------x in degrees cos(x)
---------------------------------------------------------------------0 1.0
30 0.87
60 0.50
90 0.00
120 -0.50
150 -0.87
180 -1.00

PRODUCT OF MATRIX USING ARRAYS


#include<stdio.h>
#include<stdlib.h>
int main()
{
int *a[10],*b[10], *c[10], m,n,l,i,k,j;
for(i=0;i<10;i++)
{
a[i] = (int *) calloc (10,sizeof(int));
b[i] = (int *) calloc (10,sizeof(int));
c[i] = (int *) calloc (10,sizeof(int));
}
printf("\nFor A matrix");
printf("\nHow many rows and columns ?");
scanf("%d %d", &m,&n);
printf("\nEnter A matrix values");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
scanf("%d",(*(a+i)+j));
}
printf("\nFor B matrix");
printf("\nHow many rows and columns ?");
scanf("%d %d", &n,&l);
printf("\nEnter B matrix values");
for(i=0;i<n;i++)
for(j=0;j<l;j++)
scanf("%d", (*(b+i)+j));
for(i=0;i<m;i++)

for(j=0;j<l;j++)
{
*(*(c+i)+j)=0;
for(k=0;k<n;k++)
*(*(c+i)+j) = *(*(c+i)+j) + *(*(a+i)+k) * * (*(b+k)+j);
}
printf("\n Resultant matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<l;j++)
printf("%6d", *(*(c+i)+j));
printf("\n");
}
return 0;
}
OUTPUT
For A matrix
How many rows and columns ? 3 3
Enter A matrix values
321
-2 0 4
3 2 -1
For B matrix
How many rows and columns? 3 3
Enter B matrix values
6 4 -1
212
4 -5 4

Resultant Matrix is
26 9 5
4 -28 18
18 19 -3

CREATION & READING THE CONTENT OF A FILE


#include<stdio.h>
#include<ctype.h>
struct emp
{
int eno;
char ename[20];
float bpay;
};
FILE *efile;
int main()
{
struct emp erec;
int n,i;
efile = fopen("EMPLOY.DAT", "w");
printf("\nEnter the number of employees:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nEnter the %d employee details",i);
printf("\nEnter the employee number");
scanf("%d",&erec.eno);
printf("Employee Name");
scanf("%s",erec.ename);
printf("Basicpay");
scanf("%f",&erec.bpay);
fwrite(&erec,sizeof(erec),1,efile);
}
fclose(efile);

printf("\nAfter adding content to the file");


efile=fopen("EMPLOY.DAT","r");
printf("\n--------------------------------------------------");
printf("\n Emp.no Employee name Basic Pay");
printf("\n --------------------------------------------------\n");
fread(&erec,sizeof(erec),1,efile);
while (!feof(efile))
{
printf("\n %d \t %-20s %0.2f",erec.eno,erec.ename,erec.bpay);
fread(&erec, sizeof(erec),1,efile);
}
printf("\n --------------------------------------------------");
fclose(efile);
return 0;
}
OUTPUT
Enter the number of Employees: 2
Enter the 1 employee details :
Enter the Employee number: 101
Employee name: xxxx
Basic Pay: 65433.4
Enter the 2 employee details :
Enter the Employee number: 102
Employee name: yyyy
Basic Pay: 7000
After adding content to the file
----------------------------------------------------------Emp.no Employee name Basic Pay
---------------------------------------------------------101 xxxx 65433.4
102 yyyy 7000.0

---------------------------------------------------------APPENDING A RECORD TO THE FILE

#include<stdio.h>
#include<ctype.h>
struct emp
{
int eno;
char ename[20];
float bpay;
};
FILE *efile;
int main()
{
struct emp erec;
efile = fopen("EMPLOY.DAT", "a");
printf("\nEnter the Employee number");
scanf("%d",&erec.eno);
printf("Employee Name");
scanf("%s",erec.ename);
printf("Basicpay");
scanf("%f",&erec.bpay);
fwrite(&erec,sizeof(erec),1,efile);
fclose(efile);
printf(" After Appending the content of file");
efile=fopen("EMPLOY.DAT","r");
printf("\n--------------------------------------------------");
printf("\n Emp.no Employee name Basic Pay");
printf("\n --------------------------------------------------");
fread(&erec,sizeof(erec),1,efile);

while (!feof(efile))
{
printf("\n %d \t %-20s %0.2f",erec.eno,erec.ename,erec.bpay);
fread(&erec, sizeof(erec),1,efile);
}
printf("\n --------------------------------------------------\n");
fclose(efile);
return 0;
}

OUTPUT
Enter the Employee number: 103
Employee name: zzzz
Basic Pay: 12000
After Appending the content of file
----------------------------------------------------------Emp.no Employee name Basic Pay
---------------------------------------------------------101 xxxx 65433.4
102 yyyy 7000.0
103 zzzz 12000.0

You might also like