You are on page 1of 92

# ROLL.NO.

2242

PROGRAM (1)
Write a program to calculate f(x), where x is a real number, as f(x) = -1, if -5.0 > x 0, if -5.0 < x < 5.0 +1, if x > 5.0

C++ CODE
#include<iostream.h> #include<conio.h> #include<stdlib.h> void main() {clrscr(); cout<<"enter the value of 'x' \n"; float x; int ans; cin>>x; if(x<-5.0) ans=-1; else if(x>-5.0 && x<5.0) ans=0; else if(x>5.0) ans=1; else {cout<<"f(x) not defined at this value \n"; getch(); exit(0); } cout<<"the value of f(x) is: "<<ans; getch(); }

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

ROLL.NO. 2242

PROGRAM (2)
Write a program for an n-digit number, find a) the sum of digits (e.g. 12345, output = 15) b) the reverse of the number (e.g. 12345, output = 54321) c) whether the given number is a palindrome or not (e.g. 121, 23432, 5665 is a palindrome)

C++ CODE
#include<iostream.h> #include<conio.h> void main() {clrscr(); int sum(long int); long int reverse(long int); cout<<"enter your choice \n 1. sum of digits \n 2. reverse the number \n 3. check whether the number is palindrome or not \n"; int choice; cin>> choice; long int number; cout<<"enter the number \n"; cin>>number; switch(choice) { case 1: cout<<"the sum of digits is: "<<sum(number); break; case 2: cout<<" the reversed number is: "<<reverse(number); break; case 3: if(number== reverse(number)) {cout<<"number is palindrome \n"; } else cout<<"number is not palindrome \n"; break; default:cout<<"you entered a wrong choice \n"; break; } getch(); } long int reverse(long int num) {long int a, rev=0; while(num>0) {a=num%10; num=num/10; rev=(rev*10)+a; } return (rev);
3

ROLL.NO. 2242

} int sum(long int num) {long int a,sum=0; while(num>0) {a=num%10; num=num/10; sum=sum+a; } return (sum); }

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

ROLL.NO. 2242

PROGRAM (3)
Write a program to find the smallest and second smallest of given n numbers (without using arrays)

C++ CODE
#include<iostream.h> #include<conio.h> void main() { clrscr(); int no, min1=1000, min2=1001; cout<<"enter 5 numbers (limit 1000)\n"; for(int i=0; i<5; i++) {cin>>no; if(no<min1) {min2=min1; min1=no; } else if(no>min1 && no<min2) {min2=no; } } cout<<"the smallest number is "<<min1 <<"\n"; cout<<"second smallest number is "<<min2 <<"\n"; getch(); }

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

ROLL.NO. 2242

PROGRAM (4)
Write a program for a given number (n), find a) is it positive, or negative b) is it prime number c) binary representation d) is it even or odd e) factorial of the number f) the nth Fibonacci number

C++ CODE
#include<iostream.h> #include<conio.h> #include<stdlib.h> // for exit function void main() {clrscr(); void positive(int); void prime(int); void binary(int); void even(int); void factorial(int); int fib(int); int number; cout<<"enter your choice\n"; cout<<"a. check if the number is positive or not \n"; cout<<"b. check if number is prime or not\n"; cout<<"c. check if number is a binary representation \n"; cout<<"d. check if the number is even or odd \n"; cout<<"e. find out the factorial of number \n"; cout<<"f. find nth fibonacci number \n"; char choice; cin>> choice; cout<<"enter the number: \n"; cin>>number; switch(choice) {case 'a': positive(number); break; case 'b': prime(number); break; case 'c': binary(number); break; case 'd': even(number); break; case 'e': factorial(number); break; case 'f': cout<<"the fib. no at position "<<number<<"is "<<fib(number)<<"\n"; break; } getch(); } void positive(int no) {if (no>0) cout<<"the number in positive \n";
8

ROLL.NO. 2242

