Vous êtes sur la page 1sur 70

Practical file On C language

Submitted to: Mrs. Pooja

Submitted by: Gaurav BCA I Roll no.

Index: Sr. no. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. Program name
Program to find the sum of two numbers Program to check whether given no. is odd/even Program to find the greatest of three numbers Program to find the percentage of marks obtained Program to find the roots of quadratic equation Program to add, subtract, multiply, divide two numbers using switch Program to find the average of n numbers using while loop Program to find the factorial using while loop Program to print Fibonacci series using do while loop Program to print palindrome number using do while loop Program to print 1,2,3...n using for loop Program to print asterisk (*) using for loop Program to calculate factorial using function Program to print the Fibonacci series using recursion Program to find the greatest of three numbers using function Program to search a given number from the given list using linear search Program to search a given number from the given list using binary search Program to sort a given list of numbers using bubble sort Program to find the sum of two matrices Program to find the product of two matrices Program for swapping numbers using pointer Program to find the average of n numbers using arrays Program to demonstrate use of address of operator (&) and indirection operator (*) for different purposes Program to demonstrate array elements are stored in contiguous memory location Program to sort the elements of an array using pointer Program to interchange value of two numbers using call by reference Program to show operators work in case of pointers Program to show how strings are initialized Program to copy a string to another using library function strcpy()
2

Page no. Signature


4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60

30. 31. 32. 33. 34.

Program to reverse a given string without using library function Program to concatenate two strings without using library function Program to sort the characters of string alphabetically Program to access the member of structure in which we have pointer to a structure Program to calculate marks obtained by 10 students in different subjects

62 64 66 68 69

1. /* TITLE:Sum Of Two Numbers */


#include<stdio.h> #include<conio.h> void main() { int a,b,sum; clrscr(); printf("\n Enter The Value Of a:"); scanf("%d",&a); printf("\n Enter The Value of b:"); scanf("%d",&b); sum=a+b; printf("\n Sum of two numbers is:%d",sum); getch(); }

Output

2. /* TITLE:To check whether given number is odd/even */


#include<stdio.h> #include<conio.h> void main() { int n,c; clrscr(); printf("\n Enter a number:"); scanf("%d",&n); c=n%2; if (c==0) printf("\n Number is EVEN"); else printf("\n Number is ODD"); getch(); }

Output

3. /* TITLE:Greatest Of Three numbers */


#include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("\n Enter the value of a:"); scanf("%d",&a); printf("\n Enter the value of b:"); scanf("%d",&b); printf("\n Enter the value of c:"); scanf("%d",&c); if(a>b) { if(a>c) printf("\n %d is greatest i.e. a",a); else printf("\n %d is greatest i.e. c",c); } else { if(b>c) printf("\n %d is greatest i.e. b",b); else printf("\n %d is greatest i.e. c",c); } getch(); }

Output

4. /* TITLE:Percentage of marks obtained */


#include<stdio.h> #include<conio.h> void main() { int a,b,c,d,e,f,sum; float per; clrscr(); printf("\n Enter marks in COM:"); scanf("%d",&a); printf("\n Enter marks in PBI:"); scanf("%d",&b); printf("\n Enter marks in PCS:"); scanf("%d",&c); printf("\n Enter marks in MATHS:"); scanf("%d",&d); printf("\n Enter marks in ENG:"); scanf("%d",&e); printf("\n Enter marks in C:"); scanf("%d",&f); sum=(a+b+c+d+e+f); printf("\n Total Marks=%d\n",sum); per=(a+b+c+d+e+f)/6; printf("\n Percentage= %f\n",per); if(per>=90) printf("\n Grade A"); else if(per>=80) printf("\n Grade B"); else if(per>=70) printf("\n Grade C"); else if(per>=60) printf("\n Grade D"); else printf("\n Grade E"); getch(); }

10

Output

11

5. /* TITLE:Roots Of Quadratic equation */


