You are on page 1of 38

C Lab Programs

By Tapasya Gutha in Gitam cse b Edit Doc 1. Write a program, to read X,Y coordinates of three points and then calculate the area of the triangle formed by them and print the coordinates of the three points and the area of the triangle. What will be the output from your program if the three given points are in a straight line? VARIABLES USED:

Variable name x1,x2,x3,y1,y2,y3 points area

datatype float float

purpose coordinates of three to store area of the triangle

PROGRAM: #include<stdio.h> #include<conio.h> #include<math.h> main() { float x1,x2,x3,y1,y2,y3; float area; printf("Enter the coordinates x1,y1\n"); scanf("%f%f",&x1,&y1); printf("Enter the coordinates x2,y2\n"); scanf("%f%f",&x2,&y2);

printf("Enter the coordinates x3,y3\n"); scanf("%f%f",&x3,&y3); area=0.5*(fabs(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))); printf("the coordinates (x1,y1) are %f,%f\n",x1,y1); printf("the coordinates (x2,y2) are %f,%f\n",x2,y2); printf("the coordinates (x3,y3) are %f,%f\n",x3,y3); printf("area of the triangle is %f",area); if(area= =0)

printf("\nthree given points are in the straight line"); getch(); }

OUTPUT: Enter the coordinates x1,y1 96 Enter the coordinates x2,y2 35 Enter the coordinates x3,y3 12 the coordinates (x1,y1) are 9.000000,6.000000 the coordinates (x2,y2) are 3.000000,5.000000 the coordinates (x3,y3) are 1.000000,2.000000 area=abs(0.5*((x1*y2+x2*y3+x3*y1)-(y1*x2+y2*x3+y3*x1)));

2. Write a program to find the roots of a quadratic equation using if else and switch statements. VARIABLES USED:

Variable name a,b,c D r1,r2 a1,a2

datatype float float float float

purpose coefficients of Quadratic equation to store determinant to store real roots to store imaginary roots

PROGRAM: #include<stdio.h> #include<conio.h> #include<math.h> main( ) { float a,b,c,D,r1,r2,a1,a2; int value; printf("Enter the values of a,b,c"); scanf("%f%f%f",&a,&b,&c); if(a==0) { printf("It is not a quadratic equation"); }

else { D=(b*b)-(4*a*c); if(D>0) value=1; else if(D<0) value=2; else value=3; switch(value) { case 1: printf("\nThe roots are real and distinct:"); r1=(-b+sqrt(D))/(2*a); r2=(-b-sqrt(D))/(2*a); printf("\nThe roots are:r1=%f\t r2=%f",r1,r2); break; case 2:printf("\nThe roots are imaginary and distinct:"); a1=(-b)/(2*a); a2= (sqrt(fabs(D)))/(2*a); printf("\nThe roots are r1=%f+i%f \t r2=%fi%f",a1,a2,a1,a2); break; case 3:printf("\nThe roots are real and same:");

r1=r2=(-b)/(2*a); printf("\nThe roots are r1=%f \t r2=%f",r1,r2); break; } } getch(); }

OUTPUT: Enter the values of a,b,c1 5 6 The roots are real and distinct The roots are:r1=-2.000000 r2=-3.000000

3. Write a program which generates one hundred random integers in the range of 1 to 100, store them in an array and then prints the average. Write three versions of the program using different loop constructs(e.g., for, while and do while) VARIABLES USED:

Variable name i a[100] total

data int int int

purpose to repeat loops to store random numbers to find total of 100 random numbers

PROGRAM:

//Using for loop: #include<stdio.h> #include<stdlib.h> #include <time.h> void main() { int i,a[100],total=0; randomize(); printf("\n Hundred random numbers between 1 and 100 are \n"); for(i=0;i<100;i++) { a[i]=random(100)+1; printf("%d\t",a[i]); total=total+a[i]; } printf("the average of 100 random numbers is %d ",total/100); }

//Using while loop: #include<stdio.h> #include<stdlib.h> #include <time.h> void main()

{ int i=0,a[100],total=0; randomize(); printf("\n Hundred random numbers between 1 and 100 are \n"); while(i<100) { a[i]=random(100)+1; printf("%d\t",a[i]); total=total+a[i]; i++; } printf("the average of 100 random numbers is %d ",total/100); }