else if (no<0) cout<<"the number is negative \n"; else cout<<"zero is neither positive or negative \n"; } void prime(int no) {int flag=1; if(no==2) {cout<<"the number is prime \n"; getch(); exit(0); } else {for (int i=2; i<no; i++) { if(no%i==0) flag=0; } } if(flag==1) cout<<"number is prime \n"; else cout<<"the number is not prime \n"; } void binary(int no) {static int arr[20]; int i=0, length=0; while(no!=0) {if (no%2) { arr[i]=1; length++; } else { arr[i]=0; length++; } no=no/2; i++; } for(i=length-1; i>=0; i--) {cout<<arr[i]; } } void even(int num) {if (num%2) cout<<"the number you entered is odd \n"; else
9

ROLL.NO. 2242

cout<<"the number you entered is even \n"; } void factorial(int no) {int fact=1; for( int i=no; i>=1; i--) {fact=fact*i; } cout<<"factorial of the number "<<no<<" is "<<fact<<"\n"; } int fib(int place) {int val; if (place==1||place==2) val=1; else {val= fib(place-1) + fib (place-2) ; } return val; }

10

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

11

ROLL.NO. 2242

PROGRAM (5A)
Write a program to print the following pattern: * * * * * * * * * *

C++ CODE
#include<iostream.h> #include<conio.h> void main() { clrscr(); for(int i=0; i<4;i++) {for (int j=0; j<4; j++) {if (i>=j) cout<<'*'; } cout<<"\n"; } getch(); }

12

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

13

ROLL.NO. 2242

PROGRAM (5B)
Write a program to print the following pattern: * * * * * * * * * *

C++ CODE
#include<iostream.h> #include<conio.h> void main() { clrscr(); for(int i=0; i<4;i++) { for (int j=0; j<4; j++) { if (i+j>=3) cout<<'*'; else cout<<' '; } cout<<"\n"; } getch(); }

14

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

15

ROLL.NO. 2242

PROGRAM (5C)
Write a program to print the following pattern: 0 01 012 0123

C++ CODE
#include<iostream.h> #include<conio.h> void main() { clrscr(); for(int i=0; i<4;i++) { for (int j=0; j<4; j++) { if (i>=j) cout<<j; } cout<<"\n"; } getch(); }

16

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

17

ROLL.NO. 2242

PROGRAM (5D)
Write a program to print the following pattern: 0 11 222 3333

C++ CODE
#include<iostream.h> #include<conio.h> void main() { clrscr(); for(int i=0; i<4;i++) { for (int j=0; j<4; j++) { if (i>=j) cout<<i; } cout<<"\n"; } getch(); }

18

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

19

ROLL.NO. 2242

PROGRAM (5E)
Write a program to print the following pattern: 1 23 456 7 8 9 10

C++ CODE
#include<iostream.h> #include<conio.h> void main() { clrscr(); int output=1; for(int i=0; i<4;i++) { for (int j=0; j<4; j++) { if (i>=j) cout<<output++<<" "; } cout<<"\n"; } getch(); }

20

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

21

ROLL.NO. 2242

PROGRAM (5F)
Write a program to print the following pattern: 1 01 101 0101

C++ CODE
#include<iostream.h> #include<conio.h> void main() { clrscr(); for(int i=0; i<4;i++) { for (int j=0; j<4; j++) { if ((i+j)%2 && i>=j ) cout<<0; else if (!((i+j)%2) && i>=j) cout<<1; } cout<<"\n"; } getch(); }

22

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

23

ROLL.NO. 2242

PROGRAM (5G)
Write a program to print the following pattern: * * * * * * * * * * * * * * * *

C++ CODE
#include<iostream.h> #include<conio.h> void main() { clrscr(); int j; for(int i=0; i<4; i++) { for ( j=-3; j<-i; j++) { cout<<' '; } for( j=-i; j<=i; j++) { cout<<'*'; } cout<<"\n"; } getch(); }

24

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

25

ROLL.NO. 2242

PROGRAM (5H)
Write a program to print the following pattern: 1 232 34543 4567654

C++ CODE
#include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); int output=1; for( int i=0; i<=3; i++) { int j; for(j=-3; j<-i; j++) cout<<' '; for(j=-i; j<=i; j++) cout<<output+ abs( abs(i)- abs(j)); output++; cout<<"\n"; } getch(); }

26

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

27

ROLL.NO. 2242

PROGRAM (5I)
Write a program to print the following pattern: 1 101 10101 1010101

C++ CODE
#include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); for (int i=0; i<4; i++) { int j; for (j=-3; j<-i; j++) {cout<<' ';} for (j=-i; j<=i; j++) { if((i+abs(j))%2) cout<<0; else cout<<1; } cout<<"\n"; } getch(); }

28

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

29

ROLL.NO. 2242

PROGRAM (5J)
Write a program to print the following pattern: 1234321 12321 121 1

