Você está na página 1de 91

CONTINENTAL GROUP OF HIGHER STUDIES

PRACTICAL FILE OF PROGRAMMING IN C


Submitted to:
Mrs. Nidhi Bhardwaj Signature Mr. Gurdeep Singh Gill (H.O.D Computer Science) Signature

Submitted By: Name: Neha Rani Course: PGDCA Subject:- Programming using C Paper code:- PGDCA-106 Roll No: Semester: 1st
1

INDEX
Sr. No.
1. 2. 3. 4. 5. 6. 7.

Name of the Program


W.A.P to add two nos. W.A.P to find average of three nos. W.A.P to calculate area and circumference of a circle. W.A.P to calculate (a+b)2 W.A.P to find simple interest. W.A.P to swap two nos. using third variable. W.A.P to swap two nos. without using third variable W.A.P to convert temp. from centigrade to Fahrenheit. W.AP to print grade of a student using simple if statement. W.A.P to check whether given no. is even or odd. W.A.P to find greatest of three nos. W.A.P to print color code using switch statement. W.A.P to calculate average of n nos. using while. W.A.P to print Fibonacci series. W.A.P to print average of n nos. using do-while statement.
2

Page No. 5 7 9 11 13 15 17 19 22 24 26 29 32 34 36

Remarks

8.
9. 10. 11. 12. 13. 14. 15

16. 17.

W.A.P to find factorial of a no. W.A.P to print 1 23 456 7 8 9 10 W.A.P to print * ** *** W.A.P to print the nos. using 1.D array. W.A.P to add 2 matrices W.A.P to multiply 2 matrices. W.A.P to search an element using linear statement. W.A.P to calculate length of a string. W.A.P to copy of a string. W.A.P to concatenation of a string W.A.P to reserve of a string. W.A.P to illustrate the concept of function. W.A.P to illustrate the concept of function prototyping. W.A.P to illustrate the conceptof call by reference. W.A.P to illustrate the concept of call by value.
3

38 40

18.

42

19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30.

45 47 50 53 57 59 61 63 66 68 70 72

31. 32. 33.

34.

35. 36. 37.

W.A.P to illustrate the concept of factorial of recursion. W.A.P to read and write the library data by using structure. W.A.P to find average marks of three subjects of n student by using array of structure. W.A.P to handle bank customer data by using nesting of structure. W.A.Pto show arithmetic operations by using pointer. W.A.P to display the use of pointer. W.A.P to illustrate the concept of array with pointer.

74 77 79 82 86 88 90

1. W.A.P to calculate sum of two no.


#include<stdio.h> #include<conio.h> void main() { int a,b sum=0 clrscr(); printf("Enter the value of a"); scanf("%d",&a); printf("Enter the value of b"); scanf("%d",&b); sum=a+b; printf("sum=%d",sum); getch(); }

OUTPUT

2. W.A.P to calculate average of three numbers.


#include<stdio.h> #include<conio.h> void main() int a,b,c; float avg=0; clrscr(); printf("Enter the value of a,b,c"); scanf("%d%d%d",&a,&b,&c); avg=(a+b+c)/3; printf("Average=%f",avg); getch();
}

OUTPUT

3. W.A.P to calculate area and circumference of circle.


#include<stdio.h> #include<conio.h> #define pi 3.14 void main() { int r; float area =0, circumference=0; clrscr(); printf("Enter the value of r"); scanf("%d",&r); area= pi*r*r; printf("Area of a circle=%f",area); circumference=2*pi*r; printf("\ncircumference of a circle=%f",circumference); getch(); }

OUTPUT

10

4. W.A.P to calculate (a+b)2.


#include<stdio.h> #include<conio.h> void main() { int a,b,ans; clrscr(); printf("Enter the value of a"); scanf("%d",&a); printf("Enter the value of b"); scanf("%d",&b); ans=(a*a+b*b+2*a*b); printf("ans=%d",ans); getch(); }

11

OUTPUT

12

5.W.A.P to calculate Simple Interest.


#include<stdio.h> #include<conio.h> void main() { int p,r,t; float si; clrscr(); printf("Enter the value of p"); scanf("%d",&p); printf("Enter the value of r"); scanf("%d",&r); printf("Enter the value of t"); scanf("%d",&t); si=(p*r*t)/100; printf("si=%f",si); getch(); }

13

OUTPUT

14

6.W.A.P to calculate swap two numbers without using third variable.