#include<math.h> #include<stdio.h> #include<conio.h> void main() { int a,b,c; float x1,x2,d; clrscr(); printf("\n Enter value of a:"); scanf("%d",&a); printf("\n Enter value of b:"); scanf("%d",&b); printf("\n Enter value of c:"); scanf("%d",&c); d=(b*b)-(4*a*c); if(d==0) { x1=x2=(-b)/(2*a); printf("\n Roots are equal \n\n x1= %f \n\n x2= %f",x1,x2); } if(d<0) { printf("\n Roots are imaginary"); } if(d>0) { x1=((-b)+sqrt(d))/(2*a); x2=((-b)-sqrt(d))/(2*a); printf("\n Roots are "); printf("\n\n x1=%f",x1); printf("\n\n x2=%f",x2); } getch(); }

12

Output

13

6. /* TITLE:To add, subtract, multiply, divide two numbers using switch */

#include<stdio.h> #include<conio.h> void main() { int a,b,choice,c; clrscr(); printf("\n Enter two numbers:"); scanf("%d%d",&a,&b); printf("\n 1. Addition"); printf("\n 2. Subtraction"); printf("\n 3. Multiplication"); printf("\n 4. Division"); printf("\n\n Enter your choice:"); scanf("%d",&choice); { switch(choice) { case 1:c=a+b; printf("\n Sum is %d",c); break; case 2:c=a-b; printf("\n Difference is %d",c); break; case 3:c=a*b; printf("\n Mulitplication is %d",c); break; case 4:c=a/b; printf("\n Quotient is %d",c); } } getch(); }

14

Output

15

7. /* TITLE: Average of n no's using while loop */

#include<stdio.h> #include<conio.h> void main() { int n,i=1; float sum=0,avg,x; clrscr(); printf("\n Enter the value of n:"); scanf("%d",&n); while(i<=n) { printf("\n x="); scanf("%f",&x); sum+=x; i++; } avg=sum/n; printf("\n Avearage is %f",avg); getch(); }

16

Output

17

8. /* TITLE: Factorial using while loop */


#include<stdio.h> #include<conio.h> void main() { int n,i=1; long int fact=1; clrscr(); printf("\n Enter a number:"); scanf("%d",&n); while(i<=n) { fact=fact*i; i++; } printf("\n The value of Factorial is %ld",fact); getch(); }

18

Output

19

9. /* TITLE: Print fibonacci series using do while loop */

#include<stdio.h> #include<conio.h> void main() { int a=0,b=1,c=1,n,count=3; clrscr(); printf("\n Enter the value of n:"); scanf("%d",&n); if(n==1) printf("0"); if(n==2) printf("1"); do { c=a+b; a=b; b=c; count++; } while(count<=n); if(c!=0) printf("\n %dth number is %d",n,c); getch(); }

20

Output

21

10. /* TITLE: Palindrome number using do while */


#include<stdio.h> #include<conio.h> void main() { int rem,rev,m,n,z; clrscr(); printf("\n Enter Lower Limit:"); scanf("%d",&m); printf("\n Enter Upper Limit:"); scanf("%d",&n); do { z=m;rem=0;rev=0; while(z>0) { rem=z%10; z=z/10; rev=(rev*10)+rem; } if(m==rev) { printf("\n\n %d is palindrome",m); } m=m+1; } while(m<=n); getch(); }

22

Output

23

11. /* TITLE: Print 1,2,3...'n' using for loop */


#include<stdio.h> #include<conio.h> void main() { int i,n,j,b=1; clrscr(); printf("\n Enter value of n:"); scanf("%d",&n); for(i=0;i<=n;i++) { for(j=1;j<=i;j++) { printf(" %d ",b); b++; } printf("\n\n"); } getch(); }

24

Output

25

12. /* TITLE: Print asterisk (*) using for loop */


#include<stdio.h> #include<conio.h> void main() { int i,j,n; clrscr(); printf("\n Enter how many Lines:"); scanf("%d",&n); printf("\n"); for(i=0;i<n;i++) { for(j=0;j<=i;j++) { printf(" * "); } printf("\n"); } getch(); }

26

Output

27

13. /* TITLE: Calculate factorial using function */


