Académique Documents
Professionnel Documents
Culture Documents
Website: www.methodist.edu.in
-2-
-3-
INDEX
PROGRAMS 1.SIMPLE C PROGRAMS A.PROGRAM TO PRINT B. WORKING OF FUNDAMENTAL DATATYPES C.INTERCHANGE TWO VALUES D. TO CALCULATE SIMPLE INTEREST E. TO CALCULATE AREA AND PERIMETER OF A F. TO CALCULATE THE AREA AND PERIMETER OF TRIANGLE G. TO FIND AREA AND PERIMETER OF A RECTANGLE 2. PROGRAMS TO DEMOSTRATE WORKING OF OPERATORS A. TO DEMONSTRATE THE WORKING OF ARITHMETICAL OPERATORS B. PROGRAM TO DEMONSTRATE THE WORKING OF INCREMENT AND DECREMENT OPERATORS C. PROGRAM TO DEMONSTRATE THE WORKING OF INCREMENT AND DECREMENT OPERATORS D. TO FIND THE MAX OF TWO NUMBERS BY USING CONDITIONAL OPERATORS E. TO FIND THE MAX OF THREE INTEGERS BY USING CONDITIONAL OPERATOR F. PROGRAM TO FIND THE MAX OF THREE INTEGERS BY USING CONDITIONAL OPERATOR G. PROGRAM TO DEMONSTRATE THE WORKING OF RELATIONAL OPERATORS H. TO DEMONSTRATE THE WORKING OF SHORTCUT ASSIGNMENT OPERATORS I .PROGRAM TO DEMONSTRATE THE WORKING OF BITWISE OPERATORS 16 9
-43.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS A. PROGRAM TO FIND THE NUMBER IS EVEN OR ODD A. PROGRAM TO FIND GREATEST OF TWO NOS B. PROGRAM TO FIND GREATEST OF TWO NOS C.PROGRAM TO CHECK WHETHER THE YEAR IS LEAP OR NOT D. PROGRAM TO CHECK WHETHER THE GIVEN CHARACTER IS ALPHABET OR DIGIT OR SPECIAL CHARACTER E. PROGRAM TO CHECK WHEATHER THE GIVEN CHARACTER IS ALPHABET OR DIGIT OR SPECIAL CHARACTER F. PROGRAM TO CALCULATE THE ROOTS OF A QUADRATIC G. PROGRAM TO CALCULATE THE TOTAL , AVERAGE AND GRADE OF A STUDENT H. PROGRAM TO PRINT THE MONTH NAME IF MONTH NUMBER WAS GIVEN WITH IF ELSE LADDER I. PROGRAM TO PRINT THE MONTH NAME IF MONTH NUMBER WAS GIVEN WITH SWITCH CASE J. PROGRAM TO FIND GREATEST OF 3 NOS K. PROGRAM TO CALCULATE TAX 4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS A. PROGRAM TO FIND SUM OF FIRST N NATURAL NUMBERS WITH WHILE LOOP B.PROGRAM TO CALCULATE THE FACTORIAL OF THE GIVEN NUMBER C. PROGRAM TO FIND WHETHER GIVEN NO IS ARMSTRONG OR NOT D. PROGRAM TO FIND FIBONACCI SERIES UP TO GIVEN RANGE E. PROGRAM TO PRINT MATHEMATICAL TABLE F. PROGRAM TO FIND THE VALUE OF SINX G. PROGRAM TO FIND THE VALUE OF COSX H. PROGRAM TO FIND SUM OF DIGITS OF A NUMBER I. PROGRAM TO WHETHER GIVEN NO IS PALINDROME OR NOT 5. PROGRAMS USING ARRAYS A. PROGRAM TO FIND SUM OF ALL ELEMENTS IN THE ARRAY B. PROGRAM TO FIND MAXIMUM AND MINIMUM NUMBER IN AN ARRAY C. PROGRAM TO REVERSE THE ARRAY ELEMENTS IN PLACE D. PROGRAM TO PERFORM LINEAR SEARCH E. PROGRAM TO PERFORM BINARY SEARCH F. PROGRAM TO PERFORM BUBBLE SORT 52 41 22
-5G. PROGRAM TO PERFORM SELECTION SORT H. PROGRAM TO READ AND PRINT A MATRIX I. PROGRAM TO TRANSPOSE A MATRIX J. PROGRAM TO ADD TWO MATRICES K. PROGRAM TO MULTIPLY TWO MATRICES L. PROGRAM TO COUNT FREQUENCY OF OCCURRENCES OF CHARACTERS AND SPECIAL CHARACTERS 6. PROGRAMS USING FUNCTIONS A. FUNCTION TO CALCULATE FACTORIAL OF AN INTEGER B.FUNCTION TO CALCULATE NCR AND NPR (USING FUNCTION) C. FUNCTION TO CALCULATE FACTORIAL OF AN INTEGER D. PROGRAM TO PRINT PRIME NUMBERS IN THE GIVEN RANGE (USING FUNCTION) E. PROGRAM TO PRINT ARMSTRONG NUMBERS IN THE GIVEN F. PROGRAM TO SWAP TWO INTEGER NUMBERS (USING FUNCTION) G. PROGRAM TO PRINT FIBONACCI SERIES UP TO A GIVEN NUMBER (USING FUNCTION) H. PROGRAM TO PRINT NTH FIBONACCI NUMBER (USING RECURSIVE FUNCTION I. PROGRAM TO FIND GCD OF TWO INTEGERS (USING RECURSIVE FUNCTION) J. PROGRAM TO FIND SUM OF ALL ELEMENTS IN AN ARRAY K. PROGRAM TO PERFORM BUBBLE SORT (USING FUNCTION) L. PROGRAM TO MULTIPLY TWO MATRICES (USING FUNCTION) M. PROGRAM TO REVERSE THE CONTENTS OF THE GIVEN STRING N. PROGRAM TO FIND THE LENGTH OF THE GIVEN STRING BY USING USER DEFINED FUNCTION O. PROGRAM TO COPY THE CONTENTS OF THE GIVEN STRING TO ANOTHER BY USING USER DEFINED FUNCTION P. PROGRAM TO APPEND THE CONTENTS OF THE GIVEN STRING TO ANOTHER BY USING USER DEFINED FUNCTION Q. PROGRAM TO COMPARE THE CONTENTS OF GIVEN TWO STRINGS BY USING USER DEFINED FUNCTION 7. PROGRAMS USING POINTERS M A. PROGRAM TO REVERSE THE CONTENTS OF THE GIVEN STRING BY USING USER DEFINED FUNCTION WITH POINTERS B. PROGRAM TO FIND THE LENGTH OF THE GIVEN STRING BY 99 73
-6USING USER DEFINED FUNCTION WITH POINTERS C. PROGRAM TO COPY THE CONTENTS OF THE GIVEN STRING TO ANOTHER BY USING USER DEFINED FUNCTION WITH POINTERS D. PROGRAM TO APPEND THE CONTENTS OF THE GIVEN STRING E. PROGRAM TO COMPARE THE CONTENTS OF GIVEN TWO STRINGS BY USING USER DEFINED FUNCTION 8.PROGRAMS USING STRINGS A. PROGRAM TO ABBREVIATE THE CONTENTS OF THE CONTENTS OF GIVEN STRING B. DEFINE A STRUCTURE TO REPRESENT RATIONAL NUMBER AND WRITE A PROGRAM TO PERFORM THE ARITHMETICAL OPERATIONS ON RATIONAL NUMBERS C. DEFINE A STRUCTURE TO REPRESENT COMPLEX NUMBER AND WRITE A PROGRAM TO PERFORM THEARITHMETICAL OPERATIONS ON COMPLEX NUMBERS D. DEFINE A STRUCTURE TO REPRESENT RATIONAL NUMBER AND WRITE A PROGRAM TO PERFORM THE ARITHMETICAL OPERATIONS ON RATIONAL NUMBERS BY USING FUNCTIONS E. DEFINE A STRUCTURE TO REPRESENT COMPLEX NUMBER AND WRITE A PROGRAM TO PERFORM THE ARITHMETICAL OPERATIONS ON COMPLEX NUMBERS BY USING FUNCTIONS 9.PROGRAMS USING STRUCTURES & UNIONS A. WRITE A PROGRAM TO GENERATE STUDENT RECORDS WITH TOTAL AVERAGE IF MARKS OF 3 SUBJECTS ARE GIVEN AS INPUTS BY USING AN ARRAY OF STUDENT STRUCTURES. B. WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF POINTERS TO STRUCTURES TO CALCULATE THE TOTAL AND AVERAGE OF THE GIVEN STUDENT C. PROGRAM TO PRINT TOMORROWS DATE IF TO-DAYS DATE WAS GIVEN 117 109
A. C PROGRAM TO CREATE A FILE B. C PROGRAM TO PRINT THE CONTENTS OF THE FILE ON THE MONITOR C. C PROGRAM TO COPY ONE FILE INTO ANOTHER FILE D. C PROGRAM TO CREATE A FILE OF STUDENT RECORDS E. C PROGRAM TO DISPLAY STUDENT RECORDS BY READING
124
-7-
130
148
155
-8B. Program to demonstrate single inheritance: private C. Program to demonstrate multilevel inheritance D. Program to demonstrate multiple inheritances
163
176
182
-9-
1.SIMPLE C PROGRAMS A. PROGRAM TO PRINT AIM: Accepting text from user and printing #include<stdio.h> #include<conio.h> void main() { clrscr(); printf(" C test program"); getch(); }
- 10 -
1.SIMPLE C PROGRAMS
AIM: Program to demonstrate the working of fundamental datatypes #include<stdio.h> #include<conio.h> void main() { float x; int y; char a; double k; clrscr(); x=123.456; y=20; a='z'; k=9.3456677; printf("\n x=%f",x); printf("\n y=%d",y); printf("\n a=%c",a); printf("\n k=%lf",k); getch(); }
- 11 -
Program to interchange to values #include<stdio.h> #include<conio.h> void main() { int a,b,temp; clrscr(); printf("enter the values of a,b \n"); scanf("%d%d",&a,&b); temp=a; a=b; b=temp; printf("a=%d\n b=%d\n ",a,b); getch(); }
- 12 -
AIM: Program to calculate simple interest #include<stdio.h> #include<conio.h> void main() { float p,n,r; float simple=0.0; clrscr(); printf(" enter the values of p n r\n"); scanf("%f%f%f",&p,&n,&r); simple=(p*n*r)/100.0; printf("simple interest is :%f\n",simple); getch(); }
- 14 -
1.SIMPLE C PROGRAMS E. TO CALCULATE AREA AND PERIMETER OF A CIRCLE AIM: Program to calculate area and perimeter of a circle #include<stdio.h> #include<conio.h> void main() { float r,area,peri; clrscr(); printf("\n enter any radious"); scanf("%f",&r); area=3.14*r*r; peri=2*3.14*r; printf("\n area is %f",area); printf("\n perimeter is %f",peri); getch(); }
- 15 1.SIMPLE C PROGRAMS F. TO CALCULATE THE AREA AND PERIMETER OF TRIANGLE AIM: Program to calculate the area and perimeter of triangle #include<stdio.h> #include<conio.h> #include<math.h> void main() { float a,b,c,s,area,peri; clrscr(); printf("\n enter the lengths of 3 sides"); scanf("%f%f%f",&a,&b,&c); s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); peri=a+b+c; printf("\n area is %f",area); printf("\n perimeter is %f",peri); getch(); }
INPUT: enter the lengths of 3 sides 4 5 6 OUTPUT: area is 9.921567 perimeter is 15.000000
- 16 1.SIMPLE C PROGRAMS G. TO FIND AREA AND PERIMETER OF A RECTANGLE AIM: Program to find area and perimeter of a rectangle #include<stdio.h> #include<conio.h> void main() { float l,b,area,peri; clrscr(); printf("enter the length and breadth of rectangle\n"); scanf("%f%f",&l,&b); area=l*b; peri=2*(l+b); printf("area is %f\n",area); printf("perimeter is %f\n",peri); getch(); } INPUT: enter the length and breadth of rectangle 23.4 43.4 OUTPUT: area is 1015.559998 perimeter is 133.600006
- 17 2. PROGRAMS TO DEMOSTRATE WORKING OF OPERATORS A. TO DEMONSTRATE THE WORKING OF ARITHMETICAL OPERATORS AIM: Program to demonstrate the working of arithmetical operators #include<stdio.h> #include<conio.h> void main() { int a,b,ans; clrscr(); printf("enter any two integers"); scanf("%d%d",&a,&b); ans=a+b; printf("\n sum is %d",ans); ans=a-b; printf("\n difference is %d",ans); ans=a*b; printf("\n product is %d",ans); ans=a/b; printf("\n quotient is %d",ans); ans=a%b; printf("\n remainder is %d",ans); getch(); } INPUT: enter any two integers10 20 OUTPUT: sum is 30 difference is -10 product is 200 quotient is 0 remainder is 10
- 18 -
- 19 2. PROGRAMS TO DEMOSTRATE WORKING OF OPERATORS B. PROGRAM TO DEMONSTRATE THE WORKING OF INCREMENT AND DECREMENT OPERATORS AIM: Program to demonstrate the working of increment and decrement operators #include<stdio.h> #include<conio.h> void main() { int a,ans; clrscr(); a=2; printf("\n initial value of a is %d",a); getch(); a++; printf("\n after a++ a is %d",a); getch(); ++a; printf("\n after ++a a is %d",a); getch(); ans=a++; printf("\n with ans=a++ a is %d and ans is %d",a,ans); getch(); ans=++a; printf("\n with ans=++a a is %d and ans is %d",a,ans); getch(); } INPUT: initial value of a is 2 OUTPUT: after a++ a is after ++a a is with ans=a++ with ans=++a 3 4 a is 5 and ans is 4 a is 6 and ans is 6
- 20 2. PROGRAMS TO DEMOSTRATE WORKING OF OPERATORS C. PROGRAM TO DEMONSTRATE THE WORKING OF INCREMENT AND DECREMENT OPERATORS AIM: Program to demonstrate the working of increment and decrement operators #include<stdio.h> #include<conio.h> void main() { int a,ans; clrscr(); a=2; printf("\n now a is %d",a); ans=a++ * a++ * ++a; printf("\n with ans=a++ * a++ * ++a ans is %d and a is %d",ans,a); getch(); } INPUT: now a is 2 with ans=a++ * a++ * ++a OUTPUT: ans is 27 and a is 5
- 21 2. PROGRAMS TO DEMOSTRATE WORKING OF OPERATORS D. TO FIND THE MAX OF TWO NUMBERS BY USING CONDITIONAL OPERATORS AIM: Program to find the max of two numbers by using conditional operators #include<stdio.h> #include<conio.h> void main() { int a,b,c,max; clrscr(); printf("enter any three integers"); scanf("%d%d%d",&a,&b,&c); max=a>b?a:b; max=max>c?max:c; printf("max is %d",max); getch(); }
- 22 2. PROGRAMS TO DEMOSTRATE WORKING OF OPERATORS E. TO FIND THE MAX OF THREE INTEGERS BY USING CONDITIONAL OPERATOR AIM: Program to find the max of three integers by using conditional operator #include<stdio.h> #include<conio.h> void main() { int a,b,c,max; clrscr(); printf("enter any three integers"); scanf("%d%d%d",&a,&b,&c); max=a>b?(a>c?a:c):(b>c)?b:c; printf("max is %d",max); getch(); } INPUT: enter any three integers 24 76 48 OUTPUT: max is 76
- 23 2. PROGRAMS TO DEMOSTRATE WORKING OF OPERATORS F. PROGRAM TO FIND THE MAX OF THREE INTEGERS BY USING CONDITIONAL OPERATOR AIM: Program to find the max of three integers by using conditional operator #include<stdio.h> #include<conio.h> void main() { int a,b,max; printf("enter any two integers"); scanf("%d%d",&a,&b); max=(a>b)?a:b; printf("max no is %d",max); getch(); }
- 24 2. PROGRAMS TO DEMOSTRATE WORKING OF OPERATORS G. PROGRAM TO DEMONSTRATE THE WORKING OF RELATIONAL OPERATORS AIM: Program to demonstrate the working of relational operators #include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("enter any two integers"); scanf("%d%d",&a,&b); printf("\n %d > %d is %d",a,b,a>b); printf("\n %d >= %d is %d",a,b,a>=b); printf("\n %d < %d is %d",a,b,a<b); printf("\n %d <= %d id %d",a,b,a<=b); printf("\n %d == %d is %d",a,b,a==b); printf("\n %d != %d is %d",a,b,a!=b); getch(); } INPUT: enter any two integers24 35 OUTPUT: 24 > 35 is 0 24 >= 35 is 0 24 < 35 is 1 24 <= 35 is 1 24 == 35 is 0 24 != 35 is 1
- 25 2. PROGRAMS TO DEMOSTRATE WORKING OF OPERATORS H. TO DEMONSTRATE THE WORKING OF SHORTCUT ASSIGNMENT OPERATORS AIM: Program to demonstrate the working of shortcut assignment operators #include<stdio.h> #include<conio.h> void main() { int a; clrscr(); printf("enter any integer"); scanf("%d",&a); printf("\n initially a= %d",a); a+=10; printf("\n with a+=10 a= %d\t ",a); a-=20; printf("\n with a-=20 a= %d",a); a*=5; printf("\n with a*=5 a=%d",a); a/=10; printf("\n with a/=10 a= %d",a); getch(); } INPUT: enter any integer2 OUTPUT: initially a= 2 with a+=10 a= 12 with a-=20 a= -8 with a*=5 a=-40 with a/=10 a= -4
- 26 2. PROGRAMS TO DEMOSTRATE WORKING OF OPERATORS I. PROGRAM TO DEMONSTRATE THE WORKING OF BITWISE OPERATORS AIM: Program to demstrate the working of bitwise operataors #include<stdio.h> #include<conio.h> void main() { unsigned int a,b,ans; clrscr(); printf("enter any two integers"); scanf("%u%u",&a,&b); ans=a&b; printf("\n %u & %u is %u",a,b,ans); ans=a|b; printf("\n %u | %u is %u",a,b,ans); ans=a^b; printf("\n %u ^ %u is %u",a,b,ans); ans=~a; printf("\n ones complement of %u is %u",a,ans); ans=a>>3; printf("\n shift right %u with 3 count is %u",a,ans); ans=a<<3; printf("\n shift left %u with 3 count is %u",a,ans); getch(); } INPUT: enter any two integers10 20 OUTPUT: 10 & 20 is 0 10 | 20 is 30 10 ^ 20 is 30 ones complement of 10 is 65525 shift right 10 with 3 count is 1 shift left 10 with 3 count is 80
- 27 3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS A. PROGRAM TO FIND THE NUMBER IS EVEN OR ODD
AIM: Program to find whether the numbers is even or odd #include<stdio.h> #include<conio.h> void main() { int a; clrscr(); printf("enter a no\n"); scanf("%d",&a); printf("the no can not be even or odd\n"); if(a%2==0) printf("the no %d is even",a); else printf("the no %d is odd",a); getch(); } INPUT: enter a no 23 OUTPUT: the no 23 is odd enter a no 34 the no 34 is even
- 28 3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS B. PROGRAM TO FIND GREATEST OF TWO NOS AIM: Program to find the greatest of teo numbers #include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("enter two distinct nos\n"); scanf("%d%d",&a,&b); if(a>b) printf("the no %d is greater",a); else printf("the no %d is greater",b); getch(); } INPUT: enter two nos 24 OUTPUT: the no 4 is greater
- 29 3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS C.PROGRAM TO CHECK WHETHER THE YEAR IS LEAP OR NOT
AIM: Program to check a year is leap year or not #include<stdio.h> #include<conio.h> void main() { int year; clrscr(); printf("enter the year\n"); scanf("%d",&year); if(((year%4)==0)&&((year%100)!=0)||((year%400)==0)) printf("the year %d is leap year",year); else printf("the year %d is not a leap year",year); getch(); }
INPUT: enter the year 1884 OUTPUT: the year 1884 is leap year
- 30 3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS D. PROGRAM TO CHECK WHETHER THE GIVEN CHARACTER IS ALPHABET OR DIGIT OR SPECIAL CHARACTER AIM: Program to the character ts alphabt or digit or a special character #include<stdio.h> #include<conio.h> #include<math.h> void main() { char ch; clrscr(); printf("\n enter any character"); scanf("%c",&ch); if((ch >= 'a' && ch <= 'z')||(ch>= 'A' && ch<= 'Z')) printf("\n alphabet"); else if(ch >= '0' && ch <= '9') printf("\n digit"); else printf("\n special character"); getch(); } INPUT: enter any character a OUTPUT: alphabet enter any character4 digit enter any character* special character
- 31 3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS E. PROGRAM TO CHECK WHEATHER THE GIVEN CHARACTER IS ALPHABET OR DIGIT OR SPECIAL CHARACTER
AIM: Program to check the character is a alphabet or digit or speciak character using a system defined functions #include<stdio.h> #include<conio.h> #include<ctype.h> void main() { char ch; clrscr(); printf("\n enter any character"); scanf("%c",&ch); if( isalpha(ch) ) printf("\n alphabet"); else if( isdigit(ch) ) printf("\n digit"); else printf("\n special character"); getch(); } INPUT: enter any character a OUTPUT: alphabet enter any character4 digit enter any character* special character
- 32 -
3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS F. PROGRAM TO CALCULATE THE ROOTS OF A QUADRATIC EQUATION AIM: Program to caluculate the roots of a quadratic equation #include<stdio.h> #include<conio.h> #include<math.h> void main() { float a,b,c,d,r1,r2,rp,ip; clrscr(); printf("\n enter a b c values"); scanf("%f%f%f",&a,&b,&c); if(a==0) printf(" not a QE"); else { d=b*b-4*a*c; if(d>=0) { printf("\n roots are real"); r1= (-b +sqrt(d))/(2*a); r2= (-b -sqrt(d))/(2*a); printf("\n roots are %f and %f",r1,r2); } else { printf("\n roots are imaginary"); rp= -b/(2*a); ip=sqrt(-d)/(2*a); printf("\n roots are ( %f , %f ) and ( %f , %f )",rp,ip,rp,-ip); } } getch(); }
- 33 -
INPUT: enter a b c values3 5 2 OUTPUT: roots are real roots are -0.666667 and -1.000000 enter a b c values 1 6 3 roots are real roots are -0.550510 and -5.449490 enter a b c values4 2 8 roots are imaginary roots are ( -0.250000 , 1.391941 ) and ( -0.250000 , -1.391941 )
- 34 -
3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS G. PROGRAM TO CALCULATE THE TOTAL , AVERAGE AND GRADE OF A STUDENT AIM: Program to calculate the total, average and grade of a student #include<stdio.h> #include<conio.h> void main() { int m1,m2,m3,tot; float avg; char gr; clrscr(); printf("enter the marks of 3 subjects\n"); scanf("%d%d%d",&m1,&m2,&m3); tot=m1+m2+m3; avg=(float)(m1+m2+m3)/3; if(m1<40 || m2<40 || m3 <40) gr='F'; else if(avg<50) gr='E'; else if(avg<60) gr='D'; else if(avg<70) gr='C'; else if(avg<80) gr='B';
- 35 else gr='A'; printf("\n total = %d",tot); printf("\n average = %f",avg); printf("\n Grade = %c",gr); getch(); } INPUT: enter the marks of 3 subjects 89 76 57 OUTPUT: total = 222 average = 74.000000 Grade = B
- 36 -
3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS H. PROGRAM TO PRINT THE MONTH NAME IF MONTH NUMBER WAS GIVEN WITH IF ELSE LADDER AIM: Program to print the name of the month if number is given #include<stdio.h> #include<conio.h> void main() { int mno; clrscr(); printf("\n enter any month number"); scanf("%d",&mno); if(mno==1) printf("\n jan"); else if(mno==2) printf("\n feb"); else if(mno==3) printf("\n mar"); else if(mno==4) printf("\n april"); else if(mno==5) printf("\n may"); else if(mno==6) printf("\n june");
- 37 else if(mno==7) printf("\n july"); else if(mno==8) printf("\n august"); else if(mno==9) printf("\n september"); else if(mno==10) printf("\n october"); else if(mno==11)
printf("\n november"); else if(mno==12) printf("\n december"); else printf("\n invalid month number"); getch(); } INPUT: enter any month number12 OUTPUT: december enter any month number5 may enter any month number19 invalid month number
- 38 3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS I. PROGRAM TO PRINT THE MONTH NAME IF MONTH NUMBER WAS GIVEN WITH SWITCH CASE AIM: Program to print the month name if numberis given using the switch case #include<stdio.h> #include<conio.h> void main() { int mno; clrscr(); printf("\n enter any month number"); scanf("%d",&mno); switch(mno) { case 1: printf("\n jan"); break; case 2: printf("\n feb"); break; case 3: printf("\n mar"); break; case 4: printf("\n april"); break; case 5: printf("\n may"); break; case 6: printf("\n june"); break; case 7: printf("\n july"); break; case 8: printf("\n august"); break; case 9: printf("\n september"); break; case 10: printf("\n october"); break; case 11: printf("\n november"); break;
- 39 case 12: printf("\n december"); break; default: printf("\n december"); } getch(); } INPUT: enter any month number12 OUTPUT: december enter any month number5 may enter any month number19 invalid month number
- 40 3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS J. PROGRAM TO FIND GREATEST OF 3 NOS AIM: Program to find greatest of three numbers #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("enter 3 nos\n"); scanf("%d%d%d",&a,&b,&c); if(a>b) { if(a>c) printf("the no %d is greater\n",a); else printf("the no %d is greater\n",c); } else if(b>c) printf("the no %d is greater\n",b); getch(); } INPUT: enter 3 nos 3 4 2 OUTPUT: the no 4 is greater
AIM: Program to calculate the tax #include<stdio.h> #include<conio.h> void main() { float inc,tax=0.0; int ch; clrscr(); printf("enter income \n"); scanf("%f",&inc); ch=(int)(inc/10000); switch(ch) { case 0: case 1: case 2: tax=0; break; case 3: case 4: tax=inc*10/100; break; case 5: case 6: tax=inc*20/100; break; default: tax=inc*25/100; break; } printf("tax is %f\n",tax); getch(); } INPUT: enter income 54321 OUTPUT: tax is 10864.200195
- 42 -
- 43 4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS A. PROGRAM TO FIND SUM OF FIRST N NATURAL NUMBERS WITH WHILE LOOP AIM: Program to find the sum of n natural numbers #include<stdio.h> #include<conio.h> void main() { int i,n,sum=0; clrscr(); printf("enter the value of n \n"); scanf("%d",&n); i=1; while(i<=n) { sum=sum+i; i++; } printf("sum is %d\n",sum); getch(); } INPUT: enter the value of n 150 OUTPUT: sum is 11325
- 44 4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS B.PROGRAM TO CALCULATE THE FACTORIAL OF THE GIVEN NUMBER AIM: Program to calculate the factorial of a number #include<stdio.h> #include<conio.h> void main() { int i; long fact=1; clrscr(); printf(\n enter n integer); scanf(%d,&n); for(i=1;i<=n;i++) fact=fact*I; printf("factorial is %ld\n",fact); getch(); } INPUT: enter an integer 5 OUTPUT: factorial is 120
- 45 4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS C. PROGRAM TO FIND WHETHER GIVEN NO IS ARMSTRONG OR NOT AIM: Program to check whether number is Armstrong number or not #include<stdio.h> #include<conio.h> void main() { int n,sum=0,temp,rem; clrscr(); printf("enter a no \n"); scanf("%d",&n); temp=n; while(n!=0) { rem=n%10; sum=sum+(rem*rem*rem); n=n/10; } if(temp==sum) printf("the no %d is armstrong\n",temp); else printf("the no %d is not armstrong\n",temp); getch(); }
- 46 D. PROGRAM TO FIND FIBONACCI SERIES UP TO GIVEN RANGE 4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS
AIM: Program to write the fibonacci series up to a given number #include<stdio.h> #include<conio.h> void main() { int n; int a=0,b=1,c; clrscr(); printf("enter the n value \n"); scanf("%d",&n); printf(\n %d\t%d\t,a,b); c=a+b; while(c<=n) { printf("%d",c); a=b; b=c; c=a+b; } getch(); }
- 47 -
- 48 4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS E. PROGRAM TO PRINT MATHEMATICAL TABLE AIM: Program to print mathematical table #include<stdio.h> #include<conio.h> void main() { int n,i; clrscr(); printf("enter n \n"); scanf("%d",&n); for(i=1;i<=10;i++) printf("%d * %d = %d\n",n,i,n*i); getch(); }
- 49 4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS F. PROGRAM TO FIND THE VALUE OF SINX AIM: Program to print the sinx serises #include<stdio.h> #include<conio.h> #include<math.h> void main() { float x,sum,term; int i,n; clrscr(); printf("enter the no of terms\n"); scanf("%d",&n); printf("enter the angle in degrees x \n"); scanf("%f",&x); x=(x*3.14)/180; sum=x; term=x; for(i=1;i<=n;i++) { term=(term*(-1)*x*x)/((2*i)*(2*i+1)); sum+=term; } printf("sin valve of given angle is %f",sum); getch(); } INPUT: enter the no of terms 3 OUTPUT: enter the angle in degrees x 30 sin valve of given angle is 0.499770
- 50 4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS G. PROGRAM TO FIND THE VALUE OF COSX AIM: Program to print the cosx serises #include<stdio.h> #include<conio.h> #include<math.h> void main() { float x,sum,term; int i,n; clrscr(); printf("enter the no of terms\n"); scanf("%d",&n); printf("enter the angle in degrees x \n"); scanf("%f",&x); x=(x*3.14)/180; sum=1; term=1; for(i=1;i<=n;i++) { term=(term*(-1)*x*x)/((2*i)*(2*i-1)); sum+=term; } printf("cos valve of given angle is %f",sum); getch(); } INPUT: enter the no of terms 3 OUTPUT: enter the angle in degrees x 30 cos valve of given angle is 0.866158
- 51 4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS H. PROGRAM TO FIND SUM OF DIGITS OF A NUMBER AIM: Program to find the sum of digits of a number #include<stdio.h> #include<conio.h> void main() { int n,temp,rem,ans=0; clrscr(); printf("enter a no \n"); scanf("%d",&n); temp=n; while(n!=0) { rem=n%10; ans+=rem; n=n/10; } printf("the sum of digits of %d is %d\n",temp,ans); getch(); } INPUT: enter a no 234 OUTPUT: the sum of digits of 234 is 9
- 52 4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS I. PROGRAM TO WHETHER GIVEN NO IS PALINDROME OR NOT
AIM: Program to check given number is palindrome or not #include<stdio.h> #include<conio.h> void main() { int n,rev=0,temp,rem; clrscr(); printf("enter a no \n"); scanf("%d",&n); temp=n; while(n!=0) { rem=n%10; rev=rev*10+rem; n=n/10; } if(temp==rev) printf("the no %d is a palindrome\n",temp); else printf("the no %d is not a palindrome\n",temp); getch(); } INPUT: enter a no 121 OUTPUT: the no 121 is a palindrome
- 53 -
- 54 5. PROGRAMS USING ARRAYS A. PROGRAM TO FIND SUM OF ALL ELEMENTS IN THE ARRAY AIM: Program to find sum ao all elements in array #include<stdio.h> #include<conio.h> int main() { int a[20],i,n,sum=0; clrscr(); printf("Enter array size\n"); scanf("%d",&n); printf("Enter any %d elements\n",n); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) sum += a[i]; printf("The sum of all elements is %d\n",sum); getch(); return 0; } INPUT: Enter array size 6 OUTPUT: Enter any 6 elements 98 23 45 67 12 90 The sum of all elements is 335
- 55 5. PROGRAMS USING ARRAYS B. PROGRAM TO FIND MAXIMUM AND MINIMUM NUMBER IN AN ARRAY AIM: Program to find max andd min number in an array #include<stdio.h> #include<conio.h> int main() { int a[20],i,n,min,max; clrscr(); printf("Enter array size\n"); scanf("%d",&n); printf("Enter any %d elements\n",n); for(i=0;i<n;i++) scanf("%d",&a[i]); min = a[1]; max = a[1]; for(i=0;i<n;i++) { if(a[i]<min) min = a[i]; if(a[i]>max) max = a[i]; } printf("Minimum is %d\nMaximum is %d\n",min,max); getch(); return 0; }
INPUT: Enter array size 6 OUTPUT: Enter any 6 elements 78 34 54 12 Minimum is 12 Maximum is 125 30 125
AIM: Program to reverse the array elements #include<stdio.h> #include<conio.h> int main() { int a[20],i,n,temp; clrscr(); printf("Enter array size\n"); scanf("%d",&n); printf("Enter any %d elements\n",n); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n/2;i++) { temp = a[i]; a[i] = a[n-1-i]; a[n-1-i] = temp; } printf("The array elements after reversing are:\n"); for(i=0;i<n;i++) printf("%4d",a[i]); getch(); return 0; } INPUT: Enter array size 5 OUTPUT: Enter any 5 elements 1 2 3 4 5 The array elements after reversing are: 5 4 3 2 1
- 57 5. PROGRAMS USING ARRAYS D. PROGRAM TO PERFORM LINEAR SEARCH AIM: Program to perform linear search #include<stdio.h> #include<conio.h> int main() { int a[20],i,n,x,xloc = -1; clrscr(); printf("Enter array size\n"); scanf("%d",&n); printf("Enter any %d elements\n",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter the element you want to search for\n"); scanf("%d",&x); for(i=0;i<n;i++) { if(a[i]==x) { xloc=i; break; } } if(xloc==-1) printf("%d is not found in the array\n",x); else printf("%d is found at location %d\n",x,xloc); getch(); return 0; } INPUT: Enter array size 6
- 59 Enter array size 5 Enter any 5 elements 1 2 34 5 6 Enter the element you want to search for 89 89 is not found in the array
- 60 5. PROGRAMS USING ARRAYS E. PROGRAM TO PERFORM BINARY SEARCH AIM: Program to perform binary search #include<stdio.h> #include<conio.h> int main() { int a[20],i,n,x,xloc = -1,low,high,mid; clrscr(); printf("Enter array size\n"); scanf("%d",&n); printf("Enter any %d elements in the sorted order\n",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter the element you want to search for\n"); scanf("%d",&x); low = 0;high = n-1; while(low<=high) { mid = (low+high)/2; if(a[mid]==x) { xloc=mid; break; } else if(x<a[mid]) high = mid - 1; else low = mid + 1; } if(xloc==-1) printf("%d is not found in the array\n",x); else printf("%d is found at location %d\n",x,xloc); getch(); return 0; }
- 61 INPUT: Enter array size 6 OUTPUT: Enter any 6 elements 78 45 67 23 14 49 Enter the element you want to search for 23 23 is found at location 3 Enter array size 5 Enter any 5 elements 1 2 34 5 6 Enter the element you want to search for 89 89 is not found in the array
- 62 5. PROGRAMS USING ARRAYS F. PROGRAM TO PERFORM BUBBLE SORT AIM: Program to perform bubble sort #include<stdio.h> #include<conio.h> int main() { int a[20],i,j,n,temp; clrscr(); printf("Enter array size\n"); scanf("%d",&n); printf("Enter any %d elements\n",n); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=1;i<=n-1;i++) { for(j=0;j<n-i;j++) { if(a[j]>a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } printf("After sorting, the element are\n"); for(i=0;i<n;i++) printf("%4d",a[i]); getch(); return 0; } INPUT: Enter array size 6 OUTPUT: Enter any 6 elements
- 63 78 95 62 30 12 39 After sorting, the element are 12 30 39 62 78 95 5. PROGRAMS USING ARRAYS G. PROGRAM TO PERFORM SELECTION SORT
AIM: Progrtam to perform selection sort #include<stdio.h> #include<conio.h> int main() { int a[20],i,j,n,temp,maxloc,k; clrscr(); printf("Enter array size\n"); scanf("%d",&n); printf("Enter any %d elements\n",n); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n-1;i++) { maxloc = 0; for(j=0;j<=(n-1-i);j++) { if(a[j]>a[maxloc]) maxloc = j; } temp = a[n-1-i]; a[n-1-i] = a[maxloc]; a[maxloc] = temp; } printf("After sorting, the element are\n"); for(i=0;i<n;i++) printf("%4d",a[i]); getch(); return 0; } INPUT: Enter array size 6
5. PROGRAMS USING ARRAYS H. PROGRAM TO READ AND PRINT A MATRIX AIM: Program to read and print a matrix elements #include<stdio.h> #include<conio.h> int main() { int a[5][5],i,j,n,m; clrscr(); printf("Enter matrix size\n"); scanf("%d%d",&m,&n); printf("Enter matrix elements\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("\nThe Matrix is\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%4d",a[i][j]); printf("\n"); } getch(); return 0; }
- 65 -
INPUT: Enter matrix size 2 3 Enter matrix elements 1 2 3 4 5 6 OUTPUT: The Matrix is 1 2 3 4 5 6
- 66 -
AIM: Program to perform transepose of a matrix #include<stdio.h> #include<conio.h> int main() { int a[5][5],i,j,n,m,temp; clrscr(); printf("Enter matrix size\n"); scanf("%d%d",&m,&n); printf("Enter matrix elements\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("\nThe Matrix is\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%4d",a[i][j]); printf("\n"); } for(i=0;i<m;i++) { for(j=i+1;j<n;j++) { temp = a[i][j]; a[i][j] = a[j][i];
- 67 a[j][i] = temp; } } printf("\nThe Matrix after transpose,is\n"); for(i=0;i<n;i++) { for(j=0;j<m;j++) printf("%4d",a[i][j]); printf("\n"); } getch(); return 0; } INPUT: Enter matrix size 2 3 Enter matrix elements 1 2 3 4 5 6 OUTPUT: The Matrix is 1 2 3 4 5 6 The Matrix after transpose,is 1 4 2 5 3 6
- 68 5. PROGRAMS USING ARRAYS J. PROGRAM TO ADD TWO MATRICES AIM: Program to ass two matrices #include<stdio.h> #include<conio.h> int main() { int a[5][5],b[5][5],c[5][5],i,j,n,m,p,q; clrscr(); printf("Enter first matrix size\n"); scanf("%d%d",&m,&n); printf("Enter second matrix size\n"); scanf("%d%d",&p,&q); if(m!=p || n!=q) printf("Size mismatch, Addition is not possible\n"); else { printf("Enter first matrix elements\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("Enter second matrix elements\n"); for(i=0;i<p;i++) for(j=0;j<q;j++) scanf("%d",&b[i][j]); printf("\nAddition of two Matrices is\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) c[i][j] = a[i][j] + b[i][j]; } for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%4d",c[i][j]); printf("\n"); } }
- 69 getch(); return 0; } INPUT: Enter first matrix size 2 3 Enter second matrix size 2 3 Enter first matrix elements 1 2 3 4 5 6 Enter second matrix elements 6 5 4 3 2 1 OUTPUT: Addition of two Matrices is 7 7 7 7 7 7 Enter first matrix size 2 3 Enter second matrix size 4 3 Size mismatch, Addition is not possible
- 70 -
5. PROGRAMS USING ARRAYS K. PROGRAM TO MULTIPLY TWO MATRICES AIM: Program to multiply two matrices #include<stdio.h> #include<conio.h> int main() { int a[5][5],b[5][5],c[5][5],i,j,k,n,m,p,q; clrscr(); printf("Enter first matrix size\n"); scanf("%d%d",&m,&n); printf("Enter second matrix size\n"); scanf("%d%d",&p,&q); if(n!=p) printf("Not compatible, Multiplication is not possible\n"); else { printf("Enter first matrix elements\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("Enter second matrix elements\n"); for(i=0;i<p;i++) for(j=0;j<q;j++) scanf("%d",&b[i][j]); printf("\nProduct of two Matrices is\n"); 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]; }
- 71 } for(i=0;i<m;i++) { for(j=0;j<q;j++) printf("%4d",c[i][j]); printf("\n"); } } getch(); return 0; } INPUT: Enter first matrix size 2 3 Enter second matrix size 4 3 OUTPUT: Not compatible, Multiplication is not possible Enter first matrix size 2 3 Enter second matrix size 3 1 Enter first matrix elements 1 2 3 4 5 6 Enter second matrix elements 1 2 3 Product of two Matrices is
- 72 14 32
5. PROGRAMS USING ARRAYS L. PROGRAM TO COUNT FREQUENCY OF OCCURRENCES OF CHARACTERS AND SPECIAL CHARACTERS (\N,\T AND SPACES) AIM: Program to count occurrences of charaters and special characters #include<stdio.h> #include<conio.h> int main() { char ch,str[80]; int i,nl,nt,nd,nc,ns; nl = nt = nd = ns = nc = 0; clrscr(); printf("Enter a character string,to stop enter EOF \n"); i=0; while((ch=getchar())!=EOF) { str[i] = ch; i++; } str[i] = '\0'; printf("\nThe entered string is\n %s",str); for(i=0;str[i]!='\0';i++) { if(str[i]=='\n') nl++; else if(str[i]==' ') ns++; else if(str[i]=='\t') nt++; else if(str[i]>='0' && str[i]<='9') nd++; else nc++;
- 73 } printf("\nNo. of characters = %d\n",nc); printf("No. of digits = %d\n",nd); printf("No. of newlines = %d\n",nl); printf("No. of tabs = %d\n",nt); printf("No. of spaces = %d\n",ns); getch(); return 0; }
INPUT: Enter a character string,to stop enter EOF 43u04 jrir 59jgiogfg jhvcvsopf wrkjrr23-054328324 ^Z The entered string is 43u04 jrir 59jgiogfg jhvcvsopf wrkjrr23-054328324 OUTPUT: No. No. No. No. No. of of of of of characters = 28 digits = 17 newlines = 5 tabs = 7 spaces = 3
AIM: Program to calculate factorial of an integer #include<stdio.h> #include<conio.h> int main() { int n; long ans,fact(int n); clrscr(); printf("Enter an integer\n"); scanf("%d",&n); ans = fact(n); printf("Factorial of %d is %ld\n",n,ans); getch(); return 0; } long fact(int n) { int i; long product=1; for(i=1;i<=n;i++) product *= i; return product; }
INPUT: Enter an integer 10 OUTPUT: Factorial of 10 is 3628800 Enter an integer 5 Factorial of 5 is 120
- 75 6. PROGRAMS USING FUNCTIONS B. FUNCTION TO CALCULATE NCR AND NPR (USING FUNCTION) AIM: Program to calculate ncr and npr #include<stdio.h> #include<conio.h> int main() { int n,r; long ans; long fact(int n); clrscr(); printf("Enter n and r values\n"); scanf("%d%d",&n,&r); printf("%dC%d is %d\n",n,r,ncr(n,r)); printf("%dP%d is %d\n",n,r,npr(n,r)); getch(); return 0; } long fact(int n) { int i; long product=1; for(i=1;i<=n;i++) product *= i; return product; } int ncr(int n,int r) { return fact(n)/(fact(n-r)*fact(r)); } int npr(int n,int r) { return fact(n)/fact(n-r); } INPUT: Enter n and r values 10 3 OUTPUT:
6. PROGRAMS USING FUNCTIONS C. FUNCTION TO CALCULATE FACTORIAL OF AN INTEGER AIM: Program to calculate factorial of an integer #include<stdio.h> #include<conio.h> int main() { int n; long ans; long fact(int n); clrscr(); printf("Enter an integer\n"); scanf("%d",&n); ans = fact(n); printf("Factorial of %d is %ld\n",n,ans); getch(); return 0; } long fact(int n) { if(n<=1) return 1; else return n*fact(n-1); } INPUT: Enter an integer 10 OUTPUT: Factorial of 10 is 3628800 Enter an integer
- 77 5 Factorial of 5 is 120
- 78 6. PROGRAMS USING FUNCTIONS D. PROGRAM TO PRINT PRIME NUMBERS IN THE GIVEN RANGE (USING FUNCTION) AIM: Program to print prime numbers in the given range #include<stdio.h> #include<conio.h> int main() { int m,n; void primerange(int,int); clrscr(); printf("Enter the range\n"); scanf("%d%d",&m,&n); printf("The prime numbers in the given range are\n"); primerange(m,n); getch(); return 0; } void primerange(int m,int n) { int i,j; for(i=m;i<=n;i++) { for(j=2;j<i;j++) { if(i % j == 0) break; } if(i==j) printf("%d\n",i); } }
- 79 -
INPUT: Enter the range 1 50 OUTPUT: The prime numbers in the given range are 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
- 80 6. PROGRAMS USING FUNCTIONS E. PROGRAM TO PRINT ARMSTRONG NUMBERS IN THE GIVEN RANGE (USING FUNCTION) AIM: Program to print Armstrong numbers in the given range #include<stdio.h> #include<conio.h> #include<math.h> int main() { int m,n; void armrange(int,int); clrscr(); printf("Enter the range\n"); scanf("%d%d",&m,&n); printf("The armstrong numbers in the given range are\n"); armrange(m,n); getch(); return 0; } void armrange(int m,int n) { int i,j,sum,rem,num; for(i=m;i<=n;i++) { sum=0; num = i; while(num != 0) { rem = num % 10; num = num / 10; sum +=pow(rem,3); } if(i==sum) printf("%d\n",i); } }
- 81 INPUT: Enter the range 1 1000 OUTPUT: The armstrong numbers in the given range are 1 153 370 371 407
- 82 -
6. PROGRAMS USING FUNCTIONS F. PROGRAM TO SWAP TWO INTEGER NUMBERS (USING FUNCTION) AIM: Program to swap two integers #include<stdio.h> #include<conio.h> #include<math.h> int main() { int m,n; void swap(int*,int*); clrscr(); printf("Enter any two integers\n"); scanf("%d%d",&m,&n); printf("The integers before swapping: m= %d, n = %d\n",m,n); swap(&m,&n); printf("The integers after swapping: m = %d,n = %d\n",m,n); getch(); return 0; } void swap(int *m,int *n) { int temp; temp = *m; *m = *n; *n = temp; } INPUT: Enter any two integers 20 30 OUTPUT:
- 83 The integers before swapping: m= 20, n = 30 The integers after swapping: m = 30,n = 20
6. PROGRAMS USING FUNCTIONS G. PROGRAM TO PRINT FIBONACCI SERIES UP TO A GIVEN NUMBER (USING FUNCTION) AIM: Program to print fibonacci series up to a given number #include<stdio.h> #include<conio.h> int main() { int n; void fibseries(int); clrscr(); printf("Enter the number\n"); scanf("%d",&n); printf("The fibonacci series upto %d\n",n); fibseries(n); getch(); return 0; } void fibseries(int n) { int fib1=0,fib2=1,fib3=1; printf("%d\n%d\n",fib1,fib2); while(fib3<=n) { printf("%d\n",fib3); fib1 = fib2; fib2 = fib3; fib3 = fib1 + fib2; } } INPUT: Enter the number 20 OUTPUT:
- 84 The fibonacci series upto 20 0 1 1 2 3 5 8 13 6. PROGRAMS USING FUNCTIONS H. PROGRAM TO PRINT NTH FIBONACCI NUMBER (USING RECURSIVE FUNCTION) AIM: Program to print nth fibonacci number #include<stdio.h> #include<conio.h> int main() { int n; int fib(int); clrscr(); printf("Enter the number\n"); scanf("%d",&n); printf("The %dth fibonacci number = %d\n",n,fib(n)); getch(); return 0; } int fib(int n) { if(n==1) return 0; else if(n==2) return 1; else return fib(n-1)+fib(n-2); }
- 85 OUTPUT: The 5th fibonacci number = 3 Enter the number 20 The 20th fibonacci number = 4181
6. PROGRAMS USING FUNCTIONS I. PROGRAM TO FIND GCD OF TWO INTEGERS (USING RECURSIVE FUNCTION) AIM: Program to find GCD of two numbers #include<stdio.h> #include<conio.h> int main() { int m,n; int gcd(int,int); clrscr(); printf("Enter any two integers\n"); scanf("%d%d",&m,&n); printf("The GCD of %d and %d is = %d\n",m,n,gcd(m,n)); getch(); return 0; } int gcd(int m,int n) { if(n==0) return m; else return gcd(n,m%n); }
- 86 INPUT: Enter any two integers 125 36 OUTPUT: The GCD of 125 and 36 is = 1 Enter any two integers 24 8 The GCD of 24 and 8 is = 8
6. PROGRAMS USING FUNCTIONS J. PROGRAM TO FIND SUM OF ALL ELEMENTS IN AN ARRAY (USING FUNCTION) AIM: Program to find sum of all elements in an array #include<stdio.h> #include<conio.h> int main() { int a[20],i,n; clrscr(); printf("Enter array size\n"); scanf("%d",&n); printf("Enter any %d elements\n",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("The sum of all elements is %d\n",arraysum(a,n)); getch(); return 0; } int arraysum(int a[],int n) { int i,sum=0; for(i=0;i<n;i++) sum += a[i]; return sum;
- 87 } INPUT: Enter array size 6 OUTPUT: Enter any 6 elements 98 23 45 67 12 90 The sum of all elements is 335 6. PROGRAMS USING FUNCTIONS K. PROGRAM TO PERFORM BUBBLE SORT (USING FUNCTION) AIM: Program to perform bubble sort using function #include<stdio.h> #include<conio.h> int main() { int a[20],i,n; void bubblesort(int [],int); clrscr(); printf("Enter array size\n"); scanf("%d",&n); printf("Enter any %d elements\n",n); for(i=0;i<n;i++) scanf("%d",&a[i]); bubblesort(a,n); printf("After sorting, the elements are\n"); for(i=0;i<n;i++) printf("%4d",a[i]); getch(); return 0; } void bubblesort(int a[],int n)
- 88 { int i,j,temp; for(i=1;i<=n-1;i++) { for(j=0;j<n-i;j++) { if(a[j]>a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } }
- 89 INPUT: Enter array size 6 OUTPUT: Enter any 6 elements 78 95 62 30 12 39 After sorting, the element are 12 30 39 62 78 95
- 90 6. PROGRAMS USING FUNCTIONS L. PROGRAM TO MULTIPLY TWO MATRICES (USING FUNCTION) AIM: Program to multiply two numbers #include<stdio.h> #include<conio.h> int main() { int a[5][5],b[5][5],c[5][5],i,j,k,n,m,p,q; void matrixmul(); clrscr(); printf("Enter first matrix size\n"); scanf("%d%d",&m,&n); printf("Enter second matrix size\n"); scanf("%d%d",&p,&q); if(n!=p) printf("Not compatible, Multiplication is not possible\n"); else { printf("Enter first matrix elements\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("Enter second matrix elements\n"); for(i=0;i<p;i++) for(j=0;j<q;j++) scanf("%d",&b[i][j]); matrixmul(a,b,c,m,n,q); printf("\nProduct of two Matrices is\n"); for(i=0;i<m;i++) { for(j=0;j<q;j++) printf("%4d",c[i][j]); printf("\n"); } } getch(); return 0; } void matrixmul(int a[][5],int b[][5],int c[][5],int m,int n,int q) {
- 91 int i,j,k; 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]; } } } INPUT: Enter first matrix size 2 3 OUTPUT: Enter second matrix size 4 3 Not compatible, Multiplication is not possible Enter first matrix size 2 3 Enter second matrix size 3 1 Enter first matrix elements 1 2 3 4 5 6 Enter second matrix elements 1 2 3 Product of two Matrices is 14 32
- 92 -
6. PROGRAMS USING FUNCTIONS M. PROGRAM TO REVERSE THE CONTENTS OF THE GIVEN STRING BY USING USER DEFINED FUNCTION AIM: Program to reverse the contents of the given string #include<stdio.h> #include<conio.h> void str_rev(char *s) { int i,j; char temp; i=0; j=0; while(s[j+1]!='\0') j++; while(i<j) { temp=s[i]; s[i]=s[j]; s[j]=temp; i++; j--; } } void main() { char a[20]; clrscr(); printf("enter any string"); gets(a); str_rev(a); printf("reverse string is %s",a); getch(); } INPUT:
- 93 enter any string vasavi OUTPUT: reverse string is ivasav 6. PROGRAMS USING FUNCTIONS N. PROGRAM TO FIND THE LENGTH OF THE GIVEN STRING BY USING USER DEFINED FUNCTION AIM: Program to find the length of the given string by using user defined function #include<stdio.h> #include<conio.h> int str_len(char *s) { int i; i=0; while(s[i] !='\0') i++; return (i); } void main() { char a[20]; int ans; clrscr(); printf("enter any string"); gets(a); ans=str_len(a); printf("\n length of %s is %d",a,ans); getch(); } INPUT: enter any string bala krishna
- 95 6. PROGRAMS USING FUNCTIONS O. PROGRAM TO COPY THE CONTENTS OF THE GIVEN STRING TO ANOTHER BY USING USER DEFINED FUNCTION AIM: Program to copy the contents of the given string to anpther by susing user defined function #include<stdio.h> #include<conio.h> void str_copy(char *d,char *s) { int i; i=0; while(s[i]!='\0') { d[i]=s[i]; i++; } d[i]='\0'; return; } void main() { char a[20],b[20]; clrscr(); printf("enter any string"); gets(a); str_copy(b,a); printf("\n destination string is %s ",b); getch(); } INPUT: enter any string vasavi OUTPUT: destination string is vasavi
- 96 6. PROGRAMS USING FUNCTIONS P. PROGRAM TO APPEND THE CONTENTS OF THE GIVEN STRING TO ANOTHER BY USING USER DEFINED FUNCTION AIM: Program to append the contents of the given string to another by using user defined function #include<stdio.h> #include<conio.h> void str_cat(char *d,char *s) { int i,j; i=0; while(d[i]!='\0') i++; j=0; while(s[j]!='\0') { d[i]=s[j]; i++; j++; } d[i]='\0'; return; } void main() { char a[20],b[20]; clrscr(); printf("enter any string"); gets(a); printf("enter another string"); gets(b); str_cat(b,a); printf("\n destination string is %s ",b); getch(); } INPUT: enter any string vasavi OUTPUT: enter another stringcollege destination string is college vasavi
- 97 6. PROGRAMS USING FUNCTIONS Q. PROGRAM TO COMPARE THE CONTENTS OF GIVEN TWO STRINGS BY USING USER DEFINED FUNCTION AIM: Program to compare the contents of given two strings by using user defined functions #include<stdio.h> #include<conio.h> int str_cmp(char *a, char *b) { int i=0; while(a[i]!='\0' && b[i]!='\0') { if(a[i]!=b[i]) return (a[i]-b[i]); i++; } return(a[i]-b[i]); } void main() { char a[20],b[20]; int ans; clrscr(); printf("enter any string"); gets(a); printf("enter another string"); gets(b); ans=str_cmp(a,b); printf("\n the difference between %s and %s is %d",a,b,ans); getch(); } INPUT: enter any string rama enter another string krishna OUTPUT: the difference between rama and krishna is 7
- 98 7. PROGRAMS USING POINTERS A. PROGRAM TO REVERSE THE CONTENTS OF THE GIVEN STRING BY USING USER DEFINED FUNCTION WITH POINTERS AIM: Program to reverse the ontents of the given string by using user defined functions with pointers #include<stdio.h> #include<conio.h> void str_rev(char *s) { char *p,*q; char temp; p=s; q=s; while(*(q+1) !='\0') q++; while(p<q) { temp=*p; *p=*q; *q=temp; p++; q--; } } void main() { char a[20]; clrscr(); printf("enter any string"); gets(a); str_rev(a); printf("reverse string is %s",a); getch(); } INPUT: enter any string ramana OUTPUT: reverse string is anamar
- 99 7. PROGRAMS USING POINTERS B. PROGRAM TO FIND THE LENGTH OF THE GIVEN STRING BY USING USER DEFINED FUNCTION WITH POINTERS AIM: Program to find the length of the given string by using user defined functions with pointers #include<stdio.h> #include<conio.h> int str_len(char *s) { char *p; p=s; while(*p !='\0') p++; return (p-s); } void main() { char a[20]; int ans; clrscr(); printf("enter any string"); gets(a); ans=str_len(a); printf("\n length of %s is %d",a,ans); getch(); } INPUT: enter any string bala krishna
- 100 7. PROGRAMS USING POINTERS C. PROGRAM TO COPY THE CONTENTS OF THE GIVEN STRING TO ANOTHER BY USING USER DEFINED FUNCTION WITH POINTERS AIM: Program to copy the contents of the given string to another by using user defined functions with pointers #include<stdio.h> #include<conio.h> void str_copy(char *d,char *s) { char *p,*q; p=s; q=d; while(*p !='\0') { *q = *p; p++; q++; } *q ='\0'; return; } void main() { char a[20],b[20]; clrscr(); printf("enter any string"); gets(a); str_copy(b,a); printf("\n destination string is %s ",b); getch(); } INPUT: enter any string vasavi OUTPUT: destination string is vasavi
- 101 7. PROGRAMS USING POINTERS D. PROGRAM TO APPEND THE CONTENTS OF THE GIVEN STRING TO ANOTHER BY USING USER DEFINED FUNCTION AIM: Program to append the contents of the given string to another by using user defined function #include<stdio.h> #include<conio.h> void str_cat(char *d,char *s) { char *p,*q; p=s; q=d; while(*q!='\0') q++; while(*p !='\0') { *q=*p; p++; q++; } *q ='\0'; return; } void main() { char a[20],b[20]; clrscr(); printf("enter any string"); gets(a); printf("enter another string"); gets(b); str_cat(b,a); printf("\n destination string is %s ",b); getch(); } INPUT: enter any string abcd enter another stringpqrs OUTPUT: destination string is pqrs abcd
- 102 7. PROGRAMS USING POINTERS D. Program to compare the contents of given two strings by using user defined function
AIM: Program to compare the contents of given two strings by using user defined functions #include<stdio.h> #include<conio.h> int str_cmp(char *a, char *b) { char *p,*q; p=a; q=b; while(*p !='\0' && *q!='\0') { if(*p != *q) return (*p - *q); p++; q++; } return(*p - *q); } void main() { char a[20],b[20]; int ans; clrscr(); printf("enter any string"); gets(a); printf("enter another string"); gets(b); ans=str_cmp(a,b); printf("\n the difference between %s and %s is %d",a,b,ans); getch(); } INPUT: enter any stringrama enter another stringramana OUTPUT: the difference between rama and ramana is 110
- 103 8.PROGRAMS USING STRINGS A. PROGRAM TO ABBREVIATE THE CONTENTS OF THE CONTENTS OF GIVEN STRING AIM: Proram to abbreviate the contens of the given string #include<stdio.h> #include<conio.h> main() { char s[100]; int i; clrscr(); printf("\n Enter a String\n"); gets(s); i=0; while(s[i]== ' ') i++; putchar(s[i]); putchar(' '); while(s[i]!='\0') { if(s[i]==' ' && s[i+1] != ' ') { putchar(s[i+1]); putchar(' '); } i++; } getch(); } INPUT: enter a string Vasavi College Engg OUTPUT: VCE
- 104 8.PROGRAMS USING STRINGS B. DEFINE A STRUCTURE TO REPRESENT RATIONAL NUMBER AND WRITE A PROGRAM TO PERFORM THE ARITHMETICAL OPERATIONS ON RATIONAL NUMBERS AIM: Program to erform the arithmetical operations on rational numbers using structures #include<stdio.h> #include<conio.h> struct rational { int nr; int dr; }; main( ) { struct rational r1,r2,ans; clrscr(); printf("\n Enter NR and DR of firtst rational number"); scanf("%d%d",&r1.nr,&r1.dr); printf("\n Enter NR and DR of second rational number"); scanf("%d%d",&r2.nr,&r2.dr); /* Addition*/ ans.nr=r1.nr*r2.dr + r1.dr* r2.nr; ans.dr= r1.dr* r2.dr; printf("\n Sum is %d / %d",ans.nr,ans.dr); /* substraction*/ ans.nr=r1.nr*r2.dr - r1.dr* r2.nr; ans.dr= r1.dr* r2.dr; printf("\n Difference is %d / %d",ans.nr,ans.dr); /* Multiplication*/ ans.nr=r1.nr*r2.nr; ans.dr= r1.dr* r2.dr; printf("\n product is %d / %d",ans.nr,ans.dr);
- 105 /* Division*/ ans.nr=r1.nr*r2.dr; ans.dr= r1.dr* r2.nr; printf("\n division INPUT: is %d / %d",ans.nr,ans.dr); getch(); }
INPUT: Enter NR and DR of firtst rational number 23 Enter NR and DR of second rational number 14 OUTPUT: Sum is 11 / 12 Difference is 5 / 12 product is 2 / 12 division RESULT: is 8 / 3
- 106 8.PROGRAMS USING STRINGS C. DEFINE A STRUCTURE TO REPRESENT COMPLEX NUMBER AND WRITE A PROGRAM TO PERFORM THEARITHMETICAL OPERATIONS ON COMPLEX NUMBERS AIM: Program to perform the arithmetical operations on complex numbers using structures #include<stdio.h> #include<conio.h> struct complex { float rp; float ip; }; main() { struct complex c1,c2,ans; clrscr(); printf("\n Enter RP and IP of first complex number"); scanf("%f%f",&c1.rp,&c1.ip); printf("\n Enter RP and IP of second complex number"); scanf("%f%f",&c2.rp,&c2.ip); /* addition*/ ans.rp= c1.rp + c2.rp; ans.ip= c1.ip + c2.ip; printf(" Sum is ( %f , %f )",ans.rp, ans.ip); /* Substraction*/ ans.rp= c1.rp - c2.rp; ans.ip= c1.ip - c2.ip; printf(" Sum is ( %f , %f )",ans.rp, ans.ip); getch(); } INPUT: Enter RP and IP of first complex number 12 Enter RP and IP of second complex number 34 OUTPUT: Sum is ( 4.000000 , 6.000000 ) Sum is ( -2.000000 , -2.000000 )
- 107 8.PROGRAMS USING STRINGS D. DEFINE A STRUCTURE TO REPRESENT RATIONAL NUMBER AND WRITE A PROGRAM TO PERFORM THE ARITHMETICAL OPERATIONS ON RATIONAL NUMBERS BY USING FUNCTIONS AIM: Program to perform the arithmetical operations on rational numbers by using structures and functions #include<stdio.h> #include<conio.h> struct rational { int nr; int dr; }; typedef struct rational rat; rat sum( rat r1, rat r2) { rat ans; ans.nr=r1.nr*r2.dr + r1.dr* r2.nr; ans.dr= r1.dr* r2.dr; return ans; } rat sub( rat r1, rat r2) { rat ans; ans.nr=r1.nr*r2.dr - r1.dr* r2.nr; ans.dr= r1.dr* r2.dr; return ans; } rat mul( rat r1, rat r2) { rat ans; ans.nr=r1.nr*r2.nr; ans.dr= r1.dr* r2.dr; return ans; } rat div( rat r1, rat r2) { rat ans;
- 108 ans.nr=r1.nr*r2.dr; ans.dr= r1.dr* r2.nr; return ans; } main() { rat r1,r2,ans; clrscr(); printf("\n Enter NR and DR of firtst rational number"); scanf("%d%d",&r1.nr,&r1.dr); printf("\n Enter NR and DR of second rational number"); scanf("%d%d",&r2.nr,&r2.dr); /* Addition*/ ans=sum(r1,r2); printf("\n sum is %d / %d",ans.nr,ans.dr); /* substraction*/ ans=sub(r1,r2); printf("\n Difference is %d / %d",ans.nr,ans.dr); /* Multiplication*/ ans=mul(r1,r2); printf("\n product is %d / %d",ans.nr,ans.dr); /* Division*/ ans=div(r1,r2); printf("\n division INPUT: is %d / %d",ans.nr,ans.dr); getch(); } INPUT: Enter NR and DR of firtst rational number 12 Enter NR and DR of second rational number 3 4 OUTPUT: sum is 10 / 8 Difference is -2 / 8 product is 3 / 8 division RESULT: is 4 / 6
E. DEFINE A STRUCTURE TO REPRESENT COMPLEX NUMBER AND WRITE A PROGRAM TO PERFORM THE ARITHMETICAL OPERATIONS ON COMPLEX NUMBERS BY USING FUNCTIONS AIM: Program to perform arithmetical operations on complex numbers by usinmg structures and functions #include<stdio.h> #include<conio.h> struct complex { float rp; float ip; }; typedef struct complex comp; comp sum ( comp c1, comp c2) { comp ans; ans.rp= c1.rp + c2.rp; ans.ip= c1.ip + c2.ip; return ans; } comp sub ( comp c1, comp c2) { comp ans; ans.rp= c1.rp - c2.rp; ans.ip= c1.ip - c2.ip; return ans; } main() { struct complex c1,c2,ans; clrscr(); printf("\n Enter RP and IP of first complex number"); scanf("%f%f",&c1.rp,&c1.ip); printf("\n Enter RP and IP of second complex number"); scanf("%f%f",&c2.rp,&c2.ip);
- 110 /* addition*/ ans=sum(c1,c2); printf(" Sum is ( %f , %f )",ans.rp, ans.ip); /* Substraction*/ ans=sub(c1,c2); printf(" Sum is ( %f , %f )",ans.rp, ans.ip); getch(); } INPUT: Enter RP and IP of first complex number 12 Enter RP and IP of second complex number 34 OUTPUT: Sum is ( 4.000000 , 6.000000 ) Sum is ( -2.000000 , -2.000000 )
- 111 9.PROGRAMS USING STRUCTURES & UNIONS A. WRITE A PROGRAM TO GENERATE STUDENT RECORDS WITH TOTAL AVERAGE IF MARKS OF 3 SUBJECTS ARE GIVEN AS INPUTS BY USING AN ARRAY OF STUDENT STRUCTURES. AIM: Program to generate student records with total and average by taking 3 subjects marks as input #include<stdio.h> #include<conio.h> struct student { int sno; char name[20]; int m1,m2,m3; int tot; float avg; }; main() { struct student s[10]; int n,i; clrscr(); printf("\n Enter no of students"); scanf("%d",&n); printf("\n Enter no name and marks of 3 subjects of %d students",n); for(i=0;i<n ; i++) { fflush(stdin); scanf("%d",&s[i].sno); fflush(stdin); gets(s[i].name); fflush(stdin); scanf("%d%d%d",&s[i].m1,&s[i].m2,&s[i].m3); if(i<n-1) printf("\n mext student\n \n"); } for(i=0;i<n;i++) { s[i].tot=s[i].m1+ s[i].m2 +s[i].m3;
- 112 s[i].avg=(float)s[i].tot/3; } printf("\n STUDENT REPORTS ARE\n\n\n"); for( i=0; i<n ; i++) { printf("\n \n \n"); printf("\n ROLL NO = %d",s[i].sno); printf("\n NAME = %s",s[i].name); printf("\n TOTAL = %d",s[i].tot); printf("\n AVERAGE = %f",s[i].avg); } getch(); }
INPUT: Enter no of students 3 OUTPUT: Enter no name and marks of 3 subjects of 3 students; STUDENT REPORTS ARE ROLL NO = 1001 NAME = rama TOTAL = 265 AVERAGE = 88.333336 ROLL NO = 1002 NAME = krishna TOTAL = 247 AVERAGE = 82.333336 ROLL NO = 1003 NAME = ranga TOTAL = 190 AVERAGE = 63.333332
- 113 9.PROGRAMS USING STRUCTURES & UNIONS B. WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF POINTERS TO STRUCTURES TO CALCULATE THE TOTAL AND AVERAGE OF THE GIVEN STUDENT
AIM: Program to calculate the total, average of a given student using pointers to tructures #include<stdio.h> #include<conio.h> struct student { int sno; char name[20]; int m1,m2,m3; int tot; float avg; }; main() { struct student s,*p; clrscr(); p=&s; printf("\n Enter no name and marks of 3 subjects of a student\n"); fflush(stdin); scanf("%d",&p->sno); fflush(stdin); gets(p->name); fflush(stdin); scanf("%d%d%d",&p->m1,&p->m2,&p->m3); p->tot=p->m1+ p->m2 +p->m3; p->avg=(float)p->tot/3; printf("\n STUDENT REPORT IS\n\n"); printf("\n ROLL NO = %d",p->sno);
- 114 printf("\n NAME = %s",p->name); printf("\n TOTAL = %d",p->tot); printf("\n AVERAGE = %f",p->avg); getch(); }
INPUT: Enter no name and marks of 3 subjects of a student 1001 rama 90 89 87 OUTPUT: STUDENT REPORT IS ROLL NO = 1001 NAME = rama TOTAL = 266 AVERAGE = 88.666664
- 115 9.PROGRAMS USING STRUCTURES & UNIONS C. PROGRAM TO PRINT TOMORROWS DATE IF TO-DAYS DATE WAS GIVEN AIM: Program toprint tomorrows date if todays darte is given #include<stdio.h> #include<conio.h> struct date { int dd; int mm; int yy; }; void tommorrow( struct date *p) { int month[]={ 31,28,31,30,31,30,31,31,30,31,30,31}; if((p->yy % 4 == 0 && p->yy % 100 != 0) || (p->yy % 400 == 0)) month[1]=29; if(p->dd<1 || p->dd > month[p->mm - 1] || p->mm < 1 || p->mm > 12 || p->yy < 0) printf("\n Invalid date"); else { p->dd++; if(p->dd > month[p->mm - 1]) { p->dd=1; p->mm++; if(p->mm >12) { p->mm=1; p->yy++; } } } } main() { struct date d,*p=&d; clrscr(); printf("\n Enter today date");
- 117 10. Programs using Files A. C PROGRAM TO CREATE A FILE AIM: Program to create a file #include<stdio.h> #include<conio.h> void main() { FILE *fp; char ch; clrscr(); fp = fopen("text.dat","w"); printf("Enter text ,to stop press Ctrl-Z\n"); while((ch=getchar())!=EOF) { fputc(ch,fp); } fclose(fp); } INPUT: Enter text ,to stop press Ctrl-Z This is my first program in C language to create a file. Bye. To see the contents of this file open text.dat in any editor program. ^Z File Saved on disk text.dat: This is my first program in C language to create a file. Bye. To see the contents of this file open text.dat in any editor program.
- 118 10. PROGRAMS USING FILES B. C PROGRAM TO PRINT THE CONTENTS OF THE FILE ON THE MONITOR AIM: Program to print the contens of the file on the monitor #include<stdio.h> #include<conio.h> void main() { FILE *fp; char ch; clrscr(); fp = fopen("text.dat","r"); if(fp==NULL) printf("No such file\n"); else { printf("File contents are:\n"); while(!feof(fp)) { ch = fgetc(fp); putchar(ch); } fclose(fp); } getch(); } INPUT: Given text.dat File contents are: This is my first program in C to create a file with some text. Bye To see the contents of the file open text.dat in any editor. Given example.dat No such file
- 119 10. PROGRAMS USING FILES C. C PROGRAM TO COPY ONE FILE INTO ANOTHER FILE
AIM: Program to copy one file in to another #include<stdio.h> #include<conio.h> void main() { FILE *fp; char ch; clrscr(); fp = fopen("example.dat","r"); if(fp==NULL) printf("No such file\n"); else { printf("File contents are:\n"); while(!feof(fp)) { ch = fgetc(fp); putchar(ch); } fclose(fp); } getch(); } INPUT: source file: text.dat destination file: example.dat file copied
- 120 -
10. PROGRAMS USING FILES D. C PROGRAM TO CREATE A FILE OF STUDENT RECORDS AIM: Program to create a file od students records #include<stdio.h> #include<conio.h> struct student { int rno; char name[20]; int marks; }; void main() { int i,n; struct student s; FILE *fp; clrscr(); printf("How many records you want to store in the file\n"); scanf("%d",&n); printf("Enter any %d student records\n",n); printf("For each record enter rollnumber,name and marks\n"); fp = fopen("student.dat","wb"); for(i=1;i<=n;i++) { printf("enter record\n"); scanf("%d",&s.rno); fflush(stdin); gets(s.name); scanf("%d",&s.marks); fwrite(&s,sizeof(s),1,fp); } fclose(fp); printf("file created\n"); getch(); }
- 121 -
INPUT: How many records you want to store in the file 2 Enter any 2 student records For each record enter rollnumber,name and marks OUTPUT: enter record 100 rama rao 600 enter record 200 sita kumari 500 file created
- 122 -
10. PROGRAMS USING FILES E. C PROGRAM TO DISPLAY STUDENT RECORDS BY READING FROM A FILE AIM: Program to display student records by reading from a file #include<stdio.h> #include<conio.h> struct student { int rno; char name[20]; int marks; }; void main() { int i,n; struct student s; FILE *fp; clrscr(); fp = fopen("student.dat","rb"); while(fread(&s,sizeof(s),1,fp)==1) { printf("%d\t%s\t%d\n",s.rno,s.name,s.marks); } fclose(fp); getch(); }
- 123 -
#include<iostream.h> int main() { float n1,n2,sum,avg; cout<<"enter two numbers"; cin>>n1; cin>>n2; sum=n1+n2; avg=sum/2; cout<<"sum= "<<sum<<"\n"; cout<<"Average ="<<avg<<"\n"; return 0; }
OUTPUT: input: 10 20 30 15
- 124 -
- 125 -
- 126 -
// function overloading #include<iostream.h> int volume(int); double volume(double,int); long volume(long,int,int); int main() { cout<<volume(10)<<"\n"; cout<<volume(2.5,8)<<endl; cout<<volume(100l,75,15)<<"\n"; } int volume(int s) { return(s*s*s); } double volume(double r,int h) { return(3.14*r*r*h); } long volume(long l,int b,int h) { return(l*b*h); } OUTPUT: Output: 1000,157,112500
- 127 -
- 128 -
- 129 -
- 130 -
AIM: Program to demonstrate working of static member function #include<iostream.h> class test { int code; static int count; public: void setcode(void) { code=++count; } void showcode(void) { cout<<"object number:"<<code<<"\n"; } static void showcount(void) { cout<<"count:"<<count<<"\n"; } }; int test :: count; int main() { test t1,t2; t1.setcode(); t2.setcode(); test::showcount(); test t3; t3.setcode(); test::showcount(); t1.showcode(); t2.showcode(); t3.showcode(); return 0; } OUTPUT: 1,2,3
- 131 -
- 132 -
- 133 -
- 135 -
G. Program to demonstrate constructors AIM: Program to demonstrate working of constructors #include<iostream.h> class code { int id; public: code(){} code(int a) { id=a;} code(code & x) { id=x.id; } void display(void) { cout<<id; } }; int main() { code A(100); code B(A); code C=A; code D; D=A; cout<<"\n id of A:";A.display(); cout<<"\n id of B:";B.display(); cout<<"\n id of C:";C.display(); cout<<"\n id of D:";D.display(); return 0; } OUTPUT: 100,100,100,100
- 136 -
OUTPUT: raviprasad,raviprasadrajesh
- 138 -
- 140 -
- 141 -
#include<iostream.h> class space { int x; int y; int z; public: void getdata(int a,int b,int c); void display(void); void operator-(); }; void space::getdata(int a,int b,int c) { x=a; y=b; z=c; } void space::display(void) { cout<<x<<" "; cout<<y<<" "; cout<<z<<" "; } void space::operator-() { x=-x; y=-y; z=-z; } int main() { space S; S.getdata(10,-20,30); cout<<"S:"; S.display(); -S;
- 143 -
- 144 -
- 145 istream & operator>>(istream &din,vector &b) { for(int i=0;i<size;i++) din>>b.v[i]; return(din); } ostream & operator<<(ostream &dout,vector &b) { dout<<"("<<b.v[0]; for(int i=1;i<size;i++) dout<<","<<b.v[i]; dout<<")"; return(dout); } int x[size]={2,4,6}; int main() { vector m; vector n=x; cout<<"Enter elements of vector m"<<"\n"; cin>>m; cout<<"\n"; cout<<m; vector p,q; p=2*m; q=n*2; cout<<"\n"; // cout<<"p="<<p<<"\n"; // cout<<"q="<<q<<"\n"; cout<<p; cout<<q; return 0; } //input:5 10 15 OUTPUT: m(5,10,15) p(10,20,30) q(4,8,12)
- 146 -
- 147 int n=strlen(t.p); if(m<=n) return(1); else return(0); } void show(const string s) { cout<<s.p; } int main() { string s1="ravi"; string s2="prasad"; string s3="rajesh"; string t1,t2,t3; t1=s1; t2=s2; t3=s1+s3; cout<<"\n t1=";show(t1); cout<<"\n t2=";show(t2); cout<<"\n"; cout<<"\n t3=";show(t3); cout<<"\n\n"; if(t1<=t3) { show(t1); cout<<"smaller than"; show(t3); cout<<"\n"; } else { show(t3); cout<<"smaller than"; show(t1); cout<<"\n"; } return 0; } OUTPUT: ravi prasad ravirajesh ravi smaller than ravirajesh
- 148 -
AIM: Program to demonstrate working of single inheritance using public as access specifier
#include<iostream.h> class B { int a; public: int b; void get_ab(); int get_a(void); void show_a(void); }; class D : public B { int c; public: void mul(void); void display(void); }; void B :: get_ab() { a=5;b=10; } int B::get_a() { return a; } void B:: show_a() { cout<<"a="<<a<<"\n"; } void D::mul() {
- 149 c=b*get_a(); } void D::display() { cout<<"a="<<get_a()<<"\n"; cout<<"b="<<b<<"\n"; cout<<"c="<<c<<"\n\n"; } int main() { D d; d.get_ab(); d.mul(); d.show_a(); d.display(); d.b=20; d.mul(); d.display(); return 0; } OUTPUT: a=5 a=5 b=10 c=50 a=5 b=20 c=100
- 150 -
- 151 show_a(); // cout<<"a="<<get_a()<<"\n"; cout<<"b="<<b<<"\n"; cout<<"c="<<c<<"\n\n"; } int main() { D d; // d.get_ab(); d.mul(); // d.show_a(); d.display(); // d.b=20; d.mul(); d.display(); return 0; } OUTPUT: input 5 10 output 5 10 50 input 12 20 output 12 20 240
- 152 -
- 153 put_number(); put_marks(); cout<<"total="<<total<<"\n"; } }; int main() { result student1; student1.get_number(111); student1.get_marks(75.0,59.8); student1.display(); return 0; }
- 154 -
- 156 -
AIM: Program to demonstrate working of virtual base class #include<iostream.h> class student { protected: int roll_number; public : void get_number(int a) { roll_number=a; } void put_number(void) { cout<<"rollnumber :"<<roll_number<<"\n"; } }; class test : virtual public student { protected: float p1,p2; public: void get_marks(float x,float y) { p1=x; p2=y; } void put_marks(void) { cout<<"marks"<<"\n"<<"part1:"<<p1<<"\npart2:"<<p2<<endl; } }; class sports : public virtual student { protected : float score; public: void get_score(float s) {
- 157 score=s; } void put_score(void) { cout<<"sport wt:"<<score<<"\n"; } }; class INPUT: public test,public sports { float total; public: void display(void) { total=p1+p2+score; put_number(); put_marks(); put_score(); cout<<"total score:"<<total<<"\n"; } }; int main() { result s1; s1.get_number(678); s1.get_marks(77.5,57.5); s1.get_score(8.0); s1.display(); return 0; }
INPUT: rollno:678 OUTPUT: marks obtained:77.5 57.5 sports wt:8 total score:143
- 158 -
OUTPUT: beta initialized alpha initialized gamma initialized x=5 y=10.27 m=20 n=30
- 160 -
- 161 }
- 162 -
- 163 OUTPUT: elder person is: ravi 78.9 elder person is: ravi 78.9
- 164 -
- 165 -
- 166 cout<<"\n Title:"<<title; cout<<"\n Pages:"<<pages; cout<<"\n Price:"<<price; } void tape::display() { cout<<"\n Title:"<<title; cout<<"\n play time:"<<time<<"mins"; cout<<"\n price:"<<price; } int main() { char *title=new char[30]; float price,time; int pages; cout<<"\n ENTER BOOK DETAILS\n"; cout<<"Title:";cin>>title; cout<<"Price:";cin>>price; cout<<"Pages:";cin>>pages; book book1(title,price,pages); cout<<"\n ENTER TAPE DETAILS\n"; cout<<"Title:";cin>>title; cout<<"Price:";cin>>price; cout<<"Play time(mins):";cin>>time; tape tape1(title,price,time); media* list[2]; list[0]=&book1; list[1]=&tape1; cout<<"\n MEDIA DETAILS"; cout<<".......BOOK......"; list[0]->display(); cout<<"\n......TAPE...."; list[1]->display(); return 0; }
- 167 -
INPUT: ENTER BOOK DETAILS Tile: AAA Price :255 Pages:100 ENTER TAPE DETIALS Tile : BBB Price : 75 Play time (min): 60 MEDIA DETAILS OUTPUT: .. BOOK. Tile: AAA Price: 255 Pages: 100 ..TAPE.. Tile: BBB Price: 75 Play time (min): 60
- 168 -
- 170 -
- 172 -
- 173 -
OUTPUT: Roots are real R1 = 2.5 R2 = 2.5 Roots are complex Real part = -1.2 Imaginary part = 1.375
- 174 -
AIM: Program to demonstrate working of exception handling #include<iostream.h> void divide(int x,int y,int z) { cout<<"\n we are inside the function\n"; if((x-y)!=0) { int R=z/(x-y); cout<<"Result="<<R<<"\n"; } else { throw(x-y); } } int main() { try { cout<<"we are inside the try block\n"; divide(10,20,30); divide(10,10,20); } catch(int i) { cout<<"caught the exception\n"; } return 0; } OUTPUT: result =-3 caught the exception