#include<stdio.h> #include<conio.h> void main() { int a=10, b=20; clrscr(); printf("Befor interchanging the value is a=%d,b=%d",a,b); a=a+b; b=a-b; a=a-b; printf("After interchanging the valu a=%d,b=%d",a,b); getch(); }

15

OUTPUT

16

7.W.A.P to calculate swap with using third variable.


#include<stdio.h> #include<conio.h> void main() { int a=10,b=20,temp; clrscr(); printf("Before interchanging the value is a=%d,b=%d",a,b); temp=a; a=b; b=temp; printf("After interchanging the value of is a=%d,b=%d",a,b); getch(); }

17

OUTPUT

18

8. W.A.P to convert the temp. from centigrade to Fahrenheit.


#include<stdio.h> #include<conio.h> void main() { float c,f=0; printf("enter the temprature in centigrata"); scanf("%f",&c); f=(1.8*c+32); printf(temp in fahren heat is%f;f); getch(); }

19

OUTPUT

20

CONTROL STATEMENTS

21

9. W.A.P to calculate grade using Simple if statement.


#include<stdio.h> #include<conio.h> void main() { float per; clrscr(); printf("Enter the percentage"); scanf("%f",&per); if(per>80) { printf("Grade is A"); } getch(); }

22

OUTPUT

23

10. W.A.P to check whether a given number is even or odd.


#include<stdio.h> #include<conio.h> void main() { int a=0; clrscr(); printf("Enter the value of a"); scanf("%d",&a); if(a%2==0) printf("Number is Even"); else printf("Number is Odd"); getch(); }

24

OUTPUT

25

11. W.A.P to calculate greatest of three no.


#include<stdio.h>

#include<conio.h> void main() { int a,b,c; printf("Enter the value of a"); scanf("%d",&a); printf("Enter the value of b"); scanf("%d",&b); printf("Enter the value of c"); scanf("%d",&c); if(a>b) { if(a>c) { printf("Greatest is a"); } else { printf("Greatest is c"); } } else
26

{ if(b>c) { printf("Greatest is b "); } else

{ printf("Greatest is c"); } } getch(); }

27

OUTPUT

28

12. W.A.P to print color code using switch statement.


#include<stdio.h> #include<conio.h> void main() { int code; clrscr(); printf("\n MAIN MENU"); printf("\n1 For colour Red"); printf("\n2 for colour Green"); printf("\n3 for colour White"); printf("\n4 for colour Yellow"); printf("\n5 Enter the colour code"); scanf("%d",&code); switch(code) { case 1: printf("\n Colour is Red"); break; case 2: printf("\n Colour is Green"); break; case 3: printf("\n Colour is White");
29

break; case 4: printf("\n colour is Yellow"); break; default: printf("\n Colour does not find"); } getch(); }

30

OUTPUT

31

13.W.A.P to calculate average of n number using while.


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

32

OUTPUT

33

14. W.A.P to print Fibonacci series.


#include<stdio.h> #include<conio.h> void main() { int a=0,b=1,c,n,i; clrscr(); printf("Enter the value of n"); scanf("%D",&n); printf("Fibonacci series is"); printf("%d",a); printf("%d",b); for(i=0;i<n-2;i++) { c= a+b; a=b; b=c; printf("%d",c); } getch(); }

34

OUTPUT

35

15.W.A.P to print average of n numbers using do-while.


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

36

OUTPUT

37

16.W.A.P to find factorial of a number.


#include<stdio.h> #include<conio.h> void main() { int n,i,m; clrscr(); printf("\n Enter the number:"); scanf("%d",&n); m=1; for(i=1;i<n;i++) { m=m*i; } printf("\n Factorial of %d is =%d",n,m); getch(); }

38

OUTPUT

39

17.W.A.P to print 1 23 456 7 8 9 10


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

40

OUTPUT

41

18. W.A.P to print * ** *** ****


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

42

OUTPUT

43

Arrays

44

19.W.A.P to find average of number using 1-d array.


#include<stdio.h> #include<conio.h> void main() { int n,a[50],i,sum=0; float avg; clrscr(); printf("Enter the value of n"); scanf("%d",&n); printf("Enter the element on by one "); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) { sum=sum+a[i]; } printf("sum=%d",sum); avg= (sum/n); printf("Average=%f",avg); getch(); }

45

OUTPUT

46

20. W.A.P to add 2 matrix.


#include<stdio.h> #include<conio.h> void main() { int i,j,a[10][10],b[10][10],c[10][10],n,m; clrscr(); printf("\n Enter the number of rows and columns of matrix A and B:"); scanf("%d%d",&n,&m); printf("\n Enter the elements of matrix A:\n"); for(i=0;i<n;i++) { for(j=0;j<m;j++) { scanf("%d",&a[i][j]); } } printf("\n Enter the elements of matrix B:\n"); for(i=0;i<n;i++) { for(j=0;j<m;j++) { scanf("%d",&b[i][j]); }
47

} for(i=0;i<n;i++) for(i=0;j<n;j++) { c[i][j]= a[i][j]+b[i][j]; } } printf("\n The resultant matrix c is:\n"); for(i=0;i<n;i++) { for(j=0;j<m;j++) { printf("%d",c[i][j]); } printf("\n"); } getch(); }

48

OUTPUT

49

21.W.A.P to multiply two matrix.


#include<stdio.h> #include<conio.h> void main() { int a[10][10],b[10][10],c[10][10]; int i,j,m,n,p,q,k; clrscr(); printf("order of matrix A:\n"); scanf("%d%d",&m,&n); printf("\n order of matrix B:\n"); scanf("%d%d",&p,&q); if(n==p) { printf("Enter A's element:\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } printf("Enter B's element:\n"); for(i=0;i<p;i++) for(j=0;j<q;j++) {
50

scanf("%d",&b[i][j]); } for(i=0;i<m;i++) for(j=0;j<q;j++) { for(k=0;k<n;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } }} for(i=0;i<m;i++) { for(j=0;j<q;j++) { printf("%d",c[i][j]); printf(" "); } printf("\n"); } getch(); }

51

OUTPUT

52

22. W.A.P Search an element using linear search.


#include<stdio.h> #include<conio.h> void main () { int a[10],i,item,n,loc,count=0; clrscr(); printf("Enter the number of element "); scanf("%d",&n); printf("Enter the numbers one by one"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter the item to be searched"); scanf("%d",&item); for(i=0;i<n;i++) { if(a[i]==item) { loc=i; printf("item is at % d loc",loc+1); count++; } } if(count!=0)
53

printf("The number is present %d time",count); else printf("item is not present in the list"); getch(); }

54

OUTPUT

55

Strings

56

23. W.A.P to calculate length of a string.


#include<stdio.h> #include<conio.h> void main() { charstr[80]; clrscr(); printf("\n Enter a string"); gets(str); puts(str); printf("\n The lenght of the string =%d",strlen(str)); getch(); }

57

OUTPUT

58

24. W.A.P to calculate copy a string.


#include<stdio.h> #include<conio.h> void main() { char source[20],dest[20]; clrscr(); printf("\n Enter a string"); gets(source); strcpy(dest,source); printf("\n The entered string is ") puts(source); printf("\n The copied string is "); puts(dest); getch(); }

59

OUTPUT

60

25. W.A.P to calculate concatenation a string.


#include<stdio.h> #include<conio.h> void main() { char result[35],blank[]=" "; char s1[]= "Continental",s2[]= "College",s3[]= "Fatehgarh"; clrscr(); strcat(result,s1); strcat(result,blank); strcat(result,s2); strcat(result,s3); printf("\n The resulant string is ...\n"); puts(result); getch(); }

61

OUTPUT

62

26.W.A.P to calculate reverse of a string.


#include<stdio.h> #include<conio.h> void main() { charstr[20]; clrscr(); printf("\n Enter a string"); gets(str); printf("\n String entered is %s", str); printf("\n String after reverse is %s", strrev(str)); getch();
}

63

OUTPUT

64

Function

65

27.W.A.P to illustrate the concept of function.


#include<stdio.h> #include<conio.h> void main() { int x,y,z,add(); clrscr(); printf("\n Enter the value of x,y:"); scanf("%d%d",&x,&y); z=add(x,y); printf("added value is%d",z); getche(); } int add(p,q) intp,q; { int s; s= p+q; return(s); }

66

OUTPUT

67

28. W.A.P to illustrate the concept of function prototyping.


#include<stdio.h> #include<conio.h> void main() { float a,b,mul(); clrscr(); printf("\n Enter the value of A and B:"); scanf("%f%f",&a,&b); printf("Result is =%f", mul(a,b)); getche(); } floatmul(x,y) floatx,y; { float z; z= x*y; return(z); }

68

OUTPUT

69

29. W.A.P to illustrate the concept of passing by reference.


#include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("\n Enter the two number"); scanf("%d%d",&a,&b); exchange (&a,&b); printf("\n The exchanged contents are %d %d",a,b); getch(); } exchange(int*x, int*y) { int temp; temp=*x; *x=*y; *y=temp; }

70

OUTPUT

71

30. W.A.P to illustrate the passing by value.


#include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("\n Enter two number"); scanf("%d %d",&a,&b); exchange(a,b); printf("\n The exchanged contents are:%d %d",a,b); getch(); } exchange(intx,int y) { int temp; temp=x; x=y; y=temp; }

72

OUTPUT

73

31.W.A.P to illustrate factorial of a number using recursion .


#include<stdio.h> #include<conio.h> void main () { int f,n,factorial(); clrscr(); printf("Enter the value of n"); scanf("%d",&n); f=factorial(n); printf("Factorial of a number is %d",f); getch(); } int factorial(int a) { int m; if(a==1) { return(a); } else { m=a* factorial(a-1); return(m);
74

OUTPUT

75

Structures

76

32. W.A.P to read and write the library data by using structure in C.
#include<stdio.h> #include<conio.h> void main() { struct library { char title [20]; char name [20]; int pages; float price; } book1; clrscr(); printf("Enter the title, name,page and price of a book"); scanf("%s%s%d%f",book1.title,book1.name,&book1.pages,&book1.price); printf("\n name of book=%s",book1.title); printf("\n name of author = %s",book1.name); printf("\n no.of pages=%d",book1.pages); printf("\n price=%f",book1.price); getch(); }

77

OUTPUT

78

33. W.A.P to find average marks of 3 subjects of n students in a class by using array of structure.
#include<stdio.h> #include<conio.h> void main() { struct student { int sub1; int sub2; int sub3; } st[10]; int i,n,total; floatavg; clrscr(); printf("Enter the no.of students"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n Enter marks of three sub of %d the student:",i+1); total=0; scanf("%d%d%d",&st[i].sub1,&st[i].sub2,&st[i].sub3); total=st[i].sub1+st[i].sub2+st[i].sub3; avg=total/3;
79

printf("\n average marks of %d the student is= %f",i+1,avg); } getch(); }

80

OUTPUT

81

34. W.A.P to handle bank customer data by using nesting of structures.


#include<stdio.h> #include<conio.h> void main() { struct customer { char name[20]; char address[30]; int phone_no; struct account { char b_name[20]; int account_no; } acc[10]; } cust [20]; inti,j,n,m; clrscr(); printf("\n How many customer in bank:"); scanf("\n%d",&n); printf("\n How many account number in the branch:"); scanf("%d",&m); printf("\n Enter the customer data:"); for(i=0;i<n;i++)
82

{ printf("\n Enter customer name,address and phone no.:"); scanf("%s%s%d",cust[i].name,cust[i].address,&cust[i].phone_no); printf("\n Enter the bank branch data:"); for(j=1;j<=m;j++) { printf("\n Enter branch name,account number :"); scanf("\n %s%d%f",cust[i].acc[j].b_name,&cust[i].acc[j].account_no); } } printf(" \n Customer data is as:"); for(i=0;i<n;i++) { printf("\n customer name is:%s",cust[i].name); printf("\n customer address is:%s",cust[i].address); printf("\n customer phone no is :%d",cust[i].phone_no); printf("\n The bank branch data is:"); for(j=1;j<=m;j++) { printf("\n branch name is:%d",cust[i].acc[j].b_name); printf("\n Account number is :%d",cust[i].acc[j].account_no); } } getch();}
83

OUTPUT

84

pointer

85

35. W.A.P to show arithmetic operations using pointer.


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

86

OUTPUT

87

36. W.A.P to display the use of pointer.


#include<stdio.h> #include<conio.h> void main() { char ab; int r; float p,q; clrscr(); ab ='Z'; r = 22; p = 3.12; q = 5.7; printf("\n %c is stored at address %u" ,ab,&ab); printf("\n %d is stored at address %u" ,r,&r); printf("\n %f is stored at address %u" ,p,&p); printf("\n %f is stored at address %u" ,q,&q); getch(); }

88

OUTPUT

89

37. W.A.P to illustrate the concept of array with pointer.


#include<stdio.h> #include<conio.h> void main() { Intarr[5][2]={{1234,56}, {1212,33}, {1434,80}, {1312,78}, {1203,75}}; int i,j; clrscr(); for(i=0;i<5;i++) { printf("\n"); for(j=0;j<2;j++) { printf("\t%d",*(*(arr+i)+j)); } } getch(); }

90

OUTPUT

91

Você também pode gostar