c,c++ labmanual 08

232
- 1 - METHODIST COLLEGE OF ENGINEERING AND TECHNOLOGY KING KOTI ,ABIDS, HYDERABAD 500031 Website: www.methodist.edu.in

Upload: manish-reddy

Post on 24-Oct-2014

113 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: C,C++ LABMANUAL 08

- 1 -

METHODIST COLLEGE OF ENGINEERING AND TECHNOLOGY

KING KOTI ,ABIDS, HYDERABAD 500031

Website: www.methodist.edu.in

Page 2: C,C++ LABMANUAL 08

- 2 -

PROGRAMMING IN C & C++

LABORATORY MANUAL

Hardware : Pentium-II (256MHz)/256MB RAM /40 GB HDDSoftware : Windows XP-SP2/TC3.0

Page 3: C,C++ LABMANUAL 08

- 3 -

INDEX

PROGRAMS

1.SIMPLE C PROGRAMS 9

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 16

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

A. TO DEMONSTRATE THE WORKING OF ARITHMETICAL OPERATORS

Page 4: C,C++ LABMANUAL 08

- 4 - OPERATORS

3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS 22

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 41

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 52

B. PROGRAM TO FIND MAXIMUM AND MINIMUM NUMBER IN AN ARRAY C. PROGRAM TO REVERSE THE ARRAY ELEMENTS IN PLACE

A. PROGRAM TO FIND THE NUMBER IS EVEN OR ODD

A. PROGRAM TO FIND SUM OF FIRST N NATURAL NUMBERS WITH WHILE LOOP

A. PROGRAM TO FIND SUM OF ALL ELEMENTS IN THE ARRAY

Page 5: C,C++ LABMANUAL 08

- 5 - D. PROGRAM TO PERFORM LINEAR SEARCH E. PROGRAM TO PERFORM BINARY SEARCH F. PROGRAM TO PERFORM BUBBLE SORT G. 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 73

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

A. FUNCTION TO CALCULATE FACTORIAL OF AN INTEGER

Page 6: C,C++ LABMANUAL 08

- 6 -

7. PROGRAMS USING POINTERS 99 M

B. PROGRAM TO FIND THE LENGTH OF THE GIVEN STRING BY USING 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 109

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 117

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

A. PROGRAM TO REVERSE THE CONTENTS OF THE GIVEN STRING BY USING USER DEFINED FUNCTION WITH POINTERS

A. PROGRAM TO ABBREVIATE THE CONTENTS OF THE CONTENTS OF GIVEN STRING

Page 7: C,C++ LABMANUAL 08

- 7 - WAS GIVEN10. PROGRAMS USING FILES 124 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

11. Simple C++ Programs 130 A. Average of two numbers B. Demonstration of scope resolution operator C. MUL and Div using inline functions D. Calculation of volume of cube,cylinder,rectangular box Using Function overloading E. Program to find maximum number using function template

12. Programs using classes and objects 135

B. Program on working of static data member C. Program to demonstrate static member function D. Program on friend function

E. Program on complex numbers addition using friend function

F. Program to perform complex numbers sum using friend, constructors

G. Program to demonstrate constructorsH. Program to perform string operationsI. Program to perform matrix reading and search for

elementJ. Program to demonstrate destructor

13. Program on operator overloading 148

B. Program to perform overloading + operator

A. Program to demonstrate

A. Program to perform overloading on unary minus

Page 8: C,C++ LABMANUAL 08

- 8 -C. Program to perform overloading operators using

friend functionsD. Program to perform mathematical operations on

strings

14. Programs on inheritance 155

B. Program to demonstrate single inheritance: privateC. Program to demonstrate multilevel inheritanceD. Program to demonstrate multiple inheritances

15. Programs on polymorphism 163

B. Program to demonstrate constructors in derived class

C. Program to demonstrate pointers to objectsD. Program to demonstrate this pointer

E. Program to demonstrate virtual functionsF. Program to demonstrate runtime polymorphism

16. Template programs 176

B. Program to perform bubble sort using template Functions C. Program to calculate roots of equations using templates

17. Exception handling 182

A. Program to demonstrate single inheritance: public

A. Program to demonstrate virtual base class

A. Program to perform vector operations using

A. Program on exception handling

Page 9: C,C++ LABMANUAL 08

- 9 -

1.SIMPLE C PROGRAMS

AIM: Accepting text from user and printing

#include<stdio.h>#include<conio.h>void main(){clrscr();printf(" C test program");getch();}

OUTPUT:C test program

A. PROGRAM TO PRINT

Page 10: C,C++ LABMANUAL 08

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

}

B. WORKING OF FUNDAMENTAL DATATYPES

Page 11: C,C++ LABMANUAL 08

- 11 -

OUTPUT: x=123.456001 y=20 a=z k=9.345668

1.SIMPLE C PROGRAMS

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

}

INPUT:

C. INTERCHANGE TWO VALUES

Page 12: C,C++ LABMANUAL 08

- 12 -enter the values of a,b53OUTPUT:

a=3 b=5

Page 13: C,C++ LABMANUAL 08

- 13 -

1.SIMPLE C PROGRAMS

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

INPUT:enter the values of p n r1000220

OUTPUT:

simple interest is :400.000000

D. TO CALCULATE SIMPLE INTEREST

Page 14: C,C++ LABMANUAL 08

- 14 -

1.SIMPLE C PROGRAMS

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

INPUT: enter any radious5

OUTPUT:

area is 78.500000 perimeter is 31.400000

E. TO CALCULATE AREA AND PERIMETER OF A

Page 15: C,C++ LABMANUAL 08

- 15 - 1.SIMPLE C PROGRAMS

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

F. TO CALCULATE THE AREA AND PERIMETER OF TRIANGLE

Page 16: C,C++ LABMANUAL 08

- 16 -

1.SIMPLE C PROGRAMS

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 rectangle23.443.4

OUTPUT:

area is 1015.559998perimeter is 133.600006

G. TO FIND AREA AND PERIMETER OF A RECTANGLE

Page 17: C,C++ LABMANUAL 08

- 17 -

2. PROGRAMS TO DEMOSTRATE WORKING OF 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

A. TO DEMONSTRATE THE WORKING OF ARITHMETICAL OPERATORS

Page 18: C,C++ LABMANUAL 08

- 18 -

Page 19: C,C++ LABMANUAL 08

- 19 -

2. PROGRAMS TO DEMOSTRATE WORKING OF 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 3 after ++a a is 4 with ans=a++ a is 5 and ans is 4 with ans=++a a is 6 and ans is 6

B. PROGRAM TO DEMONSTRATE THE WORKING OF INCREMENT AND DECREMENT OPERATORS

Page 20: C,C++ LABMANUAL 08

- 20 -2. PROGRAMS TO DEMOSTRATE WORKING OF 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

C. PROGRAM TO DEMONSTRATE THE WORKING OF INCREMENT AND DECREMENT OPERATORS

Page 21: C,C++ LABMANUAL 08

- 21 -