#include<stdio.h> #include<conio.h> long int fact(int n); main() { int n; long int k; clrscr(); printf("\n Enter the value of n="); scanf("%d",&n); k=fact(n); printf("\n The value of n! is=%ld",k); getch(); } long int fact(int x) { int i; long int prod = 1; if(x==0 || x==1) prod=1; else for(i=2;i<=x;i++) prod=prod*i; return(prod); }

28

Output

29

14. /* TITLE: Print Fibonacci series using recursion */


#include<stdio.h> #include<conio.h> int fib(int m); main() { int i,n; clrscr(); printf("\n Enter the value of n :"); scanf("%d",&n); for(i=1;i<=n;i++) printf("\n %d\n",fib(i)); getch(); } int fib(int m) { if(m==1 || m==2) return(1); else return(fib(m-1)+fib(m-2)); }

30

Output

31

15. /* TITLE: Greatest of 3 numbers using functions */


#include<stdio.h> #include<conio.h> comp(int,int,int); void main() { int a,b,c; clrscr(); printf("\n Enter value of a:"); scanf("%d",&a); printf("\n Enter value of b:"); scanf("%d",&b); printf("\n Enter value of c:"); scanf("%d",&c); comp(a,b,c); getch(); } comp(int x, int y, int z) { if(x>y) { if(x>z) printf("\n %d is Greatest i.e. a",x); else printf("\n %d is Greatest i.e b",z); } else { if(y>z) printf("\n %d is Greatest i.e. b",y); else printf("\n %d is Greatest i.e c",z); } }

32

Output

33

16. /* TITLE: Search a given number from a given list using linear search */
#include<stdio.h> #include<conio.h> void main() { int a[20],n,i,item,loc,count=0; clrscr(); printf("\n Enter number of elements : "); scanf("%d",&n); printf("\n Enter numbers : "); for(i=1;i<=n;i++) scanf("%d", &a[i]); printf("\n Enter item to be searched : "); scanf("%d", &item); for(i=1;i<=n;i++) { if (a[i] == item) { loc=i; printf("\n %d present at loc %d",item,loc); count++; } } if (count != 0) { printf("\n\n The number is present %d times",count); } else printf("\n Item is not present"); getch(); }

34

Output

35

17. /* TITLE: Search a given number from list using binary search */
#include<stdio.h> #include<conio.h> void main() { int a[20],n,i,loc=-1; int beg,end,mid,item; clrscr(); printf("\n Enter number of elements : "); scanf("%d",&n); printf("\n Enter sorted numbers : "); for(i=1;i<=n;i++) scanf("%d", &a[i]); printf("\n Enter element to be searched : "); scanf("%d", &item); beg=1; end=n; while(beg<=end) { mid = (beg+end)/2; if (a[mid] == item) { loc=mid; printf("\n Number is at %d loc",loc); break; } else if (a[mid]>item) end=mid-1; else beg=mid+1; } if (loc == -1) printf("\n Item is not in the list\n"); getch(); }

36

Output

37

18. /* TITLE: Sort a given list of numbers using bubble sort */


#include<stdio.h> #include<conio.h> main() { int i,j,n,temp; int a[20]; clrscr(); printf("\n Enter the no. of elements : "); scanf("%d",&n); printf("\n Enter the elements: "); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n-1;i++) for(j=0;j<n-i-1;j++) if(a[j] > a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } printf("\n The sorted elements:\n\n "); for(i=0;i<n;i++) printf("%d\t",a[i]); getch(); }

38

Output

39

19. /* TITLE: Addition of two matrices */


#include<math.h> #include<stdio.h> #include<conio.h> void main() { int a[10][10],b[10][10],c[10][10],m,n,i,j; clrscr(); printf("\n Enter the order of matrix A and B:"); scanf("%d%d",&m,&n); printf("\n Enter the elements of matrix A:"); for(i=1;i<=m;i++) for(j=1;j<=n;j++) scanf("%d",&a[i][j]); printf("\n Enter the elements of matrix B:"); for(i=1;i<=m;i++) for(j=1;j<=n;j++) scanf("%d",&b[i][j]); printf("\n Sum of two matrices is:\n\n"); for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { c[i][j]=a[i][j]+b[i][j]; printf(" %4d",c[i][j]); printf(" "); } } printf("\n"); getch(); }