//Using do-while loop: #include<stdio.h> #include<stdlib.h> #include <time.h> void main() { int i=0,a[100],total=0; randomize(); printf("\n Hundred random numbers between 1 and 100 are \n");

do { a[i]=random(100)+1; printf("%d\t",a[i]); total=total+a[i]; i++; }while(i<100); printf("the average of 100 random numbers is %d ",total/100); }

OUTPUT: Hundred random numbers between 1 and 100 are 95 76 42 45 73 93 5 53 91 16 82 13 50 61 33 20 55 14 56 12 73 1 87 85 38 61 26 39 43 45 12 81 32 36 100 22 74 20 68 52 91 9 57 85 57 96 25 199 96 91 75 69 26 47 29 14 76 45 40 72 100 9 86 76 57 32 82 3 64 5 11 6 20 83 42 5 90 52 10 55 2 9 96 30 42 39 50 82 6 2 45 51 76 80 97 56 33 60 80 94

the average of 100 random numbers is 49

4. Write a program for multiplication of square matrices

VARIABLES USED: Variable name x[ ],y[ ] elements z[ ] i,j,k datatype int int int purpose to store given matrices to store result matrix elements to repeat loops

PROGRAM: #include<stdio.h> #include<conio.h> void main( ) { void multi(int x[2][2],int y[2][2]); int x[2][2], y[2][2], i, j; printf("enter 4 elements in x :"); for(i=0; i<2; i++) for(j=0; j<2; j++) scanf("%d",&x[i][j]); printf("enter 4 elements in y :"); for(i=0; i<2; i++) for(j=0; j<2; j++)

scanf("%d",&y[i][j]); multi(x,y); getch(); } void multi(int x[2][2],int y[2][2]) { int i,j,z[2][2],k; for(i=0;i<2;i++) { for(j=0;j<2;j++) { z[i][j] =0; for(k=0; k<2;k++) z[i][j] = z[i][j] + x[i][k] * y[k][j]; } } printf("\n Elements in x matrix : \n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) printf("%d\t",x[i][j]); printf("\n"); } printf("\n Elements in y matrix : \n"); for(i=0;i<2;i++)

for(j=0;j<2;j++) printf("%d\t",y[i][j]); printf("\n");

} printf("\n Elements in z matrix : \n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) printf("%d\t",z[i][j]); printf("\n"); } }

OUTPUT: Enter the 4 elements in x: 1 1 1 1 Enter the 4 elements in x: 1 1 1 1

Elements in x matrix: 1 1 1 1

Elements in y matrix: 1 1 1 1

Elements in z matrix: 2 2 2 2

5. Write a program to find maximum and minimum elements with their positions in a given array and then sort the above array. VARIABLES USED:

Variable name x[ ] i,j t max,min maxpos,minpos positions

datatype int int int int int

purpose to store result matrix elements to repeat loops temporary variable to store max and min values to store max and min

PROGRAM: #include<stdio.h> #include<conio.h> main()

int x[5],i,j,t,max,min,maxpos,minpos;

printf("enter 5 elements"); for(i=0;i<=4;i++) scanf("%d",&x[i]); max=min=x[0]; for(i=1;i<=4;i++) { if(x[i]>max) { max=x[i]; maxpos=i; } if(x[i]<min) { min=x[i]; minpos=i; } } printf("max=%d is found at %d\n min=%d is found at %d",max,maxpos,min,minpos); printf("\n before sorting elements:"); for(i=0;i<=4;i++) printf("%d\t",x[i]); for(i=0;i<=3;i++)

{ for(j=i+1;j<=4;j++) { if(x[i]>x[j]) { t=x[i]; x[i]=x[j]; x[j]=t; } } } printf("\n after sorting elements:"); for(i=0;i<=4;i++) printf("%d\t",x[i]); getch(); } OUTPUT: Enter 5 elements:3 1 8 9 4 Max=9 is found at 3 Min=1 is found at 1 Before sorting elements:3 1 8 9 4 After sorting elements:1 3 4 8 9

6.Write a program to insert an element into an array VARIABLES USED:

Variable name a[10] array n i item pos

datatype int int int int int

purpose to store the size of the array to repeat the loop new item to be inserted to store the position

PROGRAM: #include<stdio.h> #include<conio.h> void main() { int a[10],i,n,pos,item; printf("Enter how many values u want:"); scanf("%d",&n); printf("Enter %d elements into array a:\n",n); for(i=0;i<n;i++) { scanf("%d",&a[i]);

} printf("Enter which element u want to insert:"); scanf("%d",&item); printf("Enter position for that element:"); scanf("%d",&pos); for(i=n;i>=pos;i--) { a[i]=a[i-1]; } a[pos-1]=item; printf("\nAfter insertion the array elements are:"); for(i=0;i<=n;i++) printf(" %d ",a[i]); getch(); }