2. PROGRAMS TO DEMOSTRATE WORKING OF 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(); }

INPUT:enter any three integers19 35 68

OUTPUT:

max is 68

D. TO FIND THE MAX OF TWO NUMBERS BY USING CONDITIONAL OPERATORS

Page 22: C,C++ LABMANUAL 08

- 22 -

2. PROGRAMS TO DEMOSTRATE WORKING OF OPERATORS

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

E. TO FIND THE MAX OF THREE INTEGERS BY USING CONDITIONAL OPERATOR

Page 23: C,C++ LABMANUAL 08

- 23 -2. PROGRAMS TO DEMOSTRATE WORKING OF OPERATORS

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

INPUT:enter any two integers 34 76

OUTPUT:

max no is 76

F. PROGRAM TO FIND THE MAX OF THREE INTEGERS BY USING CONDITIONAL OPERATOR

Page 24: C,C++ LABMANUAL 08

- 24 -

2. PROGRAMS TO DEMOSTRATE WORKING OF 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

G. PROGRAM TO DEMONSTRATE THE WORKING OF RELATIONAL OPERATORS

Page 25: C,C++ LABMANUAL 08

- 25 -

2. PROGRAMS TO DEMOSTRATE WORKING OF 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

H. TO DEMONSTRATE THE WORKING OF SHORTCUT ASSIGNMENT OPERATORS

Page 26: C,C++ LABMANUAL 08

- 26 -

2. PROGRAMS TO DEMOSTRATE WORKING OF 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

I. PROGRAM TO DEMONSTRATE THE WORKING OF BITWISE OPERATORS

Page 27: C,C++ LABMANUAL 08

- 27 -

3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS

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);elseprintf("the no %d is odd",a);getch();

}

INPUT:

enter a no23OUTPUT:

the no 23 is odd

enter a no34the no 34 is even

A. PROGRAM TO FIND THE NUMBER IS EVEN OR ODD

Page 28: C,C++ LABMANUAL 08

- 28 - 3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS

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);elseprintf("the no %d is greater",b);getch();}

INPUT:

enter two nos2 4

OUTPUT:

the no 4 is greater

B. PROGRAM TO FIND GREATEST OF TWO NOS

Page 29: C,C++ LABMANUAL 08

- 29 - 3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS

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);elseprintf("the year %d is not a leap year",year);getch();

}

INPUT:

enter the year1884

OUTPUT:

the year 1884 is leap year

C.PROGRAM TO CHECK WHETHER THE YEAR IS LEAP OR NOT

Page 30: C,C++ LABMANUAL 08

- 30 -3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS

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

D. PROGRAM TO CHECK WHETHER THE GIVEN CHARACTER IS ALPHABET OR DIGIT OR SPECIAL CHARACTER

Page 31: C,C++ LABMANUAL 08

- 31 -3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS

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

E. PROGRAM TO CHECK WHEATHER THE GIVEN CHARACTER IS ALPHABET OR DIGIT OR SPECIAL CHARACTER

Page 32: C,C++ LABMANUAL 08

- 32 -

3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS

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

F. PROGRAM TO CALCULATE THE ROOTS OF A QUADRATIC EQUATION

Page 33: C,C++ LABMANUAL 08

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

Page 34: C,C++ LABMANUAL 08

- 34 -

3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS

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

G. PROGRAM TO CALCULATE THE TOTAL , AVERAGE AND GRADE OF A STUDENT

Page 35: C,C++ LABMANUAL 08

- 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 subjects89 76 57

OUTPUT:

total = 222 average = 74.000000 Grade = B

Page 36: C,C++ LABMANUAL 08

- 36 -

3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS

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

H. PROGRAM TO PRINT THE MONTH NAME IF MONTH NUMBER WAS GIVEN WITH IF ELSE LADDER

Page 37: C,C++ LABMANUAL 08

- 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

Page 38: C,C++ LABMANUAL 08

- 38 -

3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS

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;

I. PROGRAM TO PRINT THE MONTH NAME IF MONTH NUMBER WAS GIVEN WITH SWITCH CASE

Page 39: C,C++ LABMANUAL 08

- 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

Page 40: C,C++ LABMANUAL 08

- 40 -3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS

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);elseprintf("the no %d is greater\n",c);}elseif(b>c)printf("the no %d is greater\n",b);getch();}

INPUT:

enter 3 nos342

OUTPUT:

the no 4 is greater

J. PROGRAM TO FIND GREATEST OF 3 NOS

Page 41: C,C++ LABMANUAL 08

- 41 - 3.PROGRAMS USING IF-ELSE,SWITCH STATEMENTS

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 income54321

OUTPUT:

tax is 10864.200195

K. PROGRAM TO CALCULATE TAX

Page 42: C,C++ LABMANUAL 08

- 42 -

Page 43: C,C++ LABMANUAL 08

- 43 -4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS

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 n150

OUTPUT:

sum is 11325

A. PROGRAM TO FIND SUM OF FIRST N NATURAL NUMBERS WITH WHILE LOOP

Page 44: C,C++ LABMANUAL 08

- 44 -4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS

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

B.PROGRAM TO CALCULATE THE FACTORIAL OF THE GIVEN NUMBER

Page 45: C,C++ LABMANUAL 08

- 45 -4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS

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);elseprintf("the no %d is not armstrong\n",temp);getch();}

INPUT:

enter a no153

OUTPUT:

the no 153 is Armstrong

C. PROGRAM TO FIND WHETHER GIVEN NO IS ARMSTRONG OR NOT

Page 46: C,C++ LABMANUAL 08

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

INPUT:

enter the n value8

OUTPUT:

0 1 1 2 3 5 8

Page 47: C,C++ LABMANUAL 08

- 47 -

Page 48: C,C++ LABMANUAL 08

- 48 -4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS

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

INPUT:

enter n4

OUTPUT:

4 * 1 = 44 * 2 = 84 * 3 = 124 * 4 = 164 * 5 = 204 * 6 = 244 * 7 = 284 * 8 = 324 * 9 = 364 * 10 = 40

E. PROGRAM TO PRINT MATHEMATICAL TABLE

Page 49: C,C++ LABMANUAL 08

- 49 -4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS

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 terms3

OUTPUT:

enter the angle in degrees x30sin valve of given angle is 0.499770

F. PROGRAM TO FIND THE VALUE OF SINX

Page 50: C,C++ LABMANUAL 08

- 50 -4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS

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 terms3

OUTPUT:

enter the angle in degrees x30cos valve of given angle is 0.866158

G. PROGRAM TO FIND THE VALUE OF COSX

Page 51: C,C++ LABMANUAL 08

- 51 -4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS

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 no234

OUTPUT:

the sum of digits of 234 is 9

H. PROGRAM TO FIND SUM OF DIGITS OF A NUMBER

Page 52: C,C++ LABMANUAL 08

- 52 -4. PROGRAMS USING WHILE, DO-WHILE, FOR LOOPS

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);elseprintf("the no %d is not a palindrome\n",temp);getch();}

