Вы находитесь на странице: 1из 49

# PRACTICAL 6

Aim: Write a C language program to study the use of function and the recurrence

function. 1) To create menu driven program using function to compute a. Addition b. Subtraction c. Multiplication d. Division e. Power f. Factorial g. L.C.M h. G.C.D 2) Modifying the above program by using the file inclusion 3) s 4) WAP using the recursive function to find the factorial of a numbers 5) WAP using the recursive function to find the Fibonacci series 6) WAP using the recursive function to add n number starting from 1.
Software used: Turbo C 3.0

Objective: Menu Driven Program: a) If the user select 1 then it perform Addition and enter the two number then it will return the result of Addition. b) If the user select 2 then it perform Subtraction and enter the two number then it will return the result of Subtraction. c) If the user select 3 then it perform Multiplication and enter the two number then it will return the result of Multiplication. d) If the user select 4 then it perform Division and enter the two number then it will return the result of Division. e) If the user select 5 then it perform Power, and enter the two number then it will return the result of Power. f) If the user select 6 then it perform Factorial, and enter the number then it will return the result of Power. g) If the user select 7 then it perform L.C.M, and enter the two number then it will return the result of L.C.M

h) If the user select 8 then it perform GCD., and enter the two number then it will return the result of GCD. Swapping of two numbers: a) When the user select 1,and enter the number 2 and 3,then it perform swapping using the third variable and after the called the value becomes 3 and 2. b) When the user select 2,and enter the number 2 and 3,then it perform swapping without using the third variable and after the called the value becomes 3 and 2. Recursion: Factorial of a number: If the user enters the number as 5 then factorial of a number will be 120. Fibonacci series: If the user enters the number as 5 then Fibonacci series will be 1 1 2 3 5. Sum of 1st n number: If the user enters the number as 10 then the result will be 55 Theory: Function: A function can be defined as the number of the statement code grouped into a single logical unit. The function can be reuse again and again. Syntax: The function can be define as: return type function name(argument type argument name,..) Passing the argument: The argument can be pass into function by using call by value and call by reference. Call by Value: i. In call by value the copy of the actual parameters are pass to the formal variable. ii. Any change made into the value of the formal variable is not reflected into the main function. Call by Reference: i. In call by reference the address of the actual parameters are pass to the formal variable. ii. Any change made into the value of the formal variable is reflected into the main function.
Code 1:

#include<stdio.h>

#include<conio.h> void add(int,int); void sub(int,int); void multi(int,int); void div(int,int); void power(int,int); void fact(int); void lcm(int,int); void gcd(int,int); void main() { int menu,a,b; clrscr(); printf("--------MENU-------\n"); printf("1 Addition\n"); printf("2 Subtraction\n"); printf("3 Multiplication\n"); printf("4 Division\n"); printf("5 Power\n"); printf("6 Factorial\n"); printf("7 LCM\n"); printf("8 GCD\n"); printf("Enter your choice\n"); scanf("%d",&menu); switch (menu) { case 1: printf("Enter two numbers\n"); scanf("%d %d",&a,&b); add(a,b); break; case 2: printf("Enter two numbers\n"); scanf("%d %d",&a,&b); sub(a,b); break; case 3: printf("Enter two numbers\n"); scanf("%d %d",&a,&b); multi(a,b); break;

case 4: printf("Enter two numbers\n"); scanf("%d %d",&a,&b); div(a,b); break; case 5: printf("Enter two numbers\n"); scanf("%d %d",&a,&b); power(a,b); break; case 6: printf("Enter the number\n"); scanf("%d",&a); fact(a); break; case 7: printf("Enter two numbers\n"); scanf("%d %d",&a,&b); lcm(a,b); break; case 8: printf("Enter two numbers\n"); scanf("%d %d",&a,&b); gcd(a,b); break; default: printf("Enter Valid Choice"); } getch(); } void add(int x,int y) { printf("The sum is %d ",x+y); } void sub(int x,int y) { printf("The Difference is %d ",(y<x)?x-y:y-x); }