OUTPUT: Enter how many values u want:5 Enter 5 elements into array a:12 4 8 45 89 Enter which element u want to insert:43 Enter position for that element:3

After insertion the array elements are: 12

43

45

89

7.Write a function for transposing a square matrix in place.(in place means that you are not allowed to have full temporary matrix) VARIABLES USED:

Variable name a[10][10] m,n i,j

datatype int int int

purpose to store the matrix to store the order of the matrix to repeat the loops

PROGRAM: #include<stdio.h> #include<conio.h> main() { int a[10][10],m,n,i,j; void trans(int a[10][10],int,int); printf("Enter Order of Matrix A:\n"); scanf("%d %d",&m,&n); if(m= =n) { printf("Enter The Elements into Matrix A:\n"); for(i=0;i<m;i+ +)

for(j=0;j<n;j++) scanf("%d",&a[i][j]); trans(a,m,n); } else printf("Not a Square Matrix"); getch(); } void trans(int a[10][10],int m,int n) { int i,j; printf("The Transpose of Matrix A is:\n"); for(i=0;i<n;i++) { for(j=0;j<m;j++) printf("%3d",a[j][i]); printf("\n"); } getch(); }

OUTPUT: Enter Order of Matrix A:3 3

Enter The Elements into Matrix A:1 2 3 4 5 6 7 8 9 The Transpose of Matrix A is: 147 258 369

8.Write a program to print Fibonacci series using functions.

VARIABLES USED:

Variable name n fib1,fib2 fib number i

datatype int

purpose to store given terms

int to store first,second temporary numbers int to store fibonacci

int

to repeat while loop

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

{ int fib1=0, fib2=1, n; printf("\n\t Enter number of terms - ") ; scanf("%d",&n); fibo(fib1,fib2,n); getch () ; } void fibo(int fib1, int fib2,int n) { int i=1,fib; /* generation of Fibonacci series */ do { printf("\t %d",fib1); fib=fib1+fib2; fib1=fib2 ; fib2=fib; i++; } while (i<=n) ; } OUTPUT: enter n:5

0 1 1 2 3

9.Write a program to find the factorial of a given number using recursion. VARIABLES USED:

Variable name n f factorial

datatype int int

purpose given number to store

PROGRAM: #include<stdio.h> #include<conio.h> void main( ) { int fact(int n); int n, f; printf(enter n :); scanf(%d,&n); f = fact(n); printf(factorial of the given no. is :%d,f); getch( ); }

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

OUTPUT: enter n : 5 factorial of the given no. is : 120

10.Write a program to find ncr " using non recursive function while finding the factorial value using recursion. VARIABLES USED: Variable name n,r result datatype int int purpose to find the ncr to store the result

PROGRAM: #include<stdio.h> #include<conio.h> int fact(int); main() { float ncr(int,int); int n,r; printf("Enter n and r values for ncr:"); scanf("%d%d",&n,&r); if(n<r) printf("ncr is not possible"); else printf("%dC%d is %.2f",n,r,ncr(n,r)); getch(); } float ncr(int n,int r) { float result; result=(float)fact(n)/(fact(n-r)*fact(r)); return (result); }

int fact(int n) { int f; if((n==0)||(n==1)) return 1; else f=n*fact(n-1); return f; }

OUTPUT: Enter n and r values for ncr:5 3 5C3 is 10.00

11.Write a program to find whether given string is palindrome or not without using string functions. VARIABLES USED: Variable name a[50],b[50] length i,j flag false datatype char int int int purpose to store the strings string length to repeat the loops to check for true or

PROGRAM: #include<stdio.h> #include<conio.h> main() { char a[50],b[50]; int length=0,i=0,j=0,flag; printf("Enter The String:"); scanf("%s",a); while(a[i]!='\0') { i++; } length=i; printf("String Length is:%d",length); for(i=length-1;i>=0;i--) { b[j]=a[i]; j++; } b[j]='\0'; for(i=0;i<length;i++) {