INPUT:

enter a no121

OUTPUT:

the no 121 is a palindrome

I. PROGRAM TO WHETHER GIVEN NO IS PALINDROME OR NOT

Page 53: C,C++ LABMANUAL 08

- 53 -

Page 54: C,C++ LABMANUAL 08

- 54 -5. PROGRAMS USING ARRAYS

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 size6

OUTPUT:

Enter any 6 elements98 23 45 67 12 90The sum of all elements is 335

A. PROGRAM TO FIND SUM OF ALL ELEMENTS IN THE ARRAY

Page 55: C,C++ LABMANUAL 08

- 55 -5. PROGRAMS USING ARRAYS

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 size6

OUTPUT:

Enter any 6 elements78 34 54 12 30 125Minimum is 12Maximum is 125

B. PROGRAM TO FIND MAXIMUM AND MINIMUM NUMBER IN AN ARRAY

Page 56: C,C++ LABMANUAL 08

- 56 -

5. PROGRAMS USING ARRAYS

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 size5

OUTPUT:

Enter any 5 elements1 2 3 4 5The array elements after reversing are: 5 4 3 2 1

C. PROGRAM TO REVERSE THE ARRAY ELEMENTS IN PLACE

Page 57: C,C++ LABMANUAL 08

- 57 -5. PROGRAMS USING ARRAYS

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

D. PROGRAM TO PERFORM LINEAR SEARCH

Page 58: C,C++ LABMANUAL 08

- 58 -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

Page 59: C,C++ LABMANUAL 08

- 59 -

Enter array size5Enter any 5 elements1 2 34 5 6Enter the element you want to search for8989 is not found in the array

Page 60: C,C++ LABMANUAL 08

- 60 -5. PROGRAMS USING ARRAYS

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

elseprintf("%d is found at location %d\n",x,xloc);

getch();return 0;}

E. PROGRAM TO PERFORM BINARY SEARCH

Page 61: C,C++ LABMANUAL 08

- 61 -INPUT:

Enter array size6

OUTPUT:

Enter any 6 elements78 45 67 23 14 49Enter the element you want to search for2323 is found at location 3

Enter array size5Enter any 5 elements1 2 34 5 6Enter the element you want to search for8989 is not found in the array

Page 62: C,C++ LABMANUAL 08

- 62 -

5. PROGRAMS USING ARRAYS

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 size6

OUTPUT:

Enter any 6 elements

F. PROGRAM TO PERFORM BUBBLE SORT

Page 63: C,C++ LABMANUAL 08

- 63 -78 95 62 30 12 39After sorting, the element are 12 30 39 62 78 95

5. PROGRAMS USING ARRAYS

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 size6

G. PROGRAM TO PERFORM SELECTION SORT

Page 64: C,C++ LABMANUAL 08

- 64 -

OUTPUT:Enter any 6 elements78 95 62 30 12 39After sorting, the element are 12 30 39 62 78 95

5. PROGRAMS USING ARRAYS

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

H. PROGRAM TO READ AND PRINT A MATRIX

Page 65: C,C++ LABMANUAL 08

- 65 -

INPUT:

Enter matrix size23

Enter matrix elements123456OUTPUT:

The Matrix is 1 2 3 4 5 6

Page 66: C,C++ LABMANUAL 08

- 66 -

5. PROGRAMS USING ARRAYS

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

I. PROGRAM TO TRANSPOSE A MATRIX

Page 67: C,C++ LABMANUAL 08

- 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 size23Enter matrix elements123456

OUTPUT:

The Matrix is 1 2 3 4 5 6

The Matrix after transpose,is 1 4 2 5 3 6

Page 68: C,C++ LABMANUAL 08

- 68 -

5. PROGRAMS USING ARRAYS

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

J. PROGRAM TO ADD TWO MATRICES

Page 69: C,C++ LABMANUAL 08

- 69 -getch();return 0;}

INPUT:

Enter first matrix size23Enter second matrix size23Enter first matrix elements123456

Enter second matrix elements654321

OUTPUT:

Addition of two Matrices is 7 7 7 7 7 7

Enter first matrix size23

Enter second matrix size43

Size mismatch, Addition is not possible

Page 70: C,C++ LABMANUAL 08

- 70 -

5. PROGRAMS USING ARRAYS

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

}

K. PROGRAM TO MULTIPLY TWO MATRICES

Page 71: C,C++ LABMANUAL 08

- 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 size23Enter second matrix size43OUTPUT:

Not compatible, Multiplication is not possibleEnter first matrix size23

Enter second matrix size31

Enter first matrix elements123456

Enter second matrix elements123

Product of two Matrices is

Page 72: C,C++ LABMANUAL 08

- 72 - 14 32

5. PROGRAMS USING ARRAYS

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

elsenc++;

L. PROGRAM TO COUNT FREQUENCY OF OCCURRENCES OF CHARACTERS AND SPECIAL CHARACTERS (\N,\T AND SPACES)

Page 73: C,C++ LABMANUAL 08

- 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 EOF43u04 jrir

59jgiogfg jhvcvsopf

wrkjrr23-054328324^Z

The entered string is 43u04 jrir

59jgiogfg jhvcvsopf

wrkjrr23-054328324

OUTPUT:

No. of characters = 28No. of digits = 17No. of newlines = 5No. of tabs = 7No. of spaces = 3

Page 74: C,C++ LABMANUAL 08

- 74 -6. PROGRAMS USING FUNCTIONS

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 integer10

OUTPUT:

Factorial of 10 is 3628800Enter an integer5Factorial of 5 is 120

A. FUNCTION TO CALCULATE FACTORIAL OF AN INTEGER

Page 75: C,C++ LABMANUAL 08

- 75 -6. PROGRAMS USING FUNCTIONS

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 values103

OUTPUT:

B. FUNCTION TO CALCULATE NCR AND NPR (USING FUNCTION)

Page 76: C,C++ LABMANUAL 08

- 76 -10C3 is 12010P3 is 720

6. PROGRAMS USING FUNCTIONS

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 integer10

OUTPUT:

Factorial of 10 is 3628800

Enter an integer

C. FUNCTION TO CALCULATE FACTORIAL OF AN INTEGER

Page 77: C,C++ LABMANUAL 08

- 77 -5Factorial of 5 is 120

Page 78: C,C++ LABMANUAL 08

- 78 -

6. PROGRAMS USING FUNCTIONS

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

D. PROGRAM TO PRINT PRIME NUMBERS IN THE GIVEN RANGE (USING FUNCTION)

Page 79: C,C++ LABMANUAL 08

- 79 -

INPUT:

Enter the range150

OUTPUT:

The prime numbers in the given range are23571113171923293137414347

Page 80: C,C++ LABMANUAL 08

- 80 -

6. PROGRAMS USING FUNCTIONS

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

E. PROGRAM TO PRINT ARMSTRONG NUMBERS IN THE GIVEN RANGE (USING FUNCTION)

Page 81: C,C++ LABMANUAL 08

- 81 -

INPUT: Enter the range11000