void multi(int x,int y) { printf("The multiplication is %d ",x*y); } void div(int x,int y) { printf("The division is %d ",x/y); } void power(int x,int y) { int i,ex=1; for(i=1;i<=y;i++) { ex=ex*x; } printf("Power is %d",ex); } void fact(int x) { int i,f=1; for(i=x;i>1;i--) { f=f*i; } printf("The factorial is %d ",f); } void lcm(int x,int y) { int i,j; i=x>y?x:y; for(j=1;;j++) { if ((i*j)%x==0 && (i*j)%y==0) { printf("lcm is %d",i*j); break; } } }

void gcd(int x,int y) { int i; if(x>y) i=y; else i=x; for(;i>=1;i--) { if(x%i==0 && y%i==0) printf("GCD is %d ",i); break; } } Output:

Code 2:

## #include<stdio.h> #include<conio.h> #include"C:\TC\BIN\109c\P6.h"

switch (menu) { case 1: printf("Enter two numbers\n"); scanf("%d %d",&a,&b); add(a,b); break;

case 6:

## void power(int x,int y) { int i,ex=1;

for(i=1;i<=y;i++)

## void fact(int x) { int i,f=1; for(i=x;i>1;i--) { f=f*i; } printf("The factorial is %d ",f); }

void lcm(int x,int y) { int i,j; i=x>y?x:y; for(j=1;;j++) { if ((i*j)%x==0 && (i*j)%y==0) {

## printf("lcm is %d",i*j); break; } } }

void gcd(int x,int y) { int i; if(x>y) i=y; else i=x; for(;i>=1;i--) { if(x%i==0 && y%i==0) printf("GCD is %d ",i); break; } }

Output:

Code 3:

#include<stdio.h> #include<conio.h> void swap(int *x,int *y); void main() { int a,b; clrscr(); printf("Enter two numbers\n"); scanf("%d %d",&a,&b); swap(&a,&b); getch(); }

void swap(int *x,int *y) { *x=*x + *y; *y=*x - *y; *x=*x - *y; printf("The swapped numbers are %d %d",*x,*y); //return 1; } /* a=a+b b=a-b a=a-b */ Output:

Code 3.a:

## #include<stdio.h> #include<conio.h> void swap(int *x,int *y);

void main() { int a,b; clrscr(); printf("Enter two numbers\n"); scanf("%d %d",&a,&b); swap(&a,&b); getch(); } void swap(int *x,int *y) { int t; t=*x; *x=*y; *y=t; printf("The swapped numbers are %d %d",*x,*y); } Output:

Code 4:

## #include<stdio.h> #include<conio.h> int fact(int);

void main()

{ int i; clrscr(); printf("Enter the number\n"); scanf("%d",&i); printf("The factorial of the number is %d ",fact(i));

getch(); }

## int fact(int n) { if(n==1) return 1; else return(n*fact(n-1)); }

Output:

Code 6:
#include<stdio.h> #include<conio.h> void fibo(int,int,int); void main() {

int a=0,b=1,c=0; clrscr(); printf("Enter the limiting number for Fibonacci"); scanf("%d",&c); fibo(a,b,c); getch(); } void fibo(int x,int y,int z) { int f3; if(x==0) { printf("%d ",x); printf("%d ",y); } f3=x+y; x=y; y=f3; printf("%d ",f3); if(f3<z) fibo(x,y,z); }

Output:

Code 6:
#include<stdio.h> #include<conio.h> int sum(int n); void main() { int i,b; clrscr(); printf("Enter the number\n"); scanf("%d",&i); b=sum(i); printf("sum of numbers till %d = %d ",i,b); getch(); }

## int sum(int n) { if(n>=1) return(n+(sum(n-1))); else return 0; }

Output:

Conclusion:

Function can be used to separate out our function code from the main function. So, it is easy to understand the code and it provide the code reusability facility to the developer.

PRACTICAL 7
Aim: Write a C language program to demonstrate array 1. 2. 3. 4. 5. 6.

To find max and min of a number. To search element from an array. To sort an array. For addition of two matrix. For multiplication of two matrix. To remove duplicate element from an array.
Software used: Turbo C 3.0

Objective:
1. Maximum and Minimum:

If the user enters three numbers as 5, 6, 9 then largest will be 9 and smallest will be 5.
2. Searching an element:

If we enter number as 21 then it will search element within array Element to determine whether element is present or not. 3. Sort an array: If we enter an element such as 1,3,2,4,5 then it will sort it in ascending order and return result as 1,2,3,4,5.

4.

5.

## Multiplication of two matrixes: It performs multiplication on 2-D array.

6. Removing duplicating element: If we enter an element such as 1, 2,2,4,5 then it will remove duplicate element and return result as 1,2,4,5.

Theory: Array is a collection similar data type stored adjacent memory location. The position of an element is specified by subscript. The array subscript start with 0 not with 1.

## Syntax : Datatype Arrayname[Arraysize]; { Statements; }

Code 1 : #include<stdio.h> #include<conio.h> void main() { int a[5],i,min,max; clrscr(); printf("Enter any 5 numbers\n"); for(i=0;i<5;i++) { scanf("%d",&a[i]); } max=a[0]; min=a[0]; for(i=0;i<5;i++) { if(a[i]<min) { min=a[i]; } if(a[i]>max) { max=a[i]; } } printf("Minimum number is %d\n",min); printf("Maximum number is %d",max); getch(); }

Output:

Code 2: #include<stdio.h> #include<conio.h> void main() { int num,n,i; int array[20]; clrscr(); printf("Enter the size of the array\n"); scanf("%d",&n); printf("Enter the number to be searched\n"); scanf("%d",&num); printf("Now enter %d array elements\n",n); for(i=0;i<n;i++) { scanf("%d",&array[i]); } for(i=0;i<n;i++) { if(array[i]==num) { printf("Number Found at position %d.",i+1); break; } } getch(); }

Output:

Code 3: #include<stdio.h> #include<conio.h> void main() { int a[5],i,j,n,t; clrscr(); printf("How many array elements do you want to enter ?\n"); scanf("%d",&n); printf("Enter array elements\n"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } }

## } printf("\nSorted Array\n"); for(i=0;i<n;i++) { printf("%d \n",a[i]); } getch(); } Output:

Code 4: #include<stdio.h> #include<conio.h> void main() { int a[3][3],b[3][3],c[3][3],i,j; clrscr(); printf("Enter First Matrix\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&a[i][j]); } printf("\n"); } printf("Enter Second Matrix\n"); for(i=0;i<3;i++) {

for(j=0;j<3;j++) { scanf("%d",&b[i][j]); c[i][j]=a[i][j]+b[i][j]; } printf("\n"); } printf("Addition of Matrices\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%d",c[i][j]); } printf("\n"); } getch(); } Output:

## Code 5: #include<stdio.h> #include<conio.h> void main() {

int a[3][3],b[3][3],c[3][3],i,j,k; clrscr(); printf("Enter First Matrix\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&a[i][j]); } printf("\n"); } printf("Enter Second Matrix\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&b[i][j]); } printf("\n"); } for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j]=0; for(k=0;k<3;k++) { c[i][j]=a[i][k]*b[k][j]+c[i][j]; } } printf("\n"); } printf("Multiplication of Matrices\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%d",c[i][j]); } printf("\n"); } getch(); }

Output:

Code 6: #include<stdio.h> #include<conio.h> void main() { int a[5],i,j; clrscr(); printf("Enter 5 Array elements\n"); for(i=0;i<5;i++) { scanf("%d",&a[i]); } for(i=0;i<5;i++) { for(j=i+1;j<5;j++) { if(a[i]==a[j]) { a[j]=0; }

## } } //printing the array for(i=0;i<5;i++) { printf("\n%d\n",a[i]); } getch(); } Output:

Conclusion: Hence we conclude that by using array we can store multiple value for a

single variable:

PRACTICAL 8
Aim: Write a C language program to study the file handling technique

1. To count no of vowels, consonant and spaces in a given string. 2. To perform string copy, string length, string reverse, string concatenate operation using standard library function. 3. To perform string copy, string length, string reverse, string concatenate operation without using standard library function. 4. To check a given string is palindrome or not using standard library function. 5. To check substring is present in a given string or not with & without standard library function. 6. To display line of text in following way. I/P string: Jack and Jill went up the hill O/P string: hill the up went Jill and Jack
Software used: Turbo C 3.0

