Próximo SlideShare
Cpds lab
Cargando en ... 3
1 de 36

### C lab manaual

1. PART- A 1a. WAP to find the area of the triangle #include<stdio.h> #include<conio.h> #include<math.h> void main() { float a,b,c,area,s;//s is semi perometer i.e. s=(a+b+c)/2 clrscr(); printf("enter the three sides of triangle"); scanf("%f%f%f",&a,&b,&c); if((a+b)>c&&(a+c)>b&&(b+c)>a) { s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)) ; printf("the area of given triangle is-->%f",area); } else printf("the sides given are invalid...enter vaild sides"); getch(); }
2. Output enter the three sides of triangle 1 1 1 the area of given triangle is-->0.433013 1b. Wap to find the area of the circle #include<stdio.h> #include<conio.h> void main() { float p=3.14,r,area; clrscr(); printf("Enter the value of radious = "); scanf("%f",&r); area=p*r*r; printf("nThe area of circle is = %f",area); getch(); }
3. Output Enter the value of radious = 2 The area of circle is = 12.560000 2. Wap to find the simple interest ,given the principle,time and rate of interest with appropriate validation #include<stdio.h> #include<conio.h> void main() { int p,t; float r,si; clrscr(); printf("n enter principle,rate and time"); scanf("%d%f%d",&p,&r,&t); si=(p*r*t)/100; printf("n simple interest:%f",si); getch(); } Output Enter the year which you know to check that it is leap year or not:->2008 The given year is leap year
4. 3. WAP to check whether the given year is leap year or not #include<stdio.h> #include<conio.h> void main() { int n; clrscr(); printf("n Enter the year which you know to check that it is leap year or not:->"); scanf("%d",&n); if((n%400==0)||(n%4==0)&&(n%100!=0)) { printf("n The given year is leap year"); } else { printf("n The given year is not a leap year"); } getch(); }
5. 4. WAP to find the root of the quadratic equation with appropriate message. //roots of the quadratic equation #include<stdio.h> #include<conio.h> #include<math.h> void main() { int a,b,c,d=0,rp,ip; float x1,x2,e; clrscr(); printf("n enter the coefficient of a,b and c"); scanf("%d%d%d",&a,&b,&c); if(a>0) { d=b*b-4*a*c; if(d==0) { printf("n roots are equal"); x1=x2=-b/(2*a); printf("n root 1=%f",x1); printf("n root 2=%f",x2);
6. } else if(d>0) { e=sqrt(d); printf("n roots are real"); x1=-(b+e)/(2*a); x2=-(b+e)/(2*a); printf("n root 1=%f",x1); printf("n root 2=%f",x2); } else { printf("root are imaginary"); } } else { printf("n this is not a quadratic equation"); } getch(); }
7. 6. WAP to find GCD and LCM of given two numbers #include<stdio.h> #include<conio.h> void main() { int u,v,temp,a,b,lcm,gcd; clrscr(); printf("n enter two number"); scanf("%d%d",&u,&v); a=u; b=v; while(v!=0) { temp=u%v; u=v; v=temp; } gcd=u; lcm=(a*b)/gcd; printf("n GCD of two number=%d",gcd); printf("n LCM of the two number=%d",lcm); getch(); }
8. 7. WAP to calculate sin(x) series. #include<stdio.h> #include<conio.h> void main() { int n,sign=1,i; float x,t,sum; clrscr(); printf("n enter value of x and n"); scanf("%f%d",&x,&n); x=(3.14 /180)*x; sum=t=x; for(i=1;i<n;i++) { sign=-1; t=sign*t*x*x/(2*i*(2*i+1)); sum=sum+t; } printf("sum of sin x series=%f",sum); getch(); }
9. 8. WAP to print all prime numbers between m and n #include<stdio.h> #include<conio.h> void main() { int m,n,i,j,prime=0; clrscr(); printf("n enter two number from which you want to check prime number"); scanf("%d%d",&m,&n); if(m<2) m=2; for(i=m;i<=n;i++) { prime=1; for(j=2;j<=i-1;j++) if (i%j==0) { prime=0; break; } if(prime) printf("n %dt",i);
10. } getch(); } 9. WAp to reverse a number and check whether it is palindrome or not #include<stdio.h> #include<conio.h> void main() { int n,d,r=0,u=0; clrscr(); printf("n enter a number"); scanf("%d",&n); u=n; while(n!=0) { d=n%10; r=(r*10)+d; n=n/10; } if (r==u) printf("n number is palindrome"); else printf("n number is not palindrome"); getch();
11. } 10 WAP to generate and print first n Fibonacci numbers using function. #include<stdio.h> #include<conio.h> void fibo(int); void main() { int n; clrscr(); printf("n enter the limit of the seriesn"); scanf("%d",&n); fibo(n); getch(); } void fibo(int n) { int a=0,b=1,c,i=3; printf("n fibonacci series=n"); if(n<1) printf("enter the valid limit"); else if(n==1) printf("0"); else if(n==2) printf("%dt%d",a,b);
12. else { printf("0 t 1"); while(i<=n) { c=a+b; printf("t%d",c); a=b; b=c; i++; } } } 11 WAP to find factorial of a given number usind recursive function. #include<stdio.h> #include<conio.h> int factorial(); void main() { int n; clrscr(); printf("n enter a numbern"); scanf("%d",&n); printf("n factorial=%d",factorial(n)); getch(); }
13. int factorial(int n) { if(n==0) { return(1); } else { return(n*factorial(n-1)); } } 12 WAP to convert upper case alphabets to lower case alphabets in the given string and vice versa. #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str[20]; int i; clrscr(); puts("enter a stringn"); gets(str); printf("n original string=%s",str); for(i=0;str[i]!='0';i++) {
14. if(str[i]>='A' && str[i]<='Z') { str[i]=str[i]+32; } else if(str[i]>='a' && str[i]<='z') { str[i]=str[i]-32; } } printf("n change string is:%s",str); getch(); } 13 write a program to read two strings and concatenate them (without using library function). #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str1[20],str2[10]; int i=0,j=0; clrscr(); puts("enter string first");
15. gets(str1); puts("n enter string second"); gets(str2); while(str1[i]!='0') { str1[i++]; } while(str2[j]!='0') { str1[i]=str2[j]; str1[i++]; str2[j++]; str1[i]='0'; } printf("concaneted string="); puts(str1); getch(); } 14 WAP to read a sentence and count the number of vovel and consonant. PART -B 1. WAP to read N integer (zero,+ve and -ve) into an arrey and find the sum of positive numbers, sum of negative numbers and average of all input numbers #include<stdio.h>
16. #include<conio.h> void main() { int a[20],n,i,sum1=0,sum2=0; float avg=0.0; clrscr(); printf("n enter the size of the arrayn"); scanf("%d",&n); printf("enter array element 1 by 1 n "); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) { if(a[i]>0) sum1=sum1+a[i]; else sum2=sum2+a[i]; } avg=(sum1+sum2)/n; printf("n sum of posotive number=%d",sum1); printf("n sum of negative number =%d",sum2); printf("n average on entered number =%f",avg); getch(); }
17. 2. WAP to input N real numbers and to find the mean , variance and standard deviation #include<stdio.h> #include<conio.h> #include<math.h> void main() { float a[20],sum=0.0,mean=0.0,var,sumsqr,dev; int n,i; clrscr(); printf("n enter the size of the arrayn"); scanf("%d",&n); printf("enter array element 1 by 1 n "); for(i=0;i<n;i++) { scanf("%f",&a[i]); sum=sum+a[i]; } mean=sum/n; for(i=0;i<n;i++) sumsqr=sumsqr+((a[i]-mean)*(a[i]-mean)); var=sumsqr/n; dev=sqrt(var); printf("n mean of entered number =%f",mean); printf("n variatiuon=%f",var);
18. printf("n deviation=%f",dev); getch(); } 3. WAP to input N numbers and store them in an array and apply linear search #include<stdio.h> #include<conio.h> void main() { int a[20],item,i,flag=0; int n; clrscr(); printf("n enter size of the arrayn"); scanf("%d",&n); printf("n enter array element 1 by 1 n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("n enter a searching numbern"); scanf("%d",&item); for(i=0;i<n;i++) if(item==a[i]) { flag=1; break;
19. } if(flag) printf("n searching number %d is found at %d position",item,i+1); else printf("n searching value not found"); getch(); } 4. WAP to sort N numbers is ascending order or descending order using bubble sort #include<stdio.h> #include<conio.h> void main() { int a[10],i,j,n,temp; clrscr(); printf("n enter the size of the arrayn"); scanf("%d",&n); printf("n enter the array element 1 by 1 n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("n after insertion array is n"); for(i=0;i<n;i++)
20. printf("%dt",a[i]); for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } printf("n after sorting in ascending order array isn"); for(i=0;i<n;i++) printf("%dt",a[i]); getch(); } 5. WAP to implement binary search //binary searching #include<stdio.h> #include<conio.h> void main() { int a[10],i,j,n,temp,item,ub,lb,mid,found=0; clrscr(); printf("n enter the size of the arrayn"); scanf("%d",&n);
21. printf("n enter the array element 1 by 1 in ascending ordern"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("n after insertion array is n"); for(i=0;i<n;i++) printf("%dt",a[i]); for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } printf("n enter a searching numbern"); scanf("%d",&item); lb=0; ub=n-1; while(lb<ub) { mid=(lb+ub)/2; if(item==a[mid]) { found=1; break;
22. } if(item<a[mid]) ub=mid-1; else lb=mid+1; } if(found) printf("n searching value is found at %d position",mid+1); else printf("n searching value not found"); getch(); } 6. WAP to read two matrices A & B of size M*N and perform product of two given matrices. #include<stdio.h> #include<conio.h> int main() { int a[' '][' '],b[' '][' '],c[' '][' '],i,j,k,m1,n1,m2,n2; clrscr(); printf("n enter the order of the first matrix"); scanf("%d%d",&m1,&n1); printf("n enter the order of the second matrix"); scanf("%d%d",&m2,&n2); if(m1!=n2) { printf("multiplication not possible"); getch(); return 0; } printf("n enter matrix 1 element 1 by 1");
23. for(i=0;i<m1;i++) { for(j=0;j<n1;j++) { scanf("%d",&a[i][j]); } } printf("n enter matrix 2 element 1 by 1"); for(i=0;i<m2;i++) { for(j=0;j<n2;j++) { scanf("%d",&b[i][j]); } } printf("n matrix 1 isn"); for(i=0;i<m1;i++) { for(j=0;j<n1;j++) printf("%dt",a[i][j]); printf("n"); } printf("n matrix 2 is n"); for(i=0;i<m2;i++) { for(j=0;j<n2;j++) printf("%dt",b[i][j]); printf("n"); } // multiplication start for(i=0;i<m1;i++) { for(j=0;j<n1;j++) { c[i][j]=0; for(k=0;k<m1;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } printf("n after multip[lication matrix 3 is n"); for(i=0;i<m1;i++) {
24. for(j=0;j<n2;j++) printf("%dt",c[i][j]); printf("n"); } getch(); return 0; } 7. WAP to list the name of the students who have scored more than 60% of total marks in three subject using str var #include<Stdio.h> #include<conio.h> struct student { int usn; char name[10]; int marks[3]; int total; }; struct student s[5]; void main() { int i,j,n; clrscr(); printf("n enter the number of the studentn"); scanf("%d",&n); for(i=0;i<n;i++) {
25. printf("n enter the details of %d studentn",i+1); printf("enter student usnn"); scanf(" %d",&s[i].usn); printf("n enter student namen"); scanf("%s",&s[i].name); printf("n enter three paper marks 1 by 1 n"); for(j=0;j<3;j++) { scanf("%d",&s[i].marks[j]); s[i].total=s[i].total+s[i].marks[j]; } } clrscr(); printf("n n student details (scored more than 60%)....."); printf("n .........."); for(i=0;i<n;i++) { if(s[i].total>=180) { printf("n usn no.=%d",s[i].usn); printf("n student name=%s",s[i].name); printf("n total marks=%d",s[i].total); } printf("nnn"); }
26. getch(); } 8 WAP to compute the sum of two complex number-passing a structure to a function. #include<stdio.h> #include<conio.h> struct complex { int real,imag; }; typedef struct complex s; void main() { s x,y; clrscr(); printf("n enter the first complex number"); printf("n enter real part="); scanf("%d",&x.real); printf("n enter the imaginary part="); scanf("%d",&x.imag); printf("n enterthe second imaginary part"); printf("n enter real part="); scanf("%d",&y.real); printf("n enter imaginary part="); scanf("%d",&y.imag);
27. printf("n addition of two complex number is=(%d+i%d)+(%d+i%d)=(%d+i%d)",x.real,x.imag,y.real,y.imag,x.real+y.real,x.imag+y.imag); getch(); } 9. Define a book structure having book info . WAP to accept date and list out all the book infopublished during date. #include<stdio.h> #include<conio.h> struct date { int month; int year; }; typedef struct date pdate; struct book { int isbn; char title[20]; char author[20]; int price; pdate date; }; typedef struct book books; void main() { pdate search;
28. books mca[20]; int n,i; clrscr(); printf("n enter how many xbookst"); scanf("%d",&n); for(i=0;i<n;i++) { printf("n enter the details of %d books n",i+1); printf("n enter the isbn number t"); scanf("%d",&mca[i].isbn); printf("n enter the name of the bookt"); scanf("%s",mca[i].title); printf("n enter the author namet"); scanf("%s",mca[i].author); printf("n enter the price of the bookt"); scanf("%d",&mca[i].price); printf("n enter the mounth of the publicationt"); scanf("%d",&mca[i].date.month); printf("n enter the year of the publicationt"); scanf("%d",&mca[i].date.year); } printf("n enter the month of publication that to searcht"); scanf("%d",&search.month); printf("n enter the year of publication to searcht"); scanf("%d", &search.year);
29. printf("nn book datails.....n"); for(i=0;i<n;i++) { if((mca[i].date.month==search.month)&&(mca[i].date.year==search.year)) { printf("n title=%s",mca[i].title); printf("n isbn=%d",mca[i].isbn); printf("n author name=%s",mca[i].author); printf("n price of the book=%d",mca[i].price); printf("n month of publication=%d",mca[i].date.month); printf("n year of publication=%d",mca[i].date.year); } } getch(); } 10. define structure having students info with 5 sub marks using array, list out all the failed students. #include<stdio.h> #include<conio.h> struct student { char name[20]; int usn,tot,sub[5]; }; void main()
30. { int i,n,j; struct student std[10]; clrscr(); printf("n enter the number of the student t"); scanf("%d",&n); printf("n enter the student details.."); for(i=0;i<n;i++) { std[i].tot=0; printf("n enter name of the %d student=t",i+1); scanf("%s",std[i].name); printf("n enter usn of the %d student=t",i+1); scanf("%d",&std[i].usn); printf("n enter marks of the %d student==t",i+1); for(j=0;j<5;j++) { printf("n enter subject %d markst",j+1); scanf("%d",&std[i].sub[j]); while(std[i].sub[j]>100) { printf("marks over flow n reemter the markst"); scanf("%d",&std[i].sub[j]); } std[i].tot+=std[i].sub[j];
31. } } printf("n list of failed studentsn"); for(i=0;i<n;i++) { if(std[i].tot<200) { printf("n name=%st",std[i].name); printf("n usn=%dt",std[i].usn); printf("n total=%d",std[i].tot); } } getch(); } 11. WAP to find the sum of n integers in an array using pointers. #include<stdio.h> #include<conio.h> void main() { int n,arr[10],i,*p,sum=0; p=arr; clrscr(); printf("n enter the size of the arrayn"); scanf("%d",&n); printf("n enter %d numbern",n);
32. for(i=0;i<n;i++) { scanf("%d",&arr[i]); } for(i=1;i<=n;i++) { sum=sum+*p; *p++; } printf("n sum of all the number =%d",sum); getch(); } 12. WAP to read and write to a file. #include<stdio.h> #include<conio.h> FILE *fp; void main() { char name[20]; int usn,marks,n,i; clrscr(); fp=fopen("student.dat","w"); printf("n how many student records you want to enter t"); scanf("%d",&n); for(i=1;i<=n;i++)
33. { printf("n enter %d student name t",i); scanf("%s",&name); printf("n enter %d student usn t",i); scanf("%d",&usn); printf("n enter %d student marks t",i); scanf("%d",&marks); fprintf(fp,"%st%dt%dn",name,usn,marks); } fclose(fp); fp=fopen("student.dat","r"); printf("students records are n"); for(i=1;i<=n;i++) { fscanf(fp,"%s%d%d",&name,&usn,&marks); printf("nnn name of %d student=%s",i,name); printf("n usn of %d student=%d",i,usn); printf("n marks of %d student=%d",i,marks); } fclose(fp); getch(); } 13. wap to creat and count number of character in a file. #include<stdio.h> void main()
34. { FILE *f; char fname[20],c; int ctr=0; clrscr(); f=fopen("rj","w"); fclose(f); printf("n enter the file name to openn"); scanf("%s",fname); f=fopen("fname","r"); if (!f) printf("n file not exist..permission denied"); else { c=getc(f); for(;getc(f)!=EOF;ctr++) { } printf("n total count of character is %d",ctr); } getch(); }
35. 14. WAP to handle file with mixed data type. #include<stdio.h> struct emp { char name[20]; int empno; float sal; char status[10]; }; void main() { typedef struct emp s; s s1; FILE *f; clrscr(); f=fopen("deepak.txt","w"); /* if(!f) puts("file not exist.permission denied"); else { */ printf("...employee details......n"); printf("n enter the employee name="); scanf("%s",s1.name); //fflush(stdin); printf("n enter the employee number ="); scanf("%d",&s1.empno);
36. printf("n enter employee salary="); scanf("%f",&s1.sal); // fflush(stdin); printf("n enter the employee status="); scanf("%s",s1.status); // fprintf(f,"%s%d%f%c",s1.name,s1.empno,s1.sal,s1.status); fclose(f); f=fopen("deepak.txt","r"); // fscanf(f,"%s%d%f%c",s1.name,&s1.empno,&s1.sal,s1.status); printf("n name=%s",s1.name); printf("n emp number =%d",s1.empno); printf("n employee salary=%f",s1.sal); printf("n employee status=%s",s1.status); fclose(f); getch(); }