C++ CODE
#include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); int output=1; for(int i=3; i>=0; i--) { int j; for(j=-3; j<-i; j++) {cout<<' '; } for (j=-i; j<=i; j++) {cout<<output+abs(abs(i)-abs(j)); } cout<<"\n"; } getch(); }

30

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

31

ROLL.NO. 2242

PROGRAM (6)
Write a program to calculate grade of student according to marks obtained in 5 subjects a) % >= 80 Grade A+ b) % >= 60 and < 80 Grade A c) % >= 50 and <60 Grade B d) % >= 40 and <50 Grade C e) % < 40 Grade F (FAIL)

C++ CODE
#include<iostream.h> #include <conio.h> void main() { clrscr(); int marks[5]; float sum=0; cout<<"enter the marks in 5 subjects out of 100\n"; for(int i=0; i<5; i++) {cin>>marks[i]; sum=sum+ marks[i]; } int perc= (sum/500)*100; cout<< "percentage is"<< perc<<"\n"; if(perc>=80) cout<<"your grade is A+ \n"; else if(perc>=60 && perc<80) cout<<"your grade is A \n"; else if(perc>=50 && perc<60) cout<<"your grade is B\n"; else if(perc>=40 && perc<50) cout<<"your grade is C \n"; else if (perc<40) cout<<"your are FAIL\n"; getch(); }

32

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

33

ROLL.NO. 2242

PROGRAM (7)
For a given array of integers, a) Search a given number using linear search b) Sort the numbers c) Search a given number using binary search d) Find the maximum and minimum number in the array e) Remove the repeated elements

C++ CODE
#include<iostream.h> #include<conio.h> int a[5], choice; void main() { clrscr(); int i; void input(); void search(); void sort(); void binary(); void minmax(); void remove(); cout<<"enter your choice\n1. search a number\n2. sort the numbers\n"; cout<<"3. search a number \n"; cout<<"4. find minimum and maximum number\n5. remove repeated elements\n"; cin>>choice switch(choice) {case 1: search(); break; case 2: sort(); break; case 3: binary(); break; case 4: minmax(); break; case 5: remove(); break; default: cout<< "you entered wrong option"; } getch(); } void input() {cout<<"input the array: \n"; for(int i=0; i<5; i++) cin>>a[i]; } void search() {input(); int item, loc=-1; cout<<"enter the element to be searched \n"; cin>>item;
34

ROLL.NO. 2242

for(int i=0; i<5; i++) {if(a[i]==item) {loc=i+1; break; } } if(loc==-1) cout<<"\nelement not in the list"; else cout<<"\nlocation of element is"<<loc; } void sort() {input(); for(int pass=0;pass<4; pass++) {for (int i=0; i<4-pass; i++) {if(a[i]>a[i+1]) {int temp; temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; } } } cout<<"sorted array is: \n"; for(int i=0; i<5; i++) cout<<a[i]<<"\t"; } void binary() {int array[5], item,i; cout<<"enter the sorted array \n"; for (i=0; i<5; i++) {cin>>array[i];} cout<<"enter the element to be searched \n"; cin>>item; int start=0, end=4, mid; mid=(start+end)/2; while(start<=end && array[mid]!=item) {if(array[mid]>item) {end=mid-1; mid=(start+end)/2; } else if(array[mid]<item) {start=mid+1; mid=(start+end)/2; } } if(array[mid]==item)
35

ROLL.NO. 2242

cout<<"location is"<<mid+1; else cout<<"element not found"; } void minmax() {input(); int i, min, max; min=a[0]; max=a[0]; for(i=0; i<5; i++) {if(a[i]>max) max=a[i]; else if(a[i]<min) min=a[i]; } cout<<"the minimum is"<<min<<"\n the maximum is"<<max; } void remove() {input(); int i,j,n, length=5; int item; for(i=0; i<length; i++) {item=a[i]; for(j=i+1; j<length; j++) {if (a[j]==item) {for(n=j; n<length-1; n++) {a[n]=a[n+1];} length--; j--; } } } cout<<"the final array is:\n"; for(i=0; i<length; i++) { cout<<a[i]<<"\n"; } cout<<"length is:"<<length; }

36

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

37

ROLL.NO. 2242

PROGRAM (8)
For a two-dimensional array of integers, a) Find the maximum and minimum in each row, each column, and in the matrix b) Find the transpose