40

Output

41

20. /* TITLE: Product of two matrices*/


#include<stdio.h> #include<conio.h> main() { int a[10][10],b[10][10],c[10][10]; int i,j,k,m,n,p,q; clrscr(); printf("\n Order of Matrix A :"); scanf("%d%d",&m,&n); printf("\n Order of Matrix B :"); scanf("%d%d",&p,&q); if(n==p) { printf("\n Enter A's element :"); for(i=0;i<m;i++) for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } printf("\n Enter B's element :"); for(i=0;i<p;i++) for(j=0;j<q;j++) { scanf("%d",&b[i][j]); } for(i=0;i<m;i++) for(j=0;j<q;j++) { c[i][j]=0; for(k=0;k<n;k++) { c[i][j]+=a[i][k]*b[k][j]; } } } printf("\n Product of two matrices is:\n\n"); for(i=0;i<m;i++) { for(j=0;j<q;j++) { printf("%4d",c[i][j]); printf(""); } printf("\n"); } getch(); }
42

Output

43

21. /* TITLE: Swapping using pointers */


#include<stdio.h> #include<conio.h> void swap(int *,int *); main() { int a,b; clrscr(); a=10;b=25; printf("\n Before calling (in main) "); printf("a = %d, b= %d\n",a,b); swap(&a,&b); printf("\n After call2ing (in main) "); printf("a= %d, b= %d\n",a,b); getch(); } void swap(int *x,int *y) { int z; z= *x; *x = *y; *y = z; printf("\n After modification (in funciuton) "); printf("x = %d, y= %d\n",*x,*y); }

44

Output

45

22. /* TITLE: Average of n numbers using arrays */


#include<stdio.h> #include<conio.h> void main() { int n,i; float avg,d,sum=0; float x[100]; clrscr(); printf("\n Enter the value of n:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n Num x[%d]=",i); scanf("%f",&x[i]); sum+=x[i]; } avg=sum/n; printf("\n The average is=%f",avg); getch(); }

46

Output

47

23. /* TITLE: Use of address operator(&) and indirection operator(*) for different purposes */
#include<stdio.h> #include<conio.h> void main() { int i=5,j=2,*ptr; clrscr(); ptr=&i; printf("\n Address of i= %u",&i); printf("\n\n Value stored in ptr= %d",ptr); printf("\n\n Value pointed by ptr= %d",*ptr); *ptr=10; printf("\n\n Value of i= %d",i); j=*ptr; printf("\n\n Value of j= %d",j); getch(); }

48

Output

49

24. /* TITLE: Demonstrate array elements are stored in contiguous memeory location */
#include<stdio.h> #include<conio.h> void main() { int x[5]={20,30,40,50,60}; int i; clrscr(); for(i=0;i<5;i++) printf("\n \n x[%d]= %d, &x[%d]= %u",i,x[i],i,&x[i]); getch(); }

50

Output

51

25. /* TITLE: To sort the elements of an array using pointers */


#include<stdio.h> #include<conio.h> void main() { int i,n,*x; void reorder(int n,int *x); clrscr(); printf("\n How many numbers :"); scanf("%d",&n); x= (int*)(n*sizeof(int)); for(i=0;i<n;i++) { printf("\n i%d= ",i+1); scanf("%d",x+i); } reorder(n,x); printf("\n Reordered List :\n"); for(i=0;i<n;i++) printf("\n x = %d\n",*(x+i)); getch(); } void reorder(int n,int *x) { int i,j,temp; for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(*(x+i) > *(x+j)) { temp= *(x+j); *(x+j) = *(x+i); *(x+i) = temp; } }

52

Output

53

26. /* TITLE: Interchange value of two numbers using call be Reference */


#include<stdio.h> #include<conio.h> void main() { int a=40; int b=50; void swap(int *x,int *y); clrscr(); printf("\n Value of a= 40\n"); printf("\n Value of b= 50\n"); swap(&a,&b); printf("\n After Swapping\n"); printf("\n Value of a= %d\n",a); printf("\n Value of b= %d",b); getch(); } void swap(int *x,int *y) { int t; t = *x; *x = *y; *y = t; }