if(b[i]==a[i]) flag=0; else { flag=1; break; } } if(flag==0) printf("\nThe String is Palindrome"); else printf("\nThe String is Not Palindrome"); printf("\nThe Reverse String of %s is %s",a,b); getch(); }

OUTPUT: Enter The String:madam String Length is:5 The String is Palindrome The Reverse String of madam is madam

12.Given an array of strings write a program to sort the string in dictionary order. VARIABLES USED: Variable name s[ 5][20],t[15] i,j,n datatype char array int to store strings to repeat loop purpose

PROGRAM: #include<stdio.h> #include<string.h> void main() { char s[5][20],t[15]; int i,j,n; printf("how many strings do you want to sort?"); scanf("%d",&n); printf("\nenter the strings"); for(i=0;i<n;i++) scanf("%s",s[i]); for(i=1;i<n;i++) { for(j=1;j<n;j++)

{ if(strcmp(s[j-1],s[j])>0) { strcpy(t,s[j-1]); strcpy(s[j-1],s[j]); strcpy(s[j],t); } } } printf("the sorted strings are:\n"); for(i=0;i<n;i++) printf("\n %s",s[i]); } OUTPUT: how many strings do you want to sort?5 enter the strings Visakhapatnam Vijayawada Hyderabad Guntur Tirupati

The sorted strings are: Guntur

Hyderabad Tirupati Vijayawada Visakhapatnam

13.Develop a program to implement a structure to read and display the Name, Birthdate and salary of ten employers.

VARIABLES USED: Variable name name[20] day,month,year,salary details i int to repeat the loop datatype char int purpose to store the name to store the employee

PROGRAM: #include<stdio.h> #include<conio.h> struct employee { char name[20]; int day,month,year,salary; }; main()

{ int i; struct employee e[10]; for(i=0;i<10;i++) { printf("enter %d employee name:",i+1); scanf("%s",e[i].name); printf("enter %d employee birthdate(dd-mm-yyyy):",i+1); scanf("%d-%d-%d",&e[i].day,&e[i].month,&e[i].year); printf("enter %d employee salary:",i+1); scanf("%d",&e[i].salary); } for(i=0;i<10;i++) printf("\n%s employee birthdate is %d-%d-%d and his or her salary is %d",e[i].name,e[i].day,e[i].month,e[i].year,e[i].salary); getch(); }

OUTPUT: enter 1 employee name: aaaa enter 1 employee birthdate(dd-mm-yyyy): 1-1-2000 enter 1 employee salary: 1000 enter 2 employee name: bbbb enter 2 employee birthdate(dd-mm-yyyy): 1-2-2000

enter 2 employee salary: 2000 enter 3 employee name: cccc enter 3 employee birthdate(dd-mm-yyyy): 1-3-2000 enter 3 employee salary: 3000 enter 4 employee name: dddd enter 4 employee birthdate(dd-mm-yyyy): 1-4-2000 enter 4 employee salary: 4000 enter 5 employee name: eeee enter 5 employee birthdate(dd-mm-yyyy): 1-5-2000 enter 5 employee salary: 5000 enter 6 employee name: ffff enter 6 employee birthdate(dd-mm-yyyy): 1-6-2000 enter 6 employee salary: 6000 enter 7 employee name: gggg enter 7 employee birthdate(dd-mm-yyyy): 1-7-2000 enter 7 employee salary: 7000 enter 8 employee name: hhhh enter 8 employee birthdate(dd-mm-yyyy): 1-8-2000 enter 8 employee salary: 8000 enter 9 employee name: iiii enter 9 employee birthdate(dd-mm-yyyy): 1-9-2000 enter 9 employee salary: 9000 enter 10 employee name: jjjj

enter 10 employee birthdate(dd-mm-yyyy): 1-10-2000 enter 10 employee salary: 10000 aaaa employee birthdate is 1-1-2000 and his or her salary is 1000 bbbb employee birthdate is 1-2-2000 and his or her salary is 2000 cccc employee birthdate is 1-3-2000 and his or her salary is 3000 dddd employee birthdate is 1-4-2000 and his or her salary is 4000 eeee employee birthdate is 1-5-2000 and his or her salary is 5000 ffff employee birthdate is 1-6-2000 and his or her salary is 6000 gggg employee birthdate is 1-7-2000 and his or her salary is 7000 hhhh employee birthdate is 1-8-2000 and his or her salary is 8000 iiii employee birthdate is 1-9-2000 and his or her salary is 9000 jjjj employee birthdate is 1-10-2000 and his or her salary is 10000