C++ CODE
#include<iostream.h> #include<conio.h> void main() { clrscr(); int array[3][3],i,j; cout<<"enter the array row wise (size 3 x 3)\n"; for(i=0; i<3; i++) {for (j=0; j<3; j++) cin>>array[i][j]; } int min=array[0][0], max=array[0][0]; for(i=0;i<3;i++) {int minr=array[i][0]; int maxr=0; for(j=0;j<3;j++) {if(array[i][j]>max) max=array[i][j]; if (array[i][j]<min) min=array[i][j]; if(array[i][j]>maxr) maxr=array[i][j]; if(array[i][j]<minr) minr=array[i][j]; } cout<<"min and max of row "<<i+1<<"\t"<< minr<<"\t"<<maxr<<"\n"; } for(j=0; j<3;j++) {int minc=array[0][j]; int maxc=0; for(i=0;i<3;i++) {if(array[i][j]>maxc) maxc=array[i][j]; if(array[i][j]<minc) minc=array[i][j]; } cout<<"min and max of column"<<j+1<<"\t"<<minc<<"\t"<<maxc<<"\n"; } cout<<"max and min in matrix is:"<<max<<"\t"<<min<<"\n\n";
38

ROLL.NO. 2242

cout<<"the transpose of matrix is: \n"; for(i=0; i<3; i++) {for(j=0; j<3; j++) cout<<array[j][i]<<"\t"; cout<<"\n"; } getch(); }

39

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

40

ROLL.NO. 2242

PROGRAM (9)
For given two matrices of integers, a) Find the sum of the matrices b) Find the difference of the matrices c) Find the product of the matrices

C++ CODE
#include<iostream.h> #include<conio.h> int a[3][3], b[3][3]; void main() { clrscr(); void add(); void subtract(); void multiply(); int i,j, choice; cout<<"enter the matrix 1 (row wise)\n"; for (i=0; i<3; i++) {for (j=0; j<3; j++) cin>>a[i][j]; } cout<<"enter the array 2 (row wise) \n"; for (i=0; i<3; i++) {for (j=0; j<3; j++) cin>>b[i][j]; } cout<<"enter your choice \n 1. add \n 2. subtract \n 3. multiply \n"; cin>> choice; switch (choice) {case 1: add(); break; case 2: subtract(); break; case 3: multiply(); break; default: cout<<" you entered a wrong choice \n"; } getch(); } void add() {int sum[3][3],m,n; cout<<"the addition is \n"; for (m=0; m<3; m++) {for(n=0; n<3; n++) { sum[m][n]=a[m][n]+ b[m][n];
41

ROLL.NO. 2242

cout<<sum[m][n]<<"\t"; } cout<<"\n"; } } void subtract() {int sub[3][3],m,n; cout<<"the subtraction is \n"; for( m=0;m<3;m++) {for(n=0;n<3;n++) {sub[m][n]=a[m][n]-b[m][n]; cout<<sub[m][n]<<"\t"; } cout<<"\n"; } } void multiply() {int mul[3][3],i,j,k; for( i=0; i<3; i++) {for(j=0; j<3;j++) {mul[i][j]=0; for(k=0; k<3; k++) {mul[i][j]=mul[i][j]+ a[i][k]*b[k][j]; } } } for(i=0; i<3; i++) {for(j=0; j<3; j++) {cout<<mul[i][j]<<"\t"; } cout<<"\n"; } }

42

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

43

ROLL.NO. 2242

PROGRAM (10)
For a given string S, a) Find the length b) Find the reverse of the string S c) Compare the string S with another string S1 d) Append another string S1 to the string S e) Find a given sub-string S1 in the string S

C++ CODE
#include<iostream.h> #include<conio.h> #include<string.h> #include<stdio.h> void main() { clrscr(); char str[20]; cout<<"input the string (max 20 characters) \n"; gets(str); cout<<"enter the funtion to be performed \n"; cout<<"1. find the length of string \n"; cout<<"2. compare the string with another string \n"; cout<<"3. append another string to the string \n"; cout<<"4. find the reverse of the string \n"; cout<<"5. find a substring in the string\n"; int choice; cin>>choice; switch (choice) { case 1: cout<<"the length of string is: "<<strlen(str); break; case 2: cout<<"enter the other string for comparison \n"; char str2[20]; gets(str2); cout<<"strcmp f(n) gives value:"<<strcmp(str,str2)<<"which means \n"; if(strcmp(str,str2)>0) cout<<"the string entered latter comes lexographicaly first \n"; else cout<<"the first string entered comes lexographicaly first \n"; break; case 3: cout<<"enter the string to be appended \n"; char str3[20]; gets(str3);
44