54

Output

55

27. /* TITLE: Show how operators work in case of pointers */


#include<stdio.h> #include<conio.h> int *p1,*p2,*p3; int i=1; int j=2; int a[5]={1,2,3,4,5}; main() { int m,n,x; clrscr(); p1=&i; p2=&j; p3=&a[0]; m=p1-p2; n=*p1+*p2; x=*p3+2; if(p1>=p2) printf("\n %d %d %d",m,n,x); if(p1<=p2) printf("\n %u %u %u",p1,p2,p3); getch(); }

56

Output

57

28. /* TITLE: To show how strings are initialized */


#include<stdio.h> #include<conio.h> void main() { char name1[]="PANKAJ"; char name2[10]="ANSHUMAN"; char name3[10]={'A','N','K','U','R','\0'}; clrscr(); printf("\n Various inputs made are \n\n "); printf("%s\n ",name1); printf("%s\n ",name2); printf("%s ",name3); getch(); }

58

Output

59

29. /* TITLE: Copy a string to another using library function strcpy() */


#include<stdio.h> #include<conio.h> void main() { char a[80],n[80]; clrscr(); printf("\n Enter a string:"); gets(a); strcpy(n,a); printf("\n String after copying:"); puts(a); getch(); }

60

Output

61

30. /* TITLE: To reverse a given string without using library function */


#include<stdio.h> #include<conio.h> void main() { char a[80],n[80]; int i,j,count=0; clrscr(); printf("\n Enter a string:"); gets(a); for(i=0;a[i]!='\0';i++) ++count; printf("\n Length of string:%d",count); for(i=count-1,j=0;i>=0;i--,j++) n[j]=a[i]; n[j]= '\0'; printf("\n\n Reversed string is:%s",n); getch(); }

62

Output

63

31. /* TITLE: To concatenate two strings without using library function */


#include<stdio.h> #include<conio.h> void main() { char a[80],c[80]=" amit"; int i,j,count=0; clrscr(); printf("\n Enter before concatenation:"); gets(a); for(i=0;a[i]!='\0';i++) ++count; for(j=count,i=0;a[i]!='\0';j++,i++) a[j]=c[i]; a[j]='\0'; printf("\n String after concatenation:%s",a); getch(); }

64

Output

65

32. /* TITLE: To sort the characters of the string alphabetically */


#include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[20]; int i,k,m,j,temp; clrscr(); printf("\n String before sorting:"); gets(a); k= strlen(a); for(j=1;j<=k-1;j++) for(m=0;m<k-j;m++) if(a[m]>a[m+1]) { temp=a[m]; a[m]=a[m+1]; a[m+1]=temp; } printf("\n String after sorting:"); puts(a); getch(); }

66

Output

67

33. /* TITLE: Access the member of structure in which we have pointer to a structure */
#include<stdio.h> #include<conio.h> struct date { int dd; int mm; int yy; }; struct record { int acc_no; struct date l_pay; }; main() { struct record b,*p=&b; clrscr(); printf("\n Enter the values \n"); scanf("%d%d%d%d",&p->acc_no,&p->1_pay.dd,&p->1_pay.mm,&p->1_pay.yy); printf("\n Entered Values are \n"); printf("\n %d %d %d %d",p->acc_no,p->1_pay.dd,p->1_pay.mm,p->1_pay.yy); getch(); }

68

34. /* TITLE: Calculate total marks obtained by 10 students in different subjects */


#include<stdio.h> #include<conio.h> struct student { int sub1; int sub2; int sub3; }; void main() { struct student s[10]; int i,total=1; clrscr(); for(i=1;i<=10;i++) { printf("\n"); printf("\n Enter marks in first subject:"); scanf("%d",&s[i].sub1); printf("\n Enter marks in second subject:"); scanf("%d",&s[i].sub2); printf("\n Enter marks in second subject:"); scanf("%d",&s[i].sub3); total=s[i].sub1+s[i].sub2+s[i].sub3; printf("\n Total marks of s[%d] student:%d",i,total); } getch(); }

69

Output

70

Vous aimerez peut-être aussi