Sei sulla pagina 1di 91

CONTINENTAL COLLEGE 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: Manjeet 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.

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 concept of call by reference. W.A.P to illustrate the concept

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

of call by value. 31. 32 33 W.A.P to calculate factorial of a number using 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.

72 74 77 79 82 86 88 90

34.

35. 36. 37.

1. W.A.P to add two numbers

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

OUTPUT

2.W.A.P to find average of three number.

#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 a 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; circumference= 2*pi*r; printf("Area of a circle=%f",area); printf("circumference of a area=%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,r,t"); scanf("%d%d%d",&p,&r,&t); si=(p*r*t)/100; printf("si=%f",si); getch(); }

13

OUTPUT

14

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

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

15

OUTPUT

16

7. W.A.P to swap two numbers without using 3rd variable.

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

17

OUTPUT

18

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

#include<stdio.h> #include<conio.h> void main() { float c,f; clrscr(); printf("Enter the temp in centigrade"); scanf("%f",&c); f=(1.8*c+32); printf("Temp in fahrenhiet is %f",f); getch(); }

19

OUTPUT

20

Control Statements

21

9. W.A.P to print grade of a student 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; 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 find greatest of three number.

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

{ if(b>c)
26

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

27

OUTPUT

28

12. W.A.P to display the color according to 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("\n 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:
29

printf("\n Colour is White"); 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 no. using while statement.

#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 calculate average of n nos. using do-while statement.

#include<stdio.h> #include<conio.h> void main() { int sum=0,n,i; float avg; clrscr(); 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 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 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.AP to find average of n nos. 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 elements one 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 two matrices.

#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(j=0;j<m;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("\t"); } printf("\n"); } getch(); }

48

OUTPUT

49

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


#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("Order of Matrix B:\n"); scanf("%d%d",&p,&q); if(n==p) { printf("Enter A's elements:\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++) { c[i][j]=0; for(k=0;k<n;k++) { c[i][j]= c[i][j]+a[i][k]*b[k][j]; } } printf("The resultant martix is\n"); 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 to 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 no. of element"); scanf("%d",&n); printf("\nEnter the elements one by one"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nEnter the item to be searched"); scanf("%d",&item); for(i=0;i<n;i++) { if(a[i]==item) { loc= i; printf("\n%d is present at loc %d",item,loc+1); count++; } }
53

if(count!=0) printf("\nThe no.is present %d times",count); else printf("Item is not present in the list"); getch(); }

54

OUTPUT

55

String

56

23. W.A.P to calculate the 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 length of the string =%d",strlen(str)); getch(); }

57

OUTPUT

58

24. W.A.P tocalculate the copy of 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 enteressrting is"); puts(source); printf("\n The copied string is"); puts(dest); getch(); }

59

OUTPUT

60

25. W.A.P to calculate concatenation of 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, blank); strcat(result, s3); strcat(result, blank); printf("\n The resultant string is...\n"); puts(result); getch(); }

61

OUTPUT

62

26. W.A.P to calculate the reserve 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 reserve is %s", strrev(str)); getch(); }

63

OUTPUT

64

Functions

65

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

#include<stdio.h> #include<conio.h> void main() { int x,y,z,add(); printf("\n Enter the values of x,y:"); scanf("%d%d",&x,&y); x= add(x,y); printf("Added value is %d",x); 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(float,float); clrscr(); printf("\nEnter the value of A and B:"); scanf("%f%f",&a,&b); printf("Result is= %f",mul(a,b)); getche(); } floatmul(float x,float y) { float z; z=x*y; return(z); }

68

OUTPUT

69

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


#include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("\n Enter the two numbers"); 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 concept of call by value.


#include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("\n Enter two numbers"); 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; }

72

OUTPUT

73

31. W.A.P to illustrate the concept of factorial of 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.

#include<stdio.h> #include<conio.h> void main() { struct library { char title[20]; char name[20]; int pages; float price; }book1; printf("Enter the title,name,pages&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 three subjects of n student 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; float avg; clrscr(); printf("Enter the no. of student"); 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=(float)total/3;
79

printf("\n AVERAGE Marks of %dth student is=%f",i+1,avg); } getch(); }

80

OUTPUT

81

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


#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 the bank:"); scanf("\n %d",&n); printf("\n How many account number in the branch:"); scanf("\n %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("\n%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 the branch name, account number :"); scanf("\n %s%d",cust[i].acc[j].b_name,&cust[i].acc[j].account_no); } } printf("\n Customer Date 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:%s",cust[i].acc[j].b_name); printf("\n Account number is:%d",cust[i].acc[j].account_no); } } getch();
83

} .

OUTPUT

84

POINTERS

85

35.W.A.Pto show arithmetic operations by using pointer.


#include<stdio.h> #include<conio.h> void main() { int *p1,*p2,*p3; int i=1,j=2; 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+2; if(*p1>=*p2) { printf("%d%d%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.17; 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 st address %u",p,&p); printf("\n %f is stored at address %u",q,&q); getche(); }

88

OUTPUT

89

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


#include<stdio.h> #include<conio.h> void main() { int arr[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

Potrebbero piacerti anche