OUTPUT:

The armstrong numbers in the given range are1153370371407

Page 82: C,C++ LABMANUAL 08

- 82 -

6. PROGRAMS USING FUNCTIONS

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 integers2030

OUTPUT:

F. PROGRAM TO SWAP TWO INTEGER NUMBERS (USING FUNCTION)

Page 83: C,C++ LABMANUAL 08

- 83 -The integers before swapping: m= 20, n = 30The integers after swapping: m = 30,n = 20

6. PROGRAMS USING FUNCTIONS

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 number20

OUTPUT:

G. PROGRAM TO PRINT FIBONACCI SERIES UP TO A GIVEN NUMBER (USING FUNCTION)

Page 84: C,C++ LABMANUAL 08

- 84 -The fibonacci series upto 20011235813

6. PROGRAMS USING FUNCTIONS

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;

elsereturn fib(n-1)+fib(n-2);

}

INPUT:

Enter the number5

H. PROGRAM TO PRINT NTH FIBONACCI NUMBER (USING RECURSIVE FUNCTION)

Page 85: C,C++ LABMANUAL 08

- 85 -

OUTPUT:

The 5th fibonacci number = 3

Enter the number20The 20th fibonacci number = 4181

6. PROGRAMS USING FUNCTIONS

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;

elsereturn gcd(n,m%n);

}

I. PROGRAM TO FIND GCD OF TWO INTEGERS (USING RECURSIVE FUNCTION)

Page 86: C,C++ LABMANUAL 08

- 86 -INPUT:Enter any two integers12536

OUTPUT:

The GCD of 125 and 36 is = 1

Enter any two integers248The GCD of 24 and 8 is = 8

6. PROGRAMS USING FUNCTIONS

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;

J. PROGRAM TO FIND SUM OF ALL ELEMENTS IN AN ARRAY (USING FUNCTION)

Page 87: C,C++ LABMANUAL 08

- 87 -}

INPUT:

Enter array size6OUTPUT:

Enter any 6 elements982345671290The sum of all elements is 335

6. PROGRAMS USING FUNCTIONS

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)

K. PROGRAM TO PERFORM BUBBLE SORT (USING FUNCTION)

Page 88: C,C++ LABMANUAL 08

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

}

}}}

Page 89: C,C++ LABMANUAL 08

- 89 -

INPUT:

Enter array size6

OUTPUT:

Enter any 6 elements789562301239After sorting, the element are

12 30 39 62 78 95

Page 90: C,C++ LABMANUAL 08

- 90 -6. PROGRAMS USING FUNCTIONS

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

L. PROGRAM TO MULTIPLY TWO MATRICES (USING FUNCTION)

Page 91: C,C++ LABMANUAL 08

- 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 size23OUTPUT:

Enter second matrix size43

Not compatible, Multiplication is not possibleEnter first matrix size23Enter second matrix size31Enter first matrix elements123456Enter second matrix elements123

Product of two Matrices is 14 32

Page 92: C,C++ LABMANUAL 08

- 92 -

6. PROGRAMS USING FUNCTIONS

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:

M. PROGRAM TO REVERSE THE CONTENTS OF THE GIVEN STRING BY USING USER DEFINED FUNCTION

Page 93: C,C++ LABMANUAL 08

- 93 -enter any string vasavi

OUTPUT:

reverse string is ivasav6. PROGRAMS USING FUNCTIONS

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

N. PROGRAM TO FIND THE LENGTH OF THE GIVEN STRING BY USING USER DEFINED FUNCTION

Page 94: C,C++ LABMANUAL 08

- 94 -

OUTPUT:

length of bala krishna is 14

Page 95: C,C++ LABMANUAL 08

- 95 -

6. PROGRAMS USING FUNCTIONS

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

O. PROGRAM TO COPY THE CONTENTS OF THE GIVEN STRING TO ANOTHER BY USING USER DEFINED FUNCTION

Page 96: C,C++ LABMANUAL 08

- 96 -6. PROGRAMS USING FUNCTIONS

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

P. PROGRAM TO APPEND THE CONTENTS OF THE GIVEN STRING TO ANOTHER BY USING USER DEFINED FUNCTION

Page 97: C,C++ LABMANUAL 08

- 97 -6. PROGRAMS USING FUNCTIONS

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 ramaenter another string krishna

OUTPUT: the difference between rama and krishna is 7

Q. PROGRAM TO COMPARE THE CONTENTS OF GIVEN TWO STRINGS BY USING USER DEFINED FUNCTION

Page 98: C,C++ LABMANUAL 08

- 98 -7. PROGRAMS USING 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

A. PROGRAM TO REVERSE THE CONTENTS OF THE GIVEN STRING BY USING USER DEFINED FUNCTION WITH POINTERS

Page 99: C,C++ LABMANUAL 08

- 99 -7. PROGRAMS USING 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

OUTPUT:

length of bala krishna is 14

B. PROGRAM TO FIND THE LENGTH OF THE GIVEN STRING BY USING USER DEFINED FUNCTION WITH POINTERS

Page 100: C,C++ LABMANUAL 08

- 100 -

7. PROGRAMS USING 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

C. PROGRAM TO COPY THE CONTENTS OF THE GIVEN STRING TO ANOTHER BY USING USER DEFINED FUNCTION WITH POINTERS

Page 101: C,C++ LABMANUAL 08

- 101 -7. PROGRAMS USING POINTERS

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 abcdenter another stringpqrs

OUTPUT: destination string is pqrs abcd

D. PROGRAM TO APPEND THE CONTENTS OF THE GIVEN STRING TO ANOTHER BY USING USER DEFINED FUNCTION

Page 102: C,C++ LABMANUAL 08

- 102 -7. PROGRAMS USING POINTERS

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 stringramaenter another stringramana

OUTPUT: the difference between rama and ramana is –110

D. Program to compare the contents of given two strings by using user defined function

Page 103: C,C++ LABMANUAL 08

- 103 -

8.PROGRAMS USING STRINGS

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:

V C E

A. PROGRAM TO ABBREVIATE THE CONTENTS OF THE CONTENTS OF GIVEN STRING

Page 104: C,C++ LABMANUAL 08

- 104 -

8.PROGRAMS USING STRINGS

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

B. DEFINE A STRUCTURE TO REPRESENT RATIONAL NUMBER AND WRITE A PROGRAM TO PERFORM THE ARITHMETICAL OPERATIONS ON RATIONAL NUMBERS

Page 105: C,C++ LABMANUAL 08

- 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 number2 3

Enter NR and DR of second rational number1 4OUTPUT:

Sum is 11 / 12 Difference is 5 / 12 product is 2 / 12 division RESULT: is 8 / 3

Page 106: C,C++ LABMANUAL 08

- 106 -8.PROGRAMS USING STRINGS

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 number1 2

Enter RP and IP of second complex number3 4OUTPUT: Sum is ( 4.000000 , 6.000000 ) Sum is ( -2.000000 , -2.000000 )