ROLL.NO. 2242

cout<<" new string is: \n "<<strcat(str, str3); break; case 4: cout<<"the reverse of the string is: \n"; for(int i=strlen(str); i>=0; i--) cout<<str[i]; break; case 5: cout<<"enter the string to be searched \n"; char s2[20]; gets(s2); // search int a=0, b=0; while(a<strlen(str)) {if(str[a]==s2[b]) {if(b==strlen(s2)-1) {cout<<"string found \n"; break; } else {a++; b++; } } else {if(a==strlen(str)-1) {cout<<"string not found \n"; break; } else a++; } } break; default: cout<<"you entered a wrong choice \n"; } getch(); }

45

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

46

ROLL.NO. 2242

PROGRAM (11)
Sort the given array of strings.

C++ CODE
#include<iostream.h> #include<conio.h> #include<string.h> #include<stdio.h> void main() { clrscr(); int i,j=0; char name[10][10]; cout<<"input 10 names to be sorted \n"; for(i=0; i<10; i++) {gets(name[i]); } char temp[10]; for(int pass=0; pass<9; pass++) {for( int i=0;i < 9-pass; i++) {if(strcmp(name[i], name[i+1])>0) {strcpy(temp,name[i]); strcpy(name[i],name[i+1]); strcpy(name[i+1],temp); } else if (strcmp(name[i], name[i+1])==0) {while(name[i][j]==name[i+1][j]) {j++;} if(name[i][j]>name[i+1][j]) {strcpy(temp, name[i]); strcpy(name[i], name[i+1]); strcpy(name[i+1],temp); } } } } //display the sorted list cout<<"\nthe sorted list of names is: \n \n"; for(i=0; i<10; i++) {puts(name[i]); } getch(); }

47

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

48

ROLL.NO. 2242

PROGRAM (12)
Write a program to implement Divide by zero exception.

C++ CODE
#include<iostream.h> #include<conio.h> class sample { public: int num1,num2; void divide(); }; void sample::divide() { try { cout<<"enter first number: "; cin>>num1; cout<<"\nenter second number: "; cin>>num2; if(num2!=0) cout<<"result: " <<num1/num2; else throw 2; } catch(int index) { cout<<"\ndivision by zero exception occured"; } } void main() { sample obj; obj.divide(); getch(); }
49

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

50

ROLL.NO. 2242

PROGRAM (13)
Write a program to find youngest and eldest person age using array.

C++ CODE
#include<iostream.h> #include<conio.h> void main() { int age[100],young,old,r,num; clrscr(); cout<<"number of person whose age are to be interted: "; cin>>num; cout<<"enter the ages: "; for(r=1;r<=num;r++) { cin>>age[r]; } old=age[1]; young=age[1]; for(r=1;r<=num;r++) { if(old<age[r]) { old=age[r]; } } for(r=1;r<=num;r++) { if(young>age[r]) { young=age[r]; } } cout<<"\n"<<"eldest person has age: "<<old; cout<<"\n"<<"youngest person has age: "<<young; getch(); }
51

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

52

ROLL.NO. 2242

PROGRAM (14)
Write a program to check array size (if reference is not valid, throw exception).

C++ CODE
#include<iostream.h> #include<conio.h> class sample { public: int num1,num[100],i; void divide(); }; void sample::divide() { try { cout<<"enter the size of array: "; cin>>num1; if(num1>100) { throw 1; } if(num1<100) { throw 'm'; } } catch(int index) { cout<<"\narray size limit is exceeded"; } catch(char m) { cout<<"\narray size is in limit"; }
53

ROLL.NO. 2242

54

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

55

ROLL.NO. 2242

PROGRAM (15)
Write a program to check input as positive or negative through exception handling and test it for if zero condition.

C++ CODE
#include<iostream.h> #include<conio.h> class sample { public: int num1; void divide(); }; void sample::divide() { try { cout<<"enter the number: "; cin>>num1; if(num1>0) throw 1; if(num1<0) throw 'm'; if(num1==0) cout<<"\nnumber is zero"; } catch(int index) { cout<<"\nnumber is positive"; } catch(char m) { cout<<"\nnumber is negative"; } } void main() {
56