Objective: 1. To count no of vowels, consonant and spaces: If we enter any string then it will count the no of vowels , consonant and spaces in a given string. 2. Copy string, string length, string reverse, string concatenate: To copy, finding the length, reversing and concatenating a given string using standard library functions like strcpy, strlrn, strrev and strcat respectively. 3. Copy: It will copy one string into another string. Length: It will give length of the string. Reverse: It will reverse the string. Concatenate: Append one string at the end of another string. 4. String is palindrome or not: If we enter string such as mam then string is palindrome.

5. String present or not: Finding a particular string i.e. A part of string in the main string using and without using standard library functions.

## 6. Reversing a string: Reversing a given string in a special format.

Theory: String is a sequence of character. It is also called character array. String ends

## Syntax: Char string_name[size of string]=characters of string;

Code 1:

#include<stdio.h> #include<conio.h> #include<string.h> void main() { int i,l,sp=0,vo=0,con=0; char a[120]; clrscr(); printf("Enter the string\n"); gets(a); l=strlen(a); for(i=0;i<l;i++)

{ if(a[i]==' ') sp++; else if(a[i]!=' ') { switch(a[i]) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': vo++; break; /*case '0': case '1': case '2': case '3': case '4': case '5':

case '6': case '7': case '8': case '9': d++; break;*/ default : con++; } } } printf("Spaces= %d\nVowels= %d\nConsonents= %d",sp,vo,con); getch(); }

Output:

Code 2:

## #include<stdio.h> #include<conio.h> #include<string.h> void main()

{ char a[100],b[100],e[100],f[100]; int c,d; clrscr(); printf("Enter first string\n"); gets(a); //scanf("%[^\n]",a); printf("Enter second string\n"); gets(b); //scanf("%[^\n]",b); strcpy(f,a); c=strlen(a); d=strlen(b); printf("Length \na: %d\nb:%d\n",c,d); printf("Reverse string of string a: %s\n",strrev(a)); strrev(a); printf("Concatenation of two strings:%s\n",strcat(a,b)); printf("Copying a to new string c\n"); printf("Contents of c are :%s\n",strcpy(e,f)); getch(); } Output:

Code 3:

## #include<stdio.h> #include<conio.h> void main() { int i=0,j=0,k=0,m=0,c=0;

char name[20],strcon[20],str1[20],strcopy[20],strrev[20]; clrscr(); printf("\n enter the string1:"); gets(name); printf("\n enter the string2:"); gets(str1); while(name[i]!='\0') { strcon[k]=name[i]; k++; i++; } while(str1[j]!='\0') { strcon[k]=str1[j]; k++; j++; } strcon[k]='\0'; printf("concatenating strings="); puts(strcon); i=0; while(name[i]!='\0') { strcopy[m++]=name[i++]; } strcopy[i]='\0'; printf("\n copied string =%s",strcopy); i=0; while(name[i]!='\0') { i++; c++; } printf("\nLength of string=%d",c); i=c-1; j=0; while(i>=0) { strrev[j]=name[i]; j++; i--; } strrev[j]='\0';

## printf("\n reverse of a string is="); puts(strrev); getch(); }

Output:

Code 4:

#include<stdio.h> #include<conio.h> #include<string.h> void main() { int i; char str[20],p[20]; clrscr(); printf("Enter the string:"); gets(str); i=strcmp(str,strrev(str)); if(i==0) { printf("It is a palindrome string%s",str); } else { printf("It is not a palindrome string%s",str); } getch(); }

Output:

Code5: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char s1[20]; char s2[20]; char *found=NULL; clrscr(); printf("Enter the 1st string:"); gets(s1); printf("Enter the 2nd string:"); gets(s2); found=strstr(s1,s2); if(found) { printf("substring is present"); } else { printf("substring is not present"); } getch(); } Output:

Code 6: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[200], b[200]; int i,j,k; clrscr(); printf("Enter a string of max 150 chars:\n"); gets(a); for(j=0,i=strlen(a);i>=0;i--,j++) { b[j]=a[i]; if(a[i]==' '||i==0) { for(k=j;k>=0;k--) printf("%c",b[k]); j=0; } } getch(); } Output:

Conclusion: Hence we conclude that we have used string using standard library function

and without using library function to perform operation like copy, length, reverse concatenate of string.

PRACTICAL 9
Aim: Write a C language program to study various structures. 1. To find the addition of two complex number. 2. To compare two dates. 3. For passing structure member to function. 4. For array of structures. 5. For pointer to structure. Software used: Turbo C 3.0

Objective: 1. Addition of two complex number: If we enter two complex number such as 1+2i and 3+4i then it will result as 4+6i.

2. Compare two dates: If we enter two dates such as 2/5/2000 and 2/5/2000 then it will result whether two date are same or different.

3. Passing Structure member to function: A structure member can be treated just like any other variable. For e.g. An integer structure variable can be treated just as an integer. Thus structure members can be passed to function, like ordinary variables.

4. Array of structures: It is possible to declare an array of structures. The array will have individual structures as its elements. An array of structures can be declared similar to declared structures variables.

5. Pointer to structures: We can create pointer pointing to structures. To access structures element using pointer we required -> operator. We cant access member using dot (.) operator if we are using pointer to structures.
Theory: It is a collection of heterogeneous elements into single entity is called structures.

## Syntax: Structure structure_name { Structure members; };

Code 1: #include<stdio.h> #include<conio.h> struct complex { int real; int img; }; void main() { struct complex c1,c2,c3; clrscr(); printf("Enter first complex no\n"); scanf("%d %d",&c1.real,&c1.img); //c2 printf("Enter second complex no\n"); scanf("%d %d",&c2.real,&c2.img);

## c3.real=c1.real+c2.real; c3.img=c1.img+c2.img; //display printf("Addition of complex nos\n"); printf("%d+%di",c3.real,c3.img); getch(); }

Output:

Code 2: #include<stdio.h> #include<conio.h> struct date { int day; char month[20]; int year; }; void main() { struct date d1,d2; int d; clrscr(); //Date 1 printf("Enter first date in the format DAY MONTH(ENter Month NAME) YEAR \n"); scanf("%d%s%d",&d1.day,&d1.month,&d1.year); //Date 2 printf("Enter second date in the format DAY MONTH(ENter Month NAME) YEAR \n"); scanf("%d%s%d",&d2.day,&d2.month,&d2.year); //comparing d=stricmp(d1.month,d2.month); if((d1.day==d2.day)&&(d1.year==d2.year)&&(d==0)) printf("Both dates are same");

## else printf("Both dates are not same"); getch(); } Output:

Code 3: #include<stdio.h> #include<conio.h> struct person { char name[20]; int salary; int days; }emp; int compute(int d,int s) { if(d>=25) s+=500; return s; } void main() { clrscr(); printf("Enter name\n"); scanf("%s",&emp.name); printf("Enter salary\n"); scanf("%d",&emp.salary); printf("Enter number of days\n"); scanf("%d",&emp.days); emp.salary=compute(emp.days,emp.salary); printf("Employee details\n"); printf("Name\tDays\tSalary\n"); printf("%s\t%d\t%d",emp.name,emp.days,emp.salary); getch(); }

Output:

Code 4: #include<stdio.h> #include<conio.h> struct employee { int no; char name[20]; char addr[20]; int sal; }; void main() { struct employee e[3]; int i; clrscr(); for(i=0;i<3;i++) { printf("Enter number name address salary \n"); scanf("%d%s%s%d",&e[i].no,&e[i].name,&e[i].addr,&e[i].sal); } for(i=0;i<3;i++) { printf("Number\tName\tAddress\tSalary\n"); printf("%d\t%s\t%s\t%d\n",e[i].no,e[i].name,e[i].addr,e[i].sal); } getch(); } Output:

Code 5:

#include<stdio.h> #include<conio.h> struct stud { char name[20]; int m1,m2; }; void increase(struct stud *ps1) { ps1->m1+=5; ps1->m2+=5; } void main() { struct stud s1; clrscr(); printf("Enter Details: Name & 2 marks\n"); scanf("%s %d %d",&s1.name,&s1.m1,&s1.m2); printf("Name=%s\n M1=%d\n M2=%d\n",s1.name,s1.m1,s1.m2 );

## increase(&s1); printf("Name=%s\n M1=%d\n M2=%d\n",s1.name,s1.m1,s1.m2 ); getch(); }

Output:

Conclusion: Hence we conclude that we have used structure using Passing Structure

## member to function, Array of structures, Pointer to structures.

PRACTICAL 10
Aim: Write a C language program to study the file handling technique 1. To display the content of a file on screen. 2. To copy the content of one file into another file 3. To copy the contents of one file into another by using the command line arguments Software used: Turbo C 3.0

Objective: 1. Writing and reading file: It will create new file and will write into new file and then it will read the content from existing file

2. Writing and reading file using command argument: It will create new file and will write into new file and then it will read the content from existing file using command line arguments.

Theory: The file handling function are easy to use, powerful and complete. The use of

function is used to manipulate files. The basic I/P-O/P function to read from keyboard and write to the screen and close function for closing file.

Syntax: File *fp; fp=Function name(File name, File mode); Function of file: Fopen():Create a new file. Open an existing file for use Fclose: Close a file which has been opened for use. Getc():Read a character from a file. Putc():write a character to a file. Getw():Read an integer from a file. Putw():write an integer to a file. Fprint():writes a set of data values to a file. Fscanf():Reads a set of data values from a file. Ftell():gives the current position in the file Fseek():Sets the position to a desired point in the file. Rewind: Sets the position to the beginning of the file. Code1: #include<stdio.h> #include<conio.h> void main() { char c; FILE *fp; clrscr(); printf("Enter data to Input in file\n"); fp=fopen("info","w"); while((c=getchar())!=EOF) putc(c,fp); fclose(fp); printf("Content in file:\n"); fp=fopen("info","r"); while((c=getc(fp))>0) { printf("%c",c); } fclose(fp); getch(); }

Output:

Code1.a: #include<stdio.h> #include<conio.h> void main(int argc,char *argv[]) { char c; FILE *fp; clrscr();

## fp=fopen(argv[1],"r"); while((c=getc(fp))!=EOF) printf("%c",c); fclose(fp); getch(); } Output:

Code2:

#include<stdio.h> #include<conio.h> void main() { char c; FILE *fp1,*fp2; clrscr(); fp1=fopen("info","r"); fp2=fopen("target","w"); printf("Copying content from 1 file to another:\n"); printf("Content in new file:\n"); while((c=getc(fp1))!=EOF) { fprintf(fp2,"%c",c); } fclose(fp1); fclose(fp2);

## fp2=fopen("target","r"); while((c=getc(fp2))!=EOF) { printf("%c",c); } fclose(fp2); getch(); }

Output:

Code 3:

#include<stdio.h> #include<conio.h> void main(int argc, char *argv[]) { char c; FILE *fp1,*fp2; clrscr(); fp1=fopen(argv[1],"r"); fp2=fopen(argv[2],"w"); while((c=getc(fp1))!=EOF) { fprintf(fp2,"%c",c); } fclose(fp1); fclose(fp2); fp2=fopen(argv[2],"r"); while((c=getc(fp2))!=EOF) { printf("%c",c);

## } fclose(fp2); getch(); } Output:

Conclusion: Hence we include that we have used file handling technique for writing and