• No results found

C Language

N/A
N/A
Protected

Academic year: 2021

Share "C Language"

Copied!
70
0
0

Loading.... (view fulltext now)

Full text

(1)

1

Practical file

On

C language

Submitted to: Submitted by:

Mrs. Pooja Gaurav

BCA I

Roll no.

(2)

2

Index:

Sr.

no.

Program name

Page no. Signature

1.

Program to find the sum of two numbers 4

2.

Program to check whether given no. is odd/even 6

3.

Program to find the greatest of three numbers 8

4.

Program to find the percentage of marks obtained 10

5.

Program to find the roots of quadratic equation 12

6.

Program to add, subtract, multiply, divide two numbers using switch

14

7.

Program to find the average of n numbers using while loop

16

8.

Program to find the factorial using while loop 18

9.

Program to print Fibonacci series using do while loop 20

10.

Program to print palindrome number using do while loop

22

11.

Program to print 1,2,3...’n’ using for loop 24

12.

Program to print asterisk (*) using for loop 26

13.

Program to calculate factorial using function 28

14.

Program to print the Fibonacci series using recursion 30

15.

Program to find the greatest of three numbers using function

32

16.

Program to search a given number from the given list using linear search

34

17.

Program to search a given number from the given list using binary search

36

18.

Program to sort a given list of numbers using bubble sort

38

19.

Program to find the sum of two matrices 40

20.

Program to find the product of two matrices 42

21.

Program for swapping numbers using pointer 44

22.

Program to find the average of n numbers using arrays 46

23.

Program to demonstrate use of address of operator (&) and indirection operator (*) for different purposes

48

24.

Program to demonstrate array elements are stored in contiguous memory location

50

25.

Program to sort the elements of an array using pointer 52

26.

Program to interchange value of two numbers using call by reference

54

27.

Program to show operators work in case of pointers 56

28.

Program to show how strings are initialized 58

29.

Program to copy a string to another using library function strcpy()

(3)

3

30.

Program to reverse a given string without using library function

62

31.

Program to concatenate two strings without using library function

64

32.

Program to sort the characters of string alphabetically 66

33.

Program to access the member of structure in which we have pointer to a structure

68

34.

Program to calculate marks obtained by 10 students in different subjects

(4)

4

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();

(5)

5

(6)

6

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();

(7)

7

(8)

8

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(); }

(9)

9

(10)

10

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();

(11)

11

(12)

12

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(); }

(13)

13

(14)

14

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(); }

(15)

15

(16)

16

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();

(17)

17

(18)

18

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();

(19)

19

(20)

20

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(); }

(21)

21

(22)

22

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(); }

(23)

23

(24)

24

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(); }

(25)

25

(26)

26

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(); }

(27)

27

(28)

28

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); }

(29)

29

(30)

30

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)); }

(31)

31

(32)

32

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); }

(33)

33

(34)

34

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();

(35)

35

(36)

36

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();

(37)

37

(38)

38

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();

(39)

39

(40)

40

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(); }

(41)

41

(42)

42

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(); }

(43)

43

(44)

44

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);

(45)

45

(46)

46

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();

(47)

47

(48)

48

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();

(49)

49

(50)

50

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();

(51)

51

(52)

52

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; } }

(53)

53

(54)

54

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; }

(55)

55

(56)

56

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(); }

(57)

57

(58)

58

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();

(59)

59

(60)

60

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(); }

(61)

61

(62)

62

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();

(63)

63

(64)

64

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();

(65)

65

(66)

66

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(); }

(67)

67

(68)

68

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();

(69)

69

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(); }

(70)

70

References

Related documents