ROLL.NO. 2242

57

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

58

ROLL.NO. 2242

PROGRAM (16)
Write a program to accept a string and to convert it into capital letters.

C++ CODE
#include<iostream.h> #include<conio.h> #include<stdio.h> #include<ctype.h> void main() { char name[50]; int i; clrscr(); cout<<"enter the string: "; gets(name); for(i=0;name[i]!='\0';i++) { name[i]=toupper(name[i]); } puts(name); getch(); }

59

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

60

ROLL.NO. 2242

PROGRAM (17)
Write a program using function overloading, overload in three ways so that it return square root for int, double and long int.

C++ CODE
#include<iostream.h> #include<conio.h> #include <math.h> int squareroot(int s) { return sqrt(s); } double squareroot(double s1) { return sqrt(s1); } long squareroot(long s2) { return sqrt(s2); } void main() { clrscr(); cout<<"\nThe square root of is: "<< squareroot(625); cout<<"\nThe square root of is: "<<squareroot(625.25); cout<<"\nThe square root of is: "<<squareroot(62500); getch(); }

61

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

62

ROLL.NO. 2242

PROGRAM (18)

C++ CODE
#include<iostream.h> #include<conio.h> class sample { int m,n; public: void getdata(int a, int b); void display(); void operator-(); }; void sample::getdata(int a, int b) { n=b; m=a; } void sample::display() { cout<<m<<"\t "; cout<<n<<"\t "; } void sample::operator-() { m=-m; n=-n; } void main() { clrscr(); sample ob; ob.getdata(40,-25); cout<<"\nvalues of data member before operator overloading: "; ob.display(); -ob; cout<<"\nvalues of data member after operator overloading: ";
63

ROLL.NO. 2242

ob.display(); getch(); }

64

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

65

ROLL.NO. 2242

PROGRAM (19)

C++ CODE
#include<iostream.h> #include<conio.h> class sample { float m,n; public: sample(){}; sample(float a, float b) { n=b; m=a; } void display() { cout<<"m = "<<m; cout<<"\nn = "<<n; } sample operator+(sample); }; sample sample::operator+(sample c) { sample temp; temp.m=m+c.m; temp.n=n+c.n; return(temp); } void main() { clrscr(); sample ob,ob1,ob2; ob=sample(1.5,7.25); ob1=sample(5.25,2.15); ob2=ob+ob1; cout<<"\nfirst object is:\n";
66

ROLL.NO. 2242

ob.display(); cout<<"\nsecond object is:\n"; ob1.display(); cout<<"\nsum of object is:\n"; ob2.display(); getch(); }

67

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

68

ROLL.NO. 2242

PROGRAM (20)
Write a program using friend function.

C++ CODE
#include<iostream.h> #include<conio.h> class sample { int a,b; public: void set() { a=25; b=40; } friend float mean(sample s); }; float mean(sample s) { return(s.a+s.b)/2.0; } void main() { clrscr(); sample ob; ob.set(); cout<<"mean value is "<<mean(ob); getch(); }

69

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

70

ROLL.NO. 2242

PROGRAM (21)
Write a program with generic function.

C++ CODE
#include<iostream.h> #include<conio.h> template<class sample> void swap(sample &num1, sample &num2) { sample temp; temp=num1; num1=num2; num2=temp; } void main() { int first=10, second=20; double n1=10.1, n2=20.6; char a='y', b='h'; clrscr(); cout<<"\noriginal first and second: "<<first<<"\t"<<second; cout<<"\noriginal n1 and n2: "<<n1<<"\t"<<n2; cout<<"\noriginal a and b: "<<a<<"\t"<<b; swap(first,second); swap(n1,n2); swap(a,b); cout<<"\nswapped first and second: "<<first<<"\t"<<second; cout<<"\nswapped n1 and n2: "<<n1<<"\t"<<n2; cout<<"\nswapped a and b: "<<a<<"\t"<<b; getch(); }

71

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

72

ROLL.NO. 2242

PROGRAM (22)
Write a program to overload + operator.

C++ CODE
#include<iostream.h> #include<conio.h> class sample { float a,b; public: sample(){}; sample(float x, float y) { a=x; b=y; } sample operator+(sample); void display() { cout<<"a = "<<a<<"\n"; cout<<"b = "<<b<<"\n"; } }; sample sample::operator+(sample c) { sample temp; temp.a=a+c.a; temp.b=b+c.b; return temp; } void main() { clrscr(); sample x,y,z; x=sample(1.5,7.25); y=sample(5.25,2.15); z=x+y; cout<<"\nfirst object is:\n"; x.display();
73

ROLL.NO. 2242

74

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

75

ROLL.NO. 2242

PROGRAM (23)
Write a program to overload ++ and -- operators.

C++ CODE
#include<iostream.h> #include<conio.h> class sample { int m,n; public: sample(){}; sample(int a, int b) { n=b; m=a; } void display() { cout<<m<<"- "<<n<<"i\n"; } void display1() { cout<<m<<"+ "<<n<<"i\n"; } sample operator--(); sample operator++(); }; sample sample::operator--() { sample temp; temp.m=--m; temp.n=--n; return(temp); } sample sample::operator++()
76

ROLL.NO. 2242

{ sample temp; temp.m=++m; temp.n=++n; return(temp); } void main() { clrscr(); sample ob(40,25); --ob; ob.display(); --ob; ob.display(); ++ob; ob.display1(); ++ob; ob.display1(); getch(); }

77

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

78

ROLL.NO. 2242

PROGRAM (24)
Write a program using virtual function, explain polymorphism.

C++ CODE
#include<iostream.h> #include<conio.h> class base { public: virtual void vfunc() { cout<<"\nthis is base class virtual function"; } }; class derived:public base { public: void vfunc() { cout<<"\nthis is derived class virtual function"; } };

void main() { clrscr(); base *p,b; derived d; p=&b; p->vfunc(); p=&d; p->vfunc(); getch(); }

79

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

80

ROLL.NO. 2242

PROGRAM (25)
Write a program to implement Bubble sort using generic function.

C++ CODE
#include<iostream.h> #include<conio.h> template<class X> void bsort(X *a,int count) { X i,j,temp; for(i=0;i<count;i++) { for(j=i+1;j<count;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } } void main() { int iarray[7] = {7, 5, 4, 3, 9, 8, 6}; int i; double darray[5] = {4.3, 2.5, -0.9, 10.2, 3.0}; clrscr(); cout << "Here is unsorted integer array: "; for( i=0; i<7; i++) cout << iarray[i] << ' '; cout << endl; bsort(iarray, 7); cout << "Here is sorted integer array: "; for( i=0; i<7; i++) cout << iarray[i] << ' ';
81

ROLL.NO. 2242

cout << endl; cout << "Here is unsorted double array: "; for( i=0; i<5; i++) cout << darray[i] << ' '; cout << endl; bsort(darray, 5); cout << "Here is sorted double array: "; for( i=0; i<5; i++) cout << darray[i] << ' '; cout << endl; getch(); }

82

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

83

ROLL.NO. 2242

PROGRAM (26)
Write a program to show public inheritance.

C++ CODE
#include<iostream.h> #include<conio.h> class base { int i,j; public: void set(int a, int b) { i=a; j=b; } void display() { cout<<i<<"\t"<<j; } }; class derived:public base { int k; public: void setk(int x) { k=x; } void show() { cout<<"\t"<<k; } }; void main() { clrscr(); derived ob;
84

ROLL.NO. 2242

85

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

86

ROLL.NO. 2242

PROGRAM (27)
Write a program to show private inheritance.

C++ CODE
#include<iostream.h> #include<conio.h> class base { protected: int i,j; public: void set(int a, int b) { i=a; j=b; } void display() { cout<<i<<"\t"<<j; } }; class derived:private base { int k; public: void show() { set(10,20); display(); k=i*j; cout<<"\t"<<k; } }; void main() { clrscr();
87

ROLL.NO. 2242

88

ROLL.NO. 2242

## OUTPUT OF THE PROGRAM

89

ROLL.NO. 2242

PROGRAM (28)
Write a program to show protected inheritance.

C++ CODE
#include<iostream.h> #include<conio.h> class base { protected: int i,j; public: void set(int a, int b) { i=a; j=b; } void display() { cout<<i<<"\t"<<j; } }; class derived:protected base { int k; public: void show() { set(10,20); display(); k=i*j; cout<<"\t"<<k; } }; void main() { clrscr();
90

ROLL.NO. 2242

91

ROLL.NO. 2242

92