C. DEFINE A STRUCTURE TO REPRESENT COMPLEX NUMBER AND WRITE A PROGRAM TO PERFORM THEARITHMETICAL OPERATIONS ON COMPLEX NUMBERS

Page 107: C,C++ LABMANUAL 08

- 107 -

8.PROGRAMS USING STRINGS

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;

D. DEFINE A STRUCTURE TO REPRESENT RATIONAL NUMBER AND WRITE A PROGRAM TO PERFORM THE ARITHMETICAL OPERATIONS ON RATIONAL NUMBERS BY USING FUNCTIONS

Page 108: C,C++ LABMANUAL 08

- 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 number1 2 Enter NR and DR of second rational number34OUTPUT: sum is 10 / 8 Difference is -2 / 8 product is 3 / 8 division RESULT: is 4 / 6

Page 109: C,C++ LABMANUAL 08

- 109 -

8.PROGRAMS USING STRINGS

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

E. DEFINE A STRUCTURE TO REPRESENT COMPLEX NUMBER AND WRITE A PROGRAM TO PERFORM THE ARITHMETICAL OPERATIONS ON COMPLEX NUMBERS BY USING FUNCTIONS

Page 110: C,C++ LABMANUAL 08

- 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 1 2

Enter RP and IP of second complex number 3 4

OUTPUT:

Sum is ( 4.000000 , 6.000000 ) Sum is ( -2.000000 , -2.000000 )

Page 111: C,C++ LABMANUAL 08

- 111 -9.PROGRAMS USING STRUCTURES & UNIONS

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;

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.

Page 112: C,C++ LABMANUAL 08

- 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 students3OUTPUT:

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

Page 113: C,C++ LABMANUAL 08

- 113 -

9.PROGRAMS USING STRUCTURES & UNIONS

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

B. WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF POINTERS TO STRUCTURES TO CALCULATE THE TOTAL AND AVERAGE OF THE GIVEN STUDENT

Page 114: C,C++ LABMANUAL 08

- 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 student1001rama90 89 87OUTPUT:

STUDENT REPORT IS

ROLL NO = 1001 NAME = rama TOTAL = 266 AVERAGE = 88.666664

Page 115: C,C++ LABMANUAL 08

- 115 -9.PROGRAMS USING STRUCTURES & UNIONS

AIM: Program toprint tomorrows date if today’s 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");

C. PROGRAM TO PRINT TOMORROWS DATE IF TO-DAYS DATE WAS GIVEN

Page 116: C,C++ LABMANUAL 08

- 116 - scanf("%d%d%d",&p->dd,&p->mm,&p->yy); tommorrow(p); getch(); }

INPUT:

Enter today date12 5 2007

Page 117: C,C++ LABMANUAL 08

- 117 -

10. Programs using Files

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-ZThis is my first program in Clanguage to create a file.Bye.To see the contents of this fileopen text.dat in anyeditor program.^ZFile Saved on disk

text.dat:This is my first program in Clanguage to create a file.Bye.To see the contents of this fileopen text.dat in anyeditor program.

A. C PROGRAM TO CREATE A FILE

Page 118: C,C++ LABMANUAL 08

- 118 -

10. PROGRAMS USING FILES

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.datFile contents are:This is my first programin C to create a file with some text.ByeTo see the contents of the file open text.datin any editor.

Given example.dat No such file

B. C PROGRAM TO PRINT THE CONTENTS OF THE FILE ON THE MONITOR

Page 119: C,C++ LABMANUAL 08

- 119 -

10. PROGRAMS USING FILES

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.datdestination file: example.datfile copied

C. C PROGRAM TO COPY ONE FILE INTO ANOTHER FILE

Page 120: C,C++ LABMANUAL 08

- 120 -

10. PROGRAMS USING FILES

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

D. C PROGRAM TO CREATE A FILE OF STUDENT RECORDS

Page 121: C,C++ LABMANUAL 08

- 121 -

INPUT:

How many records you want to store in the file2

Enter any 2 student recordsFor each record enter rollnumber,name and marks

OUTPUT:

enter record100rama rao600enter record200sita kumari500file created

Page 122: C,C++ LABMANUAL 08

- 122 -

10. PROGRAMS USING FILES

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

OUTPUT:

100 rama rao 600200 sita kumari 500

E. C PROGRAM TO DISPLAY STUDENT RECORDS BY READING FROM A FILE

Page 123: C,C++ LABMANUAL 08

- 123 -

11. Simple C++ Programs

AIM:

Program to print average of two numbers

#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

A. Average of two

Page 124: C,C++ LABMANUAL 08

- 124 -11. Simple C++ Programs

AIM: Program to demonstrate scope resolution operator

#include<iostream.h>int m=10;int main(){ int m=20; { int k=m; int m=30; cout<<"we are in inner block"; cout<<"k= "<<k<<"\n"; cout<<"m= "<<m<<"\n"; cout<<"::m= "<<::m<<"\n"; } cout<<"we are in outer block"; cout<<"m= "<<m<<"\n"; cout<<"::m= "<<::m<<"\n";

return 0;}

OUTPUT:

we are in inner block 20 30 10

we are in outer block 20 10

B. Demonstration of scope resolution operator

Page 125: C,C++ LABMANUAL 08

- 125 -11. Simple C++ Programs

AIM: Program to demonstrate multiple and division operation

using inline functions

#include<iostream.h> inline float mul(float x,float y) { return(x*y); } inline float div(float x,float y) { return(x/y); } int main() { float a=12.34; float b=9.82; cout<<mul(a,b)<<"\n"; cout<<div(a,b)<<"\n";

return 0; }

OUTPUT:

121.17,1.25

C. MUL and Div using inline functions

Page 126: C,C++ LABMANUAL 08

- 126 -

11. Simple C++ Programs

AIM:Program to calculate of volume of cube, cylinder,

rectangular box using Function overloading

// 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

D. Calculation of volume of cube,cylinder,rectangular box

Page 127: C,C++ LABMANUAL 08

- 127 -11. Simple C++ Programs

AIM: Program to find maximum number using function template

#include<iostream.h>#include<conio.h>template<class T>T maximum(T a,T b,T c){ T m=a; if(b>m) m=b; if(c>m) m=c; return m;}int main(){ int i1,i2,i3; clrscr(); cout<<"enter i1,i2,i3"; cin>>i1>>i2>>i3; cout<<maximum(i1,i2,i3)<<"\n"; float f1,f2,f3; cout<<"enter f1,f2,f3"; cin>>f1>>f2>>f3; cout<<maximum(f1,f2,f3)<<endl; char c1,c2,c3; cout<<"enter c1,c2,c3"; cin>>c1>>c2>>c3; cout<<maximum(c1,c2,c3)<<"\n"; return 0;}

OUTPUT:input:10 20 30output:30input:3.2 4.6 2.4output:4.6input:a w coutput:w

E. Program to find maximum number using function template

Page 128: C,C++ LABMANUAL 08

- 128 -12. Program using classes and objects

AIM: Program to demonstrate classes objects