14.Develop a program to display the Name, Marks in five subjects and total marks of ten students. (Using array of structures) VARIABLES USED: Variable name name[20] i Sub1,sub2,sub3,sub3,sub4,sub5 int datatype char int purpose student name to repeat loop five subject marks

PROGRAM: #include<stdio.h> main()

{ struct student { char name[20]; int sub1,sub2,sub3,sub4,sub5,total; }student[10]; int i; for(i=0;i<=9;i++) { printf("enter %d student name and marks in 5 subjects:\n",i+1); scanf("%s%d%d%d%d %d",student[i].name,&student[i].sub1,&student[i].sub2,&student[i].sub3,&student[ i].sub4,&student[i].sub5); student[i].total=student[i].sub1+student[i].sub2+student[i].sub3+student[i].sub4+st udent[i].sub5; } printf("\n\t\tThe Student Details"); printf("\n\t\t-------------------------"); for(i=0;i<=9;i++) { printf("\n\t\tname --- sub1 sub2 sub3 sub4 sub5 total\n");

printf("\t\t%s --- %d %d %d %d %d %d\n",student[i].name,student[i].sub1,student[i].sub2,student[i].sub3,student[i].sub 4,student[i].sub5,student[i].total); } getch();

} OUTPUT: enter 1 student name and marks in 5 subjects:aaa 10 10 10 10 10 enter 2 student name and marks in 5 subjects:bbb 20 20 20 20 20 enter 3 student name and marks in 5 subjects:ccc 30 30 30 30 30 enter 4 student name and marks in 5 subjects:ddd 40 40 40 40 40 enter 5 student name and marks in 5 subjects:eee 50 50 50 50 50 enter 6 student name and marks in 5 subjects:fff 60 60 60 60 60 enter 7 student name and marks in 5 subjects:ggg 70 70 70 70 70 enter 8 student name and marks in 5 subjects:hhh 80 80 80 80 80 enter 9 student name and marks in 5 subjects:iii 90 90 90 90 90 enter 10 student name and marks in 5 subjects:jjj 100 100 100 100 100 The Student Details ------------------------name aaa bbb ccc ddd eee fff ggg hhh --- sub1 ----------------10 20 30 40 50 60 70 80 sub2 sub3 sub4 sub5 total 10 20 30 40 50 60 70 80 10 20 30 40 50 60 70 80 10 20 30 40 50 60 70 80 10 20 30 40 50 60 70 80 10 20 30 40 50 60 70 80

iii jjj

-----

90 100

90 100

90 100

90 100

90 100

90 100

15.Develop a program to read and write to a file VARIABLES USED: Variable name *f1 c PROGRAM: #include<stdio.h> main() { FILE *f1; char c; printf("Data Input\n"); f1=fopen("INPUT","w"); while((c=getchar())!=EOF) putc(c,f1); fclose(f1); printf("\n Data Output\n\n"); f1=fopen("INPUT","r"); while((c=getc(f1))!=EOF) printf("%c",c); datatype FILE char purpose file pointer to read and write character

fclose(f1); }

OUTPUT: Data Input Good morning ctrl+z Data Output Good morning

16.Develop a program to create and count number of characters in a file VARIABLES USED: Variable name c co fp datatype int int FILE purpose to store the character to count the characters file pointer

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

FILE *fp; int c,co=0; printf("Enter the chars:\n"); printf("Press ctrl+Z to stop entry\n"); fp=fopen("enter.txt","w"); while((c=getchar())!=EOF) fputc(c,fp); fclose(fp); printf("\n"); fp=fopen("enter.txt","r"); while((c=fgetc(fp))!=EOF) { printf("%c",(char)c); ++co; } fclose(fp); printf("\nThe no of chars in the file are %d",co); getch(); }

OUTPUT: Enter the chars: Press ctrl+Z to stop entry

Gitam University Gitam University The no of chars in the file are 17

Like Follow Post Report November 12 at 10:28pm


o o

Sumanth Saggurthi, Abhinand Turlapati and Vinay Vithala like this.