#include<iostream.h>class item{ int number; float cost; public: void getdata(int a,float b); void putdata(void) { cout<<"number:"<<number<<"\n"; cout<<"cost:"<<cost<<endl; }};void item :: getdata(int a,float b){ number=a; cost=b;}int main(){ item x; cout<<"\n object x"<<"\n"; x.getdata(100,299.95); x.putdata(); item y; cout<<"\n object y"<<"\n"; y.getdata(200,175.50); y.putdata(); return 0;}

OUTPUT:

100 299.95200 175.50

A. Program to demonstrate class

Page 129: C,C++ LABMANUAL 08

- 129 -12. Program using classes and objects

AIM: Program to show the working of static data members

#include<iostream.h>class item{ static int count; int number; public: void getdata(int a) { number=a; count++; } void getcount(void) { cout<<"count: "; cout<<count<<"\n"; }};int item :: count;int main(){ item a,b,c; a.getcount(); b.getcount(); c.getcount(); a.getdata(100); b.getdata(200); c.getdata(300); cout<<" after reading data\n"; a.getcount(); b.getcount(); c.getcount(); return 0;}Results:

0,0,03,3,3

B. Program on working of static data member

Page 130: C,C++ LABMANUAL 08

- 130 -12. Program using classes and objects

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:

C. Program to demonstrate static member function

Page 131: C,C++ LABMANUAL 08

- 131 -1,2,3

Page 132: C,C++ LABMANUAL 08

- 132 -12. Program using classes and objects

AIM: Program to demonstrate working friend functions

#include<iostream.h>class ABC;class XYZ{ int x; public: void setvalue(int i){x=i;} friend void max(XYZ,ABC); };class ABC{ int a; public: void setvalue(int i){a=i;} friend void max(XYZ,ABC); }; void max(XYZ m,ABC n) { if(m.x>=n.a) cout<<m.x; else cout<<n.a; } int main() { ABC abc; abc.setvalue(10); XYZ xyz; xyz.setvalue(20); max(xyz,abc); return 0; }

OUTPUT:

20

D. Program on friend function

Page 133: C,C++ LABMANUAL 08

- 133 -12. Program using classes and objects

AIM: Program to calculate addition of two complex numbers

using friend function

#include<iostream.h>class complex{ float x,y; public: void input(float real,float imag) {

x=real;y=imag;

} friend complex sum(complex,complex); void show(complex); }; complex sum(complex c1,complex c2) { complex c3; c3.x=c1.x+c2.x; c3.y=c1.y+c2.y; return(c3); } void complex::show(complex c) { cout<<c.x<<"+i"<<c.y<<"\n"; } int main() { complex A,B,C; A.input(3.1,5.65); B.input(2.75,1.2); C=sum(A,B); cout<<"A=";A.show(A); cout<<"B = ";B.show(B); cout<<"c=";C.show(C); return 0; }OUTPUT:

5.85+i6.85

E. Program on complex numbers addition using friend

Page 134: C,C++ LABMANUAL 08

- 134 -

12. Program using classes and objects

AIM: Program to calculate addition of two complex numbers

using copy constructors and friend function

#include<iostream.h>class complex{ float x,y; public: complex(){} complex(float a){x=y=a;} complex(float real,float imag) {x=real;y=imag;} friend complex sum(complex,complex); friend void show(complex); }; complex sum(complex c1,complex c2) { complex c3; c3.x=c1.x+c2.x; c3.y=c1.y+c2.y; return(c3); } void show(complex c) { cout<<c.x<<"+i"<<c.y<<"\n"; } int main() { complex A(2.7,3.5); complex B(1.6); complex C; C=sum(A,B); cout<<"A=";show(A); cout<<"B=";show(B); cout<<"C=";show(C); complex p,q,r; p=complex(2.5,3.9); q=complex(1.6,2.5); r=sum(p,q);

F. Program to perform complex numbers sum using friend, constructors

Page 135: C,C++ LABMANUAL 08

- 135 - cout<<endl; cout<<"p=";show(p); cout<<"q=";show(q); cout<<"r=";show(r); return 0; } OUTPUT:

4.3+i5.1,4.1+i6.4

Page 136: C,C++ LABMANUAL 08

- 136 -

12. Program using classes and objects

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

G. Program to demonstrate constructors

Page 137: C,C++ LABMANUAL 08

- 137 -

12. Program using classes and objects

AIM: Program to demonstrate working of string operations

#include<iostream.h>#include<string.h>

class String{ char *name; int length; public: String() { length=0; name=new char[length+1]; } String(char *s) { length=strlen(s); name=new char[length+1]; strcpy(name,s); } void display(void) { cout<< name <<"\n";} void join(String &a,String &b); }; void String::join(String &a,String &b) { length=a.length+b.length; delete name; name=new char[length+1]; strcpy(name,a.name); strcat(name,b.name); }; int main() { char *first="Ravi"; String name1(first),name2("Prasad"),name3("rajesh"),s1,s2; s1.join(name1,name2); s2.join(s1,name3);

H. Program to perform string operations

Page 138: C,C++ LABMANUAL 08

- 138 - name1.display(); name2.display(); name3.display(); s1.display(); s2.display(); return 0; }

OUTPUT: raviprasad,raviprasadrajesh

Page 139: C,C++ LABMANUAL 08

- 139 -

12. Program using classes and objects

AIM: Program to read elements into a matrix and search for an

element in a matrix

#include<iostream.h>class matrix{ int **p; int d1,d2; public: matrix(int x,int y); void get_element(int i,int j,int value) { p[i][j]=value; } int & put_element(int i,int j) { return p[i][j]; }};matrix :: matrix(int x,int y) { d1=x; d2=y; p=new int *[d1]; for(int i=0;i<d1;i++) p[i]=new int[d2]; } int main() { int m,n; cout<< "enter size of matrix"; cin>>m>>n; matrix A(m,n); cout<<"enter matrix elements"; int i,j,value; for(i=0;i<m;i++) for(j=0;j<n;j++)

I. Program to perform matrix reading and search for element

Page 140: C,C++ LABMANUAL 08

- 140 - { cin>>value; A.get_element(i,j,value); } cout<<endl; cout<<A.put_element(1,2); return 0; }

OUTPUT:

input:3 3input:1 2 3 4 5 6 7 8 9input:1,2

5

Page 141: C,C++ LABMANUAL 08

- 141 -

12. Program using classes and objects

AIM: Program to demonstrate working of destructor in a class

#include<iostream.h>int count=0;class alpha{ public: alpha() { count++; cout<<"\n no of objects created"<<count; } ~alpha() { cout<<"\n no of objects destroyed"<<count; count--; }};int main(){ alpha A1,A2,A3,A4; { cout<<"enter block"; alpha A5; } { cout<<"enter block2"; alpha A6; } return 0;}

OUTPUT: object created:1,2,3,4,5destroyed:5created:5destroyed:5destroyed:4,3,2,1

J. Program to demonstrate destructor

Page 142: C,C++ LABMANUAL 08

- 142 -13. Program on operator overloading

AIM: Program t demonstrate working of unary minus operator

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

A. Program to perform overloading on unary minus

Page 143: C,C++ LABMANUAL 08

- 143 - cout<<"S:"; S.display(); return 0;}

INPUT:10 -20 30

-10 20 -30

Page 144: C,C++ LABMANUAL 08

- 144 -13. Program on operator overloading

AIM: Program to demonstrate overloading of + operator

#include<iostream.h>class complex{ float x; float y;public: complex(){ } complex(float real,float imag) { x=real; y=imag;} complex operator+(complex); void display(void);};

complex complex::operator+(complex c){ complex temp; temp.x=x+c.x; temp.y=y+c.y; return(temp);}void complex::display(void){ cout<<x<<"+j"<<y<<"\n";}int main(){ complex C1,C2,C3; C1=complex(2.5,3.5); C2=complex(1.6,2.7); C3=C1+C2;

cout<<"C1=";C1.display(); cout<<"C2=";C2.display(); cout<<"C3=";C3.display(); return 0;}OUTPUT:c1=2.5+ j 3.5c2=1.6+j2.7 c3=4.1+j6.2

B. Program to perform overloading + operator

Page 145: C,C++ LABMANUAL 08

- 145 -13. Program on operator overloading

AIM: Program to demonstrate overloading operators using

friend functions

#include<iostream.h>const size=3;class vector{ int v[size];public: vector(); vector(int *x); friend vector operator*(int a,vector b); friend vector operator*(vector b,int a); friend istream & operator>>(istream &,vector &); friend ostream & operator>>(ostream &,vector &);};vector::vector(){ for(int i=0;i<size;i++) v[i]=0;}vector::vector(int *x){ for(int i=0;i<size;i++) v[i]=x[i];}vector operator *(int a,vector b){ vector c; for(int i=0;i<size;i++) c.v[i]=a*b.v[i]; return c;} vector operator *(vector b,int a){ vector c; for(int i=0;i<size;i++) c.v[i]=b.v[i]*a; return c;}

C. Program to perform overloading operators using friend functions

Page 146: C,C++ LABMANUAL 08

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

Page 147: C,C++ LABMANUAL 08

- 147 -13. Program on operator overloading

AIM: Program to demonstrate overloading of operator on

strings using friend function

#include<string.h>#include<iostream.h>class string{ char *p; int len; public: string(){len=0;p=0;} string(const char *s) { len=strlen(s); p=new char[len+1]; strcpy(p,s); } string(const string & s) { len=s.len; p=new char[len+1]; strcpy(p,s.p); } ~string(){delete p;} friend string operator+(const string &s,const string &t) { string temp; temp.len=s.len+t.len; temp.p=new char[temp.len+1]; strcpy(temp.p,s.p); strcat(temp.p,t.p); return(temp); } friend int operator<=(const string &s,const string &t); friend void show(const string s); }; int operator<=(const string &s,const string &t) { int m=strlen(s.p);

D. Program to perform mathematical operations on strings

Page 148: C,C++ LABMANUAL 08

- 148 - 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:

raviprasadravirajeshravi smaller than ravirajesh

Page 149: C,C++ LABMANUAL 08

- 149 -

14. Program on inheritance

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

A. Program to demonstrate single inheritance: public

Page 150: C,C++ LABMANUAL 08

- 150 - 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=5a=5b=10c=50a=5b=20c=100

Page 151: C,C++ LABMANUAL 08

- 151 -14. Program on inheritance

AIM: Program to demonstrate single inheritance using private

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 : private B{ int c; public: void mul(void); void display(void);};void B :: get_ab(){ cout<<"enter values for a and b"; cin>>a>>b;}int B::get_a(){ return a;}void B:: show_a(){ cout<<"a="<<a<<"\n";}void D::mul(){ get_ab(); c=b*get_a();}void D::display(){

B. Program to demonstrate single inheritance: private

Page 152: C,C++ LABMANUAL 08

- 152 - 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

Page 153: C,C++ LABMANUAL 08

- 153 -14. Program on inheritance

AIM: Program to demonstrate multilevel inheritance

#include<iostream.h>class student{ protected: int roll_number; public: void get_number(int); void put_number(void);};void student::get_number(int a) { roll_number=a; }void student :: put_number() { cout<<"rollnumber:"<<roll_number<<"\n"; }class test : public student { protected: float sub1,sub2; public: void get_marks(float x,float y) {

sub1=x; sub2=y;

} void put_marks() {

cout<<"marks in sub1:"<<sub1<<"\n";cout<<"marks in sub2:"<<sub2<<"\n";

}}; class result :public test {

float total; public: void display() { total=sub1+sub2;

C. Program to demonstrate multilevel inheritance

Page 154: C,C++ LABMANUAL 08

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

}

RESULT:rollnumber:111 marks in sub1 75marks in sub2:59.5total:134.5

Page 155: C,C++ LABMANUAL 08

- 155 -14. Program on inheritance

AIM: Program to demonstrate multiple inheritances

#include<iostream.h>class M{ protected : int m; public : void get_m(int);};class N { protected: int n; public: void get_n(int); };class P:public M,public N{ public: void display(void) { cout<<"m="<<m<<"\n"; cout<<"n="<<n<<"\n"; cout<<"m*n"<<m*n<<"\n"; } }; void M :: get_m(int x) { m=x; } void N :: get_n(int y) { n=y; } int main() { P p; p.get_m(10); p.get_n(20); p.display();

D. Program to demonstrate multiple inheritances

Page 156: C,C++ LABMANUAL 08

- 156 - return 0; }

INPUT:

output:m=10n=20OUTPUT:

m*n=200

Page 157: C,C++ LABMANUAL 08

- 157 -15. Program on polymorphism

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

A. Program to demonstrate virtual base class

Page 158: C,C++ LABMANUAL 08

- 158 -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:678OUTPUT:

marks obtained:77.5 57.5sports wt:8total score:143

Page 159: C,C++ LABMANUAL 08

- 159 -

15. Program on polymorphism

AIM: Program to demonstrate working of constructors in

derived class

#include<iostream.h>class alpha{ int x; public: alpha(int i) { x=i; cout<<"alpha initialized\n"; } void show_x(void) { cout<<"x="<<x<<"\n"; } }; class beta { float y; public: beta(float j) { y=j; cout<<"beta initialized"; } void show_y(void) { cout<<"y="<<y<<"\n"; } }; class gamma : public alpha,public beta { int m,n; public: gamma(int a,float b,int c,int d): alpha(a),beta(b)

B. Program to demonstrate constructors in derived class

Page 160: C,C++ LABMANUAL 08

- 160 - {

m=c;n=d;

} void show_mn(void) {

cout<<"m="<<m<<"\n"<<"n="<<n<<"\n"; } }; int main() {

gamma g(5,10.27,20,30); cout<<"\n"; g.show_x(); g.show_y(); g.show_mn(); return 0; }

OUTPUT:

beta initialized alpha initialized gamma initialized x=5 y=10.27 m=20 n=30

Page 161: C,C++ LABMANUAL 08

- 161 -15. Program on polymorphism

AIM: Program to demonstrate working of pointers to objects

#include<iostream.h>class item{ int code; float price; public: void getdata(int a,float b) { code=a; price=b; } void show(void) { cout<<"code:"<<code<<"\n"; cout<<"price:"<<price<<"\n"; } }; const int size=2; int main() { item *p=new item[size]; item *d=p; int x,i; float y; for(i=0;i<size;i++) { cout<<"enter input"; cin>>x>>y; p->getdata(x,y); p++; } for(i=0;i<size;i++) { cout<<"item:"<<i+1<<"\n"; d->show(); d++; } return 0;

C. Program to demonstrate pointers to objects

Page 162: C,C++ LABMANUAL 08

- 162 - }

OUTPUT:

40 50050 60040 50050 600

Page 163: C,C++ LABMANUAL 08

- 163 -

15. Program on polymorphism

AIM: Program to demonstrate working of ‘this’ pointer

#include<iostream.h>#include<string.h>class person{ char name[20]; float age; public: person(char *s,float a) { strcpy(name,s); age=a; } person & person::greater(person & x) { if(x.age>=age)

return x; else

return *this; } void display(void) { cout<<"name:"<<name<<"\n"; cout<<"age:"<<age<<"\n"; } }; int main() { person p1("ravi",78.90); person p2("prasad",56.49); person p3("rajesh",34.67); person p=p1.greater(p3); cout<<"elder person is:\n"; p.display(); p=p1.greater(p2); cout<<"elder person is:\n"; p.display(); return 0; }

D. Program to demonstrate this pointer

Page 164: C,C++ LABMANUAL 08

- 164 -

OUTPUT: elder person is: ravi 78.9 elder person is: ravi 78.9

Page 165: C,C++ LABMANUAL 08

- 165 -15. Program on polymorphism

AIM: Program to demonstrate working of virtual functions

#include<iostream.h>class Base{ public: void display() {cout<<"\n Display base";} virtual void show() { cout<<"\n show base";}};class Derived:public Base{ public: void display() { cout<<"\n Display derived";} void show() { cout<<"\n show derived";}};int main(){ Base B; Derived D; Base *bptr; cout<<"\n bptr points to Base \n"; bptr=&B; bptr->display(); bptr->show(); cout<<"\n\n bptr points to Derived\n"; bptr=&D; bptr->display(); bptr->show(); return 0;}OUTPUT:

bptr points to base display base show base bptr points to derived display base show derived */

E. Program to demonstrate virtual functions

Page 166: C,C++ LABMANUAL 08

- 166 -

15. Program on polymorphism

AIM: Program to demonstrate working of runtime polymorphism

#include<iostream.h>#include<string.h>class media{ protected: char title[50]; float price; public: media(char *s,float a) { strcpy(title,s); price=a; } virtual void display(){ }};class book:public media{ int pages; public: book(char *s,float a,int p):media(s,a) { pages=p; } void display();};class tape:public media{ float time; public: tape(char *s,float a,float t):media(s,a) { time=t; } void display();};void book::display(){

F. Program to demonstrate runtime polymorphism

Page 167: C,C++ LABMANUAL 08

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

Page 168: C,C++ LABMANUAL 08

- 168 -

INPUT:ENTER BOOK DETAILSTile: AAAPrice :255Pages:100

ENTER TAPE DETIALSTile : BBBPrice : 75Play time (min): 60

MEDIA DETAILS

OUTPUT:

….. BOOK….

Tile: AAAPrice: 255Pages: 100

…..TAPE…..

Tile: BBBPrice: 75Play time (min): 60

Page 169: C,C++ LABMANUAL 08

- 169 -

16. Template programs

AIM: Program to demonstrate working of vector operations

using templates

#include<iostream.h>const size=3;template<class T>class vector{ T* v; public: vector() { v=new T[size]; for(int i=0;i<size;i++) v[i]=0; } vector(T* a) { for(int i=0;i<size;i++) v[i]=a[i]; } T operator*(vector &y) { T sum=0; for(int i=0;i<size;i++) sum+=this->v[i]*y.v[i]; return sum; }};int main(){ int x[3]={1,2,3}; int y[3]={4,5,6}; vector <int>v1; vector<int>v2; v1=x; v2=y; int R=v1*v2;

A. Program to perform vector operations using templates

Page 170: C,C++ LABMANUAL 08

- 170 - cout<<"R="<<R<<"\n"; return 0;}

OUTPUT:

r=32

Page 171: C,C++ LABMANUAL 08

- 171 -

16. Template programs

AIM: Program to demonstrate working of bubble sort using

template functions

#include<iostream.h>template<class T>void bubble(T a[],int n){ for(int i=0;i<n-1;i++) for(int j=n-1;i<j;j--) if(a[j]<a[j-1]) { swap(a[j],a[j-1]); }}template<class X>void swap(X &a,X &b){ X temp=a; a=b; b=temp;}int main(){ int x[5]={10,50,30,40,20}; float y[5]={1.1,5.5,3.3,4.4,2.2}; bubble(x,5); bubble(y,5); cout<<"Stored x-array:"; for(int i=0;i<5;i++) cout<<x[i]<<" "; cout<<endl;

cout<<"Stored y-array:"; for(int j=0;j<5;j++) cout<<y[j]<<" "; cout<<endl; return 0;}

B. Program to perform bubble sort using template functions

Page 172: C,C++ LABMANUAL 08

- 172 -

OUTPUT:

10 20 30 40 501.1 2.2 3.3 4.4 5.5

Page 173: C,C++ LABMANUAL 08

- 173 -

16. Template programs

AIM: Program to calculate roots of equations using templates

#include<iostream.h>#include<iomanip.h>#include<math.h>template<class T>void roots(T a,T b,T c){ T d=b*b-4*a*c; if(d==0) { cout<<"R1=R2="<<-b/(2*a)<<endl; } else if(d>0) { cout<<"Roots are real\n"; float R=sqrt(d); float R1=(-b+R)/(2*a); float R2=(-b-R)/(2*a); cout<<"R1="<<R1<<"and"; cout<<"R2="<<R2<<endl; } else { cout<<"Roots are complex\n"; float R1=-b/(2*a); float R2=sqrt(-d)/(2*a); cout<<"Real part="<<R1<<endl; cout<<"Imaginary part="<<R2; cout<<endl; }}int main(){ cout<<"Integer coefficients\n"; roots(1,-5,6); cout<<"\n Float coefficients\n"; roots(1.5,3.6,5.0); return 0;}

C. Program to calculate roots of equations using

Page 174: C,C++ LABMANUAL 08

- 174 -

OUTPUT:

Roots are realR1 = 2.5R2 = 2.5Roots are complexReal part = -1.2

Imaginary part = 1.375

Page 175: C,C++ LABMANUAL 08

- 175 -17. Exception handling

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

A. Program on exception handling