c++ lab exercises

99
PROGRAM NO: 1 AIM: OOP Program to find the Area of a Rectangle : PROGRAM: #include <iostream.h> #include <conio.h> class Area { private: int a, b, A; public: void getdata() { cout << "Enter the length and breadth of the rectangle "; cin >> a >> b; } void calculate() { A = a * b; } void display() { cout << "Area of rectangle = " << A; } }; void main() { clrscr(); Area ar; ar.getdata(); ar.calculate(); ar.display(); getch(); } OUTPUT :

Upload: jaya-ganthan

Post on 21-Apr-2015

106 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: c++ Lab Exercises

PROGRAM NO: 1

AIM:OOP Program to find the Area of a Rectangle :

PROGRAM:

#include <iostream.h>#include <conio.h>class Area{private:

int a, b, A;public:void getdata(){cout << "Enter the length and breadth of the rectangle  ";cin >> a >> b;}void calculate(){

A = a * b;}void display(){

cout << "Area of rectangle =  " << A;}};void main(){clrscr();Area ar;        ar.getdata();ar.calculate();ar.display();getch();}

OUTPUT :

++++++++++++++++++++++++++++++++++++++++PROGRAM NO: 2

AIM:OOP Program to find the sum of digits of a given number :

Page 2: c++ Lab Exercises

PROGRAM:

#include <iostream.h>#include <conio.h>class sum{     private:          int n, num, s;     public:          void getdata();          void calculate();          void putdata();};void sum :: getdata(){       cout << "Enter number   ";       cin >> n;       num = n;}void sum :: calculate(){       s = 0;       int dt;       for ( ; n>0 ; n/=10)       {          dt = n % 10;          s = s + dt;       }}void sum :: putdata(){       cout << "\n Sum is " << s ;}

void main(){     clrscr();     sum s;     s.getdata();     s.calculate();     s.putdata();     getch();}

OUTPUT :

Page 3: c++ Lab Exercises

++++++++++++++++++++++++++++++++++++++++

PROGRAM NO: 3

AIM:OOP Program to reverse and print a given number :

PROGRAM:

/*Reverse a given number*/#include <iostream.h>#include <conio.h>class reverse{     private:          int n, num, rev;     public:          void getdata();          void calculate();          void putdata();};void reverse :: getdata(){       cout << "Enter number   ";       cin >> n;       num = n;

Page 4: c++ Lab Exercises

}void reverse :: calculate(){       rev = 0;       int dt;       for ( ; n>0 ; n/=10)       {          dt = n % 10;          rev = rev * 10 + dt;       }}void reverse :: putdata(){       cout << "\nReverse of  " << num << " is " << rev ;}

void main(){     clrscr();     reverse r;     r.getdata();     r.calculate();     r.putdata();     getch();}

OUTPUT :

++++++++++++++++++++++++++++++++++++++++

Page 5: c++ Lab Exercises

PROGRAM NO: 4

AIM:Program to Check if a given number is Amstrong or not:

PROGRAM:

#include <iostream.h>#include <conio.h>class Amstrong{     private:          int n, num, s;     public:          void getdata();          void calculate();          void putdata();};void Amstrong :: getdata(){       cout << "Enter number   ";       cin >> n;       num = n;}void Amstrong :: calculate(){       s = 0;       int dt;       for ( ; n>0 ; n/=10)       {           dt = n % 10;           s = s + dt * dt * dt;       }}void Amstrong :: putdata(){    if (s == num)       cout << num << " is an Amstrong number";   else       cout << num << " is not an Amstrong number";}

Page 6: c++ Lab Exercises

void main(){     clrscr();     Amstrong a;     a.getdata();     a.calculate();     a.putdata();     getch();}

OUTPUT :

++++++++++++++++++++++++++++++++++++++++

PROGRAM NO: 5

AIM:Program to Check if a given number is Perfect or not:

PROGRAM:

Page 7: c++ Lab Exercises

#include <iostream.h>#include <conio.h>class Perfect{     private:          int n, num, sum;     public:          void getdata();          void calculate();          void putdata();};void Perfect :: getdata(){       cout << "Enter number   ";       cin >> n;       num = n;}void Perfect :: calculate(){       sum = 0;       int i;       for ( i=1; i<=n/2 ; i++)       {         if (n % i == 0)              sum = sum + i;       }}void Perfect :: putdata(){    if (sum == num)       cout << num << " is a Perfect number";   else       cout << num << " is not Perfect number";}

void main(){     clrscr();     Perfect p;     p.getdata();     p.calculate();     p.putdata();     getch();}

Page 8: c++ Lab Exercises

OUTPUT :

++++++++++++++++++++++++++++++++++++++++

PROGRAM NO: 6

AIM:Program to Check if a given number is Prime or not:

PROGRAM:

#include <iostream.h>#include <conio.h>#include <stdlib.h>class Prime{     private:          int n ;     public:          void getdata();          void calculate();};void Prime :: getdata()

Page 9: c++ Lab Exercises

{       cout << "Enter number   ";       cin >> n;}void Prime :: calculate(){       int i;         for ( i=2; i<=n/2 ; i++)              if (n % i == 0)                {                            cout << "NOT PRIME" ;                         getch();                            exit(0);                }                cout << "PRIME" ;}

void main(){     clrscr();     Prime p;     p.getdata();     p.calculate();     getch();}

OUTPUT :++++++++++++++++++++++++++++++++++++++++

PROGRAM NO: 7

AIM:Program to Check if a given number is Palindrom or not:

PROGRAM:

#include <iostream.h>#include <conio.h>class Palindrom{     private:          int n, num, rev;     public:          void getdata();          void calculate();          void putdata();

Page 10: c++ Lab Exercises

};void Palindrom :: getdata(){       cout << "Enter number   ";       cin >> n;       num = n;}void Palindrom :: calculate(){       rev = 0;       int dt;       for ( ; n>0 ; )       {           dt = n % 10;           rev = rev * 10 + dt;           n/=10;       }}void Palindrom :: putdata(){    if (rev == num)       cout << num << " is an Palindrom number";   else       cout << num << " is not an Palindrom number";}

void main(){     clrscr();     Palindrom p;     p.getdata();     p.calculate();     p.putdata();     getch();}

OUTPUT :

++++++++++++++++++++++++++++++++++++++++

Page 11: c++ Lab Exercises

PROGRAM NO: 8

AIM:OOP Program to find the Factorial of a number :

PROGRAM:

#include <iostream.h>#include <conio.h>class Factorial{     private:          int n;          long fact;     public:          void getdata();          void calculate();          void putdata();};void Factorial :: getdata(){       cout << "Enter number   ";       cin >> n;}void Factorial :: calculate(){       fact = 1;       int i;       for (i=1 ; i<=n ; i++)            fact = fact * i;}void Factorial :: putdata(){

Page 12: c++ Lab Exercises

    cout << "\n Factorial of  " <<  n << " is  " << fact;}

void main(){     clrscr();     Factorial f;     f.getdata();     f.calculate();     f.putdata();     getch();}

OUTPUT :++++++++++++++++++++++++++++++++++++++++

PROGRAM NO: 9

AIM:Program to Check if a given String is Palindrom or not:

PROGRAM:

#include <iostream.h>#include <conio.h>#include <stdio.h>#include <stdlib.h>

class Palindrom{     private:          char a[20], len ;     public:          void getdata();           void findlength();          void putdata();};void Palindrom :: getdata(){           char ch;           int i;           cout << "Enter the string " ;           for(i=0 ; (ch=getchar())!='\n'; i++)                      a[i] = ch;           a[i] = '\0';}

Page 13: c++ Lab Exercises

void Palindrom :: findlength(){        int i;        for (i=0 ; a[i] != '\0' ; i++) ;        len = i ;}void Palindrom :: putdata(){         int i;        for(i=0 ; i<len/2; i++)              if (a[i] != a[len-i-1])              {                        cout << "NOT PALINDROM" ;                        getch();                        exit(0);              }        cout << "PALINDROM" ;}

void main(){     clrscr();     Palindrom p;     p.getdata();     p.findlength();     p.putdata();     getch();}

OUTPUT :

++++++++++++++++++++++++++++++++++++++++

PROGRAM NO: 10

Page 14: c++ Lab Exercises

AIM:Program to find the biggest element of an array  :

PROGRAM:

#include <iostream.h>#include <conio.h>#include <stdio.h>

class BIGGEST{

private:int  n, a[100],big;public:void getdata();void findbig();void display();

};

void BIGGEST :: getdata(){       puts("Enter the value of n  " );       cin >>  n ;       puts("\nEnter n elements  " );       for(int i=0;i<n;i++)              cin >>  a[i] ;}

void BIGGEST :: findbig(){       big = a[0];       for(int i=1;i<n;i++)                     if (big < a[i])                     big = a[i];

}

void BIGGEST :: display(){   puts("Contents of array is   ");   for(int i=0;i<n;i++)         cout <<  a[i] << "\t";    cout << "\nThe biggest element of the array is    " << big;}

Page 15: c++ Lab Exercises

void main(){   clrscr();   BIGGEST b;   b.getdata();   b.findbig();   b.display();   getch();}

OUTPUT :Enter the value of n5

Enter n elements

234426272892Contents of array is234     426     27      28      92The biggest element of the array is    426

++++++++++++++++++++++++++++++++++++++++

PROGRAM NO: 11

Page 16: c++ Lab Exercises

AIM:Program to reverse an array  :

PROGRAM:#include <iostream.h>#include <conio.h>class REVERSE{

private:              int a[20], n ;

public:void getdata();void reverse();void display();

};

void REVERSE :: getdata(){   cout << "Enter the value of n  " ;   cin >>  n ;   cout << "Enter the array elements  " ;   for(int i=0 ; i<n ; i++)        cin >>  a[i] ;}void REVERSE :: reverse(){   cout << "\n\nThe Initial Array is \n" ;   for(int i=0 ; i<n ; i++)         cout <<   a[i] << "\t";   int temp;   for(i=0 ; i<n/2 ; i++)   {       temp = a[i];       a[i] = a[n-i-1];       a[n-i-1] = temp;    }}void REVERSE :: display(){   cout << "\n\nThe Array in Reverse order is \n" ;   for(int i=0;i<n;i++)         cout <<   a[i] << "\t";}

void main()

Page 17: c++ Lab Exercises

{       clrscr();       REVERSE r;       r.getdata();       r.reverse();       r.display();       getch();}

OUTPUT :Enter the value of n  10Enter the array elements  2342652728439595637388658

The Initial Array is234     265     27      28      439     59      56      37      388     658

The Array in Reverse order is658     388     37      56      59      439     28      27      265     234

++++++++++++++++++++++++++++++++++++++++

PROGRAM NO: 12

AIM:

Page 18: c++ Lab Exercises

Program to combine 2 arrays  :

PROGRAM:#include <iostream.h>#include <conio.h>class COMBINE{       private:              int a[20], b[20], c[20], m, n, i, j;       public:              void getdata();              void comb();              void firstdisplay();              void seconddisplay();};

void COMBINE :: getdata(){   /*Read First array*/   cout << "Enter the no: of elements in First array  " ;   cin >>  m ;   cout << "Enter the array elements  " ;   for(i=0 ; i<m ; i++)        cin >>  a[i] ;   /*Read Second array*/   cout << "Enter the no: of elements in Second array  " ;   cin >>  n ;   cout << "Enter the array elements  " ;   for(i=0 ; i<n ; i++)        cin >>  b[i] ;}void COMBINE :: comb(){   for(i=0 ; i<m ; i++)      c[i] = a[i];   for(j=0 ; j<n ; j++)   {      c[i] = b[j];      i++;   }}void COMBINE :: firstdisplay(){   cout << "\n\nThe First Array is \n";   for(i=0 ; i<m ; i++)      cout <<   a[i] << "\t";

Page 19: c++ Lab Exercises

   cout << "\n\nThe Second Array is \n";   for(i=0 ; i<n ; i++)      cout <<   b[i] << "\t";}void COMBINE :: seconddisplay(){   cout << "\n\nThe Combined Array is \n" ;   for(i=0;i<m+n;i++)      cout <<   c[i] << "\t";}void main(){       clrscr();       COMBINE c;       c.getdata();       c.firstdisplay();       c.comb();       c.seconddisplay();       getch();}

OUTPUT :Enter the no: of elements in First array  5Enter the array elements  123151617358Enter the no: of elements in Second array  4Enter the array elements  111222333444

The First Array is123     15      16      17      358

The Second Array is111     222     333     444

The Combined Array is123     15      16      17      358     111     222     333     444

Page 20: c++ Lab Exercises

++++++++++++++++++++++++++++++++++++++++

Page 21: c++ Lab Exercises

PROGRAM NO: 13

AIM:Program to perform Linear search  :

PROGRAM:

#include <iostream.h>#include <conio.h>#include <stdlib.h>class LINEAR{       private:              int a[20], n, i, srch;

       public:              void getdata();              void search();              void display();};

void LINEAR :: getdata(){   cout << "Enter the no: of elements in array  " ;   cin >>  n ;   cout << "Enter the array elements  " ;   for(i=0 ; i<n ; i++)        cin >>  a[i] ;   cout << "Enter the search element  " ;   cin >>  srch ;}void LINEAR :: search(){   for(i=0 ; i<n ; i++)      if (a[i] == srch)      {           cout << "\n\nSearch element Found" ;           cout << "\nPosition is   " <<  i ;           getch();           exit(0);      }   cout << "\n\nSearch element not Found" ;}

Page 22: c++ Lab Exercises

void LINEAR :: display(){   cout << "\n\nThe Array is \n";   for(i=0 ; i<n ; i++)         cout <<   a[i] << "\t";}

void main(){       clrscr();       LINEAR ls;       ls.getdata();       ls.display();       ls.search();       getch();}

OUTPUT :Enter the no: of elements in array  5Enter the array elements  45345256735Enter the search element  7

The Array is45      345     256     7       35

Search element FoundPosition is   3------------------------------Enter the no: of elements in array  5Enter the array elements  1122334455Enter the search element  88

The Array is11      22      33      44      55

Search element not Found

Page 23: c++ Lab Exercises

++++++++++++++++++++++++++++++++++++++++

PROGRAM NO: 14

AIM:Program to perform Binary Search  :

PROGRAM:#include <iostream.h>#include <conio.h>#include <stdlib.h>class BINARY{       private:              int a[20], n, i, srch, low, mid, high;

       public:              void getdata();              void search();              void display();};

void BINARY :: getdata(){   cout << "Enter the no: of elements in array  " ;   cin >>  n ;   cout << "Enter the array elements in sorted order  " ;   for(i=0 ; i<n ; i++)        cin >>  a[i] ;   cout << "Enter the search element  " ;   cin >>  srch ;}void BINARY :: search(){   low = 0;   high = n-1;   while (low <= high)   {       mid = (low + high) / 2;       if (srch == a[mid])       {           cout << "\n\nSearch element Found" ;           cout << "\nPosition is   "<< mid;           getch();

Page 24: c++ Lab Exercises

           exit(0);       }       if (srch > a[mid])           low = mid + 1;       else           high = mid + 1;   }   cout << "\n\nSearch element not Found" ;}void BINARY :: display(){   cout << "\n\nThe Array is \n" ;   for(i=0 ; i<n ; i++)        cout <<   a[i] <<"\t";}

void main(){       clrscr();       BINARY bs;       bs.getdata();       bs.display();       bs.search();       getch();}

OUTPUT :Enter the no: of elements in array  5Enter the array elements in sorted order  12233445565656Enter the search element  4556

The Array is12      23      34      4556    5656

Search element FoundPosition is   3------------ ----------------------Enter the no: of elements in array  5Enter the array elements in sorted order  1122

Page 25: c++ Lab Exercises

334455Enter the search element  1111

The Array is11      22      33      44      55

Search element not Found

++++++++++++++++++++++++++++++++++++++++

Page 26: c++ Lab Exercises

PROGRAM NO: 15

AIM:Program to perform Sorting  :

PROGRAM:#include <iostream.h>#include <conio.h>class SORT{       private:              int a[20], n, i, j, temp;

       public:              void getdata();              void sort();              void firstdisplay();              void seconddisplay();};

void SORT :: getdata(){   cout << "Enter the no: of elements in array  " ;   cin >>  n ;   cout << "Enter the array elements  " ;   for(i=0 ; i<n ; i++)       cin >>  a[i] ;}void SORT :: sort(){   for(i=0 ; i<n-1 ; i++)       for(j=i+1 ; j<n ; j++)       if (a[i] > a[j])       {          temp = a[i];          a[i] = a[j];          a[j] = temp;       }}void SORT :: firstdisplay(){   cout << "\n\nThe Array is \n" ;

Page 27: c++ Lab Exercises

   for(i=0 ; i<n ; i++)        cout <<   a[i] <<"\t";}void SORT :: seconddisplay(){   cout << "\n\nThe Array in sorted order is \n" ;   for(i=0 ; i<n ; i++)        cout <<   a[i] <<"\t";}

void main(){       clrscr();       SORT s;       s.getdata();       s.firstdisplay();       s.sort();       s.seconddisplay();       getch();}

OUTPUT :Enter the no: of elements in array  5Enter the array elements  78563557833

The Array is78      56      355     783     3

The Array in sorted order is3       56      78      355     783

++++++++++++++++++++++++++++++++++++++++

Page 28: c++ Lab Exercises

PROGRAM NO: 16

AIM:Program to convert a Decimal number to Binary number  :

PROGRAM:#include <iostream.h>#include <conio.h>class BINARYTODECIMAL{       private:              int a[20], n, i, num;

       public:              void getdata();              void convert();              void display();};

void BINARYTODECIMAL :: getdata(){   cout << "Enter the no:  " ;   cin >>  n ;   num = n;}void BINARYTODECIMAL :: convert(){   for(i=0 ; n>0 ; i++)   {       a[i] = n % 2;       n = n / 2;   }}void BINARYTODECIMAL :: display(){   cout << "\n\nBinary equivalent of   " <<  num << " is       ";   for(i=i-1 ; i>=0 ; i--)        cout <<   a[i] <<"\t";}

void main(){

Page 29: c++ Lab Exercises

       clrscr();       BINARYTODECIMAL bd;       bd.getdata();       bd.convert();       bd.display();       getch();}

OUTPUT :Enter the no:  25

Binary equivalent of   25 is       1    1    0    0    1

++++++++++++++++++++++++++++++++++++++++

Page 30: c++ Lab Exercises

PROGRAM NO: 17

AIM:Program to read a string and print it  (use getchar(), putchar()  :

PROGRAM:#include <iostream.h>#include <conio.h>#include <stdio.h>class STRING{       private:              int i;

char str[20], ch;       public:              void getdata();              void display();};

void STRING :: getdata(){   cout << "Enter the string " ;   for(i=0 ; (ch=getchar())!='\n'; i++)        str[i] = ch;   str[i] = '\0';}void STRING :: display(){   cout << "\n\nThe String is \n" ;   for(i=0 ; str[i] != '\0'; i++)        putchar(str[i]);}

void main(){       clrscr();

Page 31: c++ Lab Exercises

       STRING r;       r.getdata();       r.display();       getch();}

OUTPUT :Enter the string India is my Country

The String isIndia is my Country

++++++++++++++++++++++++++++++++++++++++

Page 32: c++ Lab Exercises

PROGRAM NO: 18

AIM:Program to Reverse a string   :

PROGRAM:#include <iostream.h>#include <conio.h>#include <stdio.h>class REVERSE{       private:              int i, l;              char str[20], ch, tmp;       public:              void getdata();              void reverse();              void firstdisplay();              void seconddisplay();};

void REVERSE :: getdata(){   cout << "Enter the string " ;   for(i=0 ; (ch=getchar())!='\n' ; i++)        str[i] = ch;   str[i] = '\0';   l = i;}void REVERSE :: reverse(){   for (i=0 ; i < l/2; i++)

Page 33: c++ Lab Exercises

   {       tmp = str[i];       str[i] = str[l-i-1];       str[l-i-1] = tmp;   }}void REVERSE :: firstdisplay(){   cout << "\n\nThe Original string is \n" ;   for(i=0 ; str[i] != '\0'; i++)        putchar(str[i]);}void REVERSE :: seconddisplay(){   cout << "\n\nThe string in reverse order is \n" ;   for(i=0 ; str[i] != '\0'; i++)        putchar(str[i]);}

void main(){       clrscr();       REVERSE r;       r.getdata();       r.firstdisplay();       r.reverse();       r.seconddisplay();       getch();}

OUTPUT :Enter the string india is my country

The Original string isindia is my country

The string in reverse order isyrtnuoc ym si aidni

++++++++++++++++++++++++++++++++++++++++

Page 34: c++ Lab Exercises

PROGRAM NO: 19

AIM:Program to combine 2 strings  :

PROGRAM:#include <iostream.h>#include <conio.h>#include <stdio.h>class COMBINE{       private:              int i, l;              char a[20], b[20], c[40], ch;       public:              void getdata();              void comb();              void display();};

void COMBINE :: getdata(){   /*  Read first string  */   cout << "Enter the first string " ;   for(i=0 ; (ch=getchar())!='\n'; i++)         a[i] = ch;   a[i] = '\0';   l = i;   /*  Read second string  */   cout << "Enter the second string " ;   for(i=0 ; (ch=getchar())!='\n'; i++)

Page 35: c++ Lab Exercises

         b[i] = ch;   b[i] = '\0';}void COMBINE :: comb(){   /*  combine the 2 strings  */   for(i=0 ; a[i]!='\0'; i++)         c[i] = a[i];   for(i=0 ; b[i]!='\0'; i++)         c[l+i] = b[i];   c[l+i] = '\0';}void COMBINE :: display(){   cout << "\n\nThe First string is \n" ;   for(i=0 ; a[i] != '\0'; i++)         putchar(a[i]);   cout << "\n\nThe Second string is \n" ;   for(i=0 ; b[i] != '\0'; i++)         putchar(b[i]);   cout << "\n\nThe Combined String is \n" ;   for(i=0 ; c[i] != '\0'; i++)         putchar(c[i]);}

void main(){       clrscr();       COMBINE c;       c.getdata();       c.comb();       c.display();       getch();}

OUTPUT :Enter the first string kiranEnter the second string wills

The First string iskiran

The Second string iswills

Page 36: c++ Lab Exercises

The Combined String iskiranwills

++++++++++++++++++++++++++++++++++++++++

PROGRAM NO: 20

AIM:Program to add 2 matrices   :

PROGRAM:#include <iostream.h>#include <conio.h>class ADD{       private:              int a[20][20], b[20][20], s[20][20], i, j, m, n;

       public:              void getdata();              void findsum();              void display();};

void ADD :: getdata(){   cout << "Enter the order of the matrix  \n" ;  cin >>  m >> n ;   /*Read First matrix*/   cout << "Enter the matrix-1 elements  \n" ;   for(i=0 ; i<m; i++)       for(j=0 ; j<n; j++)       cin  >> a[i][j] ;   /*Read Second matrix*/   cout << "Enter the matrix-2 elements  \n" ;   for(i=0 ; i<m; i++)       for(j=0 ; j<n; j++)

Page 37: c++ Lab Exercises

       cin >>  b[i][j] ;}void ADD :: findsum(){   /*Sum of 2 matrices*/   for(i=0 ; i<m; i++)       for(j=0 ; j<n; j++)       s[i][j] = a[i][j] + b[i][j];}void ADD :: display(){   cout << "\nThe First matrix is \n\n" ;   for(i=0 ; i<m; i++)   {       for(j=0 ; j<n; j++)          cout <<  a[i][j] << "\t";       cout << "\n\n";   }   cout << "\nThe Second matrix is \n\n" ;   for(i=0 ; i<m; i++)   {       for(j=0 ; j<n; j++)          cout <<  b[i][j] << "\t";       cout << "\n\n";   }   cout << "\nThe Sum matrix is \n\n" ;   for(i=0 ; i<m; i++)   {       for(j=0 ; j<n; j++)          cout <<  s[i][j] << "\t";       cout << "\n\n";   }}

void main(){       clrscr();       ADD r;       r.getdata();       r.findsum();       r.display();       getch();}

OUTPUT :Enter the order of the matrix

Page 38: c++ Lab Exercises

22Enter the matrix-1 elements11223344Enter the matrix-2 elements55667788

The First matrix is

11      22

33      44

The Second matrix is

55      66

77      88

The Sum matrix is

66      88

110     132

++++++++++++++++++++++++++++++++++++++++

Page 39: c++ Lab Exercises

PROGRAM NO: 21

AIM:Program to find the transpose of a given matrix  :

PROGRAM:#include <iostream.h>#include <conio.h>class TRANSPOSE{       private:              int a[20][20], b[20][20], i, j, m, n;

       public:              void getdata();              void trans();              void display();};

void TRANSPOSE :: getdata(){   cout << "Enter the order of the matrix  \n" ;   cin >> m >> n;   /*Read matrix*/   cout << "Enter the matrix elements  \n" ;   for(i=0 ; i<m; i++)       for(j=0 ; j<n; j++)       cin >>  a[i][j] ;}void TRANSPOSE :: trans()

Page 40: c++ Lab Exercises

{   /*transpose matrix*/   for(i=0 ; i<m; i++)       for(j=0 ; j<n; j++)       b[j][i] = a[i][j] ;}void TRANSPOSE :: display(){   cout << "\nThe Original matrix is \n\n" ;   for(i=0 ; i<m; i++)   {       for(j=0 ; j<n; j++)          cout <<  a[i][j] << "\t";       cout << "\n\n";   }   cout << "\nThe Transpose matrix is \n\n"  ;   for(i=0 ; i<n; i++)   {       for(j=0 ; j<m; j++)          cout <<  b[i][j] << "\t";       cout << "\n\n";   }}

void main(){       clrscr();       TRANSPOSE t;       t.getdata();       t.trans();       t.display();       getch();}

OUTPUT :Enter the order of the matrix22Enter the matrix elements1234

The Original matrix is

Page 41: c++ Lab Exercises

1       2

3       4

The Transpose matrix is

1       3

2       4

++++++++++++++++++++++++++++++++++++++++

PROGRAM NO: 22

AIM:Program to find the product of 2 matrices  :

PROGRAM:#include <iostream.h>#include <conio.h>#include <stdlib.h>class PRODUCT{       private:              int a[20][20], b[20][20], p[20][20], i, j, k, m1, n1, m2, n2;

       public:              void getorder();              void confirmation();              void getdata();              void findproduct();              void display();};

void PRODUCT :: getorder(){   cout << "\nEnter the order of the matrix-1  \n" ;  cin >>  m1 >> n1 ;   cout << "\nEnter the order of the matrix-2  \n" ;  cin >>  m2 >> n2;

}void PRODUCT :: confirmation(){

Page 42: c++ Lab Exercises

   if (n1 != m2)   {       cout << "NOT CONFIRMABLE FOR MULTIPLICATION" ;       getch();       exit(0);   }}void PRODUCT :: getdata(){   /*Read First matrix*/   cout << "Enter the matrix-1 elements  \n" ;   for(i=0 ; i<m1; i++)       for(j=0 ; j<n1; j++)       cin >>  a[i][j] ;   /*Read Second matrix*/   cout << "Enter the matrix-2 elements  \n" ;   for(i=0 ; i<m2; i++)       for(j=0 ; j<n2; j++)       cin >>  b[i][j] ;}void PRODUCT :: findproduct(){   /*Product of 2 matrices*/   for(i=0 ; i<m1; i++)       for(j=0 ; j<n2; j++)           for(k=0, p[i][j]=0 ; k<n1; k++)       p[i][j] = p[i][j] + a[i][k] * b[k][j];}void PRODUCT :: display(){   cout << "\nThe First matrix is \n\n" ;   for(i=0 ; i<m1; i++)   {       for(j=0 ; j<n1; j++)          cout <<  a[i][j] << "\t";       cout << "\n\n";   }   cout << "\nThe Second matrix is \n\n" ;   for(i=0 ; i<m2; i++)   {       for(j=0 ; j<n2; j++)          cout <<  b[i][j] << "\t";       cout << "\n\n";   }   cout << "\nThe Product matrix is \n\n" ;   for(i=0 ; i<m1; i++)

Page 43: c++ Lab Exercises

   {       for(j=0 ; j<n2; j++)          cout <<  p[i][j] << "\t";       cout << "\n\n";   }}

void main(){       clrscr();       PRODUCT p;       p.getorder();       p.confirmation();       p.getdata();       p.findproduct();       p.display();       getch();}

OUTPUT :

Enter the order of the matrix-122

Enter the order of the matrix-242NOT CONFIRMABLE FOR MULTIPLICATION-----------------------------------------------------------------------

Enter the order of the matrix-123

Enter the order of the matrix-232Enter the matrix-1 elements111111

Page 44: c++ Lab Exercises

Enter the matrix-2 elements333333

The First matrix is

1       1       1

1       1       1

The Second matrix is

3       3

3       3

3       3

The Product matrix is

9       9

9       9

++++++++++++++++++++++++++++++++++++++++

Page 45: c++ Lab Exercises

PROGRAM NO: 23

AIM:Program to find the Sum of Diagonal elements of a square matrix  :

PROGRAM:#include <iostream.h>#include <conio.h>#include <stdlib.h>class DIAGONALSUM{       private:              int a[20][20], i, j, m, n, sum;

       public:              void getorder();              void confirm();              void getdata();              void findsum();              void display();};

void DIAGONALSUM :: getorder(){   cout << "\nEnter the order of the matrix  \n" ;   cin >>  m >>n ;}void DIAGONALSUM :: confirm(){   if (m != n)

Page 46: c++ Lab Exercises

   {       cout << "NOT A SQUARE MATRIX" ;       getch();       exit(0);   }}void DIAGONALSUM :: getdata(){   /*Read matrix*/   cout << "Enter the matrix elements  \n" ;   for(i=0 ; i<m; i++)       for(j=0 ; j<n; j++)       cin >> a[i][j] ;}void DIAGONALSUM :: findsum(){   sum = 0;   for(i=0 ; i<m; i++)       for(j=0 ; j<n; j++)            if (i == j)         sum = sum + a[i][j];}void DIAGONALSUM :: display(){   cout << "\nThe Matrix is \n\n" ;   for(i=0 ; i<m; i++)   {       for(j=0 ; j<n; j++)          cout <<  a[i][j] << "\t";       cout << "\n\n";   }   cout << "\nThe sum is  " << sum;}

void main(){       clrscr();       DIAGONALSUM ds;       ds.getorder();       ds.confirm();       ds.getdata();       ds.findsum();       ds.display();       getch();}

Page 47: c++ Lab Exercises

OUTPUT :OUTPUT

Enter the order of the matrix22Enter the matrix elements1234

The Matrix is

1       2

3       4

The sum is  5

++++++++++++++++++++++++++++++++++++++++

Page 48: c++ Lab Exercises

PROGRAM NO: 24

AIM:Program to input the rollno, name, and 5 marks of ‘n’ students and then find the total and average mark and then display the details  :

PROGRAM:#include <iostream.h>#include <conio.h>

   class student   {       private:          int rollno;          char name[20];          int m[5];          int tot;          float avg;       public:          void getdata();          void total();          void average();          void display();   };   void student :: getdata()   {       cout << "Enter the roll number  ";

Page 49: c++ Lab Exercises

       cin >> rollno;       cout << "Enter the name  ";       cin >> name;       cout << "Enter 5 marks  ";       for (int i=0 ; i<5 ; i++)          cin >> m[i];   }

   void student :: total()   {       tot = 0;       for (int i=0 ; i<5 ; i++)          tot = tot + m[i];   }

   void student :: average()   {       avg = tot / 5;   }   void student :: display()   {       cout << rollno<< "  "<< name<< "  " << m[0]<< "  "<<m[1]<< "  "<<m[2]<< "  "<<m[3]<< "  "<<m[4]<< "  "  << tot <<"  "<<avg<<"  ";       cout <<endl;   }

   void main()   {      student s[10];      int n, i;      clrscr();      cout <<"Enter the no: of students   ";      cin >> n;      for (i=0;i<n;i++)         s[i].getdata();      for (i=0;i<n;i++)      {         s[i].total();         s[i].average();         s[i].display();      }      getch();   }

Page 50: c++ Lab Exercises

OUTPUT :Enter the no: of students   3Enter the roll number  1Enter the name  kiranEnter 5 marks  1223344556Enter the roll number  2Enter the name  muneerEnter 5 marks  3445563567Enter the roll number  3Enter the name  jacobEnter 5 marks  4556473423

1  kiran      12  23  34  45  56  170  342  muneer  34  45  56  35  67  237  473  jacob     45  56  47  34  23  205  41

++++++++++++++++++++++++++++++++++++++++

Page 51: c++ Lab Exercises

PROGRAM NO: 25

AIM:Program to process the employee details  :

PROGRAM:#include <iostream.h>#include <conio.h>#include <string.h>

class employee{   private:      int code[20];      char name[20][20];      char desig[20][20];      int exp[20];      int age[20];      int n;   public:      void get();      void list();      void search();      void edit();      void sort();};void employee :: get(){   clrscr();   cout <<"Enter the no: of employees   : ";   cin >> n;   for (int i=0 ; i<n ; i++)   {      cout << "Enter the details of employee  "<< i+1<< endl;      cout <<"Enter the employee code : ";      cin >> code[i];      cout <<"Enter the employee name : ";      cin >> name[i];

Page 52: c++ Lab Exercises

      cout <<"Enter the designation   : ";      cin >> desig[i];      cout <<"Enter the experience    : ";      cin >> exp[i];      cout <<"Enter the age           : ";      cin >> age[i];   }}void employee :: list(){   clrscr();   cout <<"\t\t EMPLOYEE DETAILS"<<endl;   cout <<"\t\t ----------------"<<endl;   for (int i=0 ; i<n ; i++)   {      cout <<"Employee Code         :  "<< code[i] << endl;      cout <<"Employee Name         :  "<< name[i] << endl;      cout <<"Employee Designation  :  "<< desig[i] << endl;      cout <<"Employee Experience   :  "<< exp[i] << endl;      cout <<"Employee Age          :  "<< age[i] << endl <<endl;   }   getch();}void employee :: search(){   int cod;   cout << "Enter the code no: to be searched  ";   cin >> cod;   for (int i=0 ; i<n ; i++)   {      if (code[i] == cod)      {       cout <<"Employee Code         :  "<< code[i] << endl;       cout <<"Employee Name         :  "<< name[i] << endl;       cout <<"Employee Designation  :  "<< desig[i] << endl;       cout <<"Employee Experience   :  "<< exp[i] << endl;       cout <<"Employee Age          :  "<< age[i] << endl;       getch();      }   }}void employee :: edit(){   int cod;   cout << "Enter the employee code no: to be edited  ";   cin >> cod;

Page 53: c++ Lab Exercises

   for (int i=0 ; i<n ; i++)   {      if (code[i] == cod)      {       cout <<"Employee Code         :  "<< code[i] << endl;       cout <<"Employee Name         :  "<< name[i] << endl;       cout <<"Employee Designation  :  "<< desig[i] << endl;       cout <<"Employee Experience   :  "<< exp[i] << endl;       cout <<"Employee Age          :  "<< age[i] << endl;       cout << "ENTER THE DETAILS TO MODIFY "<<endl;       cout <<"Enter the employee name : ";       cin >> name[i];       cout <<"Enter the designation   : ";       cin >> desig[i];       cout <<"Enter the experience    : ";       cin >> exp[i];       cout <<"Enter the age           : ";       cin >> age[i];      }   }}

void employee :: sort(){   char a[20], b[20];   int p, q, r;   for (int i=0 ; i<n ; i++)   {     for (int j=0 ; j<n ; j++)     {        if (code[i] < code[j])        {            p = code[i];            code[i] = code[j];            code[j] = p;

            strcpy(a, name[i]);            strcpy(name[i], name[j]);            strcpy(name[j], a);

            strcpy(b, desig[i]);            strcpy(desig[i], desig[j]);            strcpy(desig[j], b);

            q = exp[i];            exp[i] = exp[j];

Page 54: c++ Lab Exercises

            exp[j] = q;

            r = age[i];            age[i] = age[j];            age[j] = r;        }     }   }   cout <<"ALL RECORDS SORTED. PRESS 2";}void main(){    employee e;    int ch;    do    {        clrscr();        cout << "1.  CREATE "<<endl;        cout << "2.  LIST "<<endl;        cout << "3.  SEARCH "<<endl;        cout << "4.  EDIT "<<endl;        cout << "5.  SORT "<<endl;        cout << "6.  EXIT "<<endl;        cout << "Enter your choice   ";        cin >> ch;        switch(ch)        {            case 1:                e.get();                break;            case 2:                e.list();                break;            case 3:                e.search();                break;            case 4:                e.edit();                break;            case 5:                e.sort();                break;        }    }while(ch>0 && ch < 6);    getch();}

Page 55: c++ Lab Exercises

OUTPUT :1.  CREATE2.  LIST3.  SEARCH4.  EDIT5.  SORT6.  EXITEnter your choice

Enter the no: of employees   : 3

Enter the details of employee  1Enter the employee code : 34Enter the employee name : kiranEnter the designation   : managerEnter the experience    : 5Enter the age           : 28

Enter the details of employee  2Enter the employee code : 22Enter the employee name : devanandEnter the designation   : doctorEnter the experience    : 4Enter the age           : 30

Enter the details of employee  3Enter the employee code : 32Enter the employee name : jinshadEnter the designation   : programmerEnter the experience    : 5Enter the age           : 30

1.  CREATE2.  LIST3.  SEARCH4.  EDIT5.  SORT6.  EXITEnter your choice   2

Page 56: c++ Lab Exercises

                 EMPLOYEE DETAILS                 ----------------Employee Code         :  34Employee Name         :  kiranEmployee Designation  :  managerEmployee Experience   :  5Employee Age          :  28

Employee Code         :  22Employee Name         :  devanandEmployee Designation  :  doctorEmployee Experience   :  4Employee Age          :  30

Employee Code         :  32Employee Name         :  jinshadEmployee Designation  :  programmerEmployee Experience   :  5Employee Age          :  30

1.  CREATE2.  LIST3.  SEARCH4.  EDIT5.  SORT6.  EXITEnter your choice   3Enter the code no: to be searched  22Employee Code         :  22Employee Name         :  devanandEmployee Designation  :  doctorEmployee Experience   :  4Employee Age          :  30

1.  CREATE2.  LIST3.  SEARCH4.  EDIT5.  SORT6.  EXITEnter your choice   4Enter the employee code no: to be edited  22Employee Code         :  22Employee Name         :  devanand

Page 57: c++ Lab Exercises

Employee Designation  :  doctorEmployee Experience   :  4Employee Age          :  30ENTER THE DETAILS TO MODIFYEnter the employee name : DEVANANDEnter the designation   : PROFESSOREnter the experience    : 5Enter the age           : 32

1.  CREATE2.  LIST3.  SEARCH4.  EDIT5.  SORT6.  EXITEnter your choice   2

                 EMPLOYEE DETAILS                 ----------------Employee Code         :  34Employee Name         :  kiranEmployee Designation  :  managerEmployee Experience   :  5Employee Age          :  28

Employee Code         :  22Employee Name         :  DEVANANDEmployee Designation  :  PROFESSOREmployee Experience   :  5Employee Age          :  32

Employee Code         :  32Employee Name         :  jinshadEmployee Designation  :  programmerEmployee Experience   :  5Employee Age          :  30

1.  CREATE2.  LIST3.  SEARCH4.  EDIT5.  SORT

Page 58: c++ Lab Exercises

6.  EXITEnter your choice   5ALL RECORDS SORTED. PRESS 2

1.  CREATE2.  LIST3.  SEARCH4.  EDIT5.  SORT6.  EXITEnter your choice   2

                 EMPLOYEE DETAILS                 ----------------Employee Code         :  22Employee Name         :  DEVANANDEmployee Designation  :  PROFESSOREmployee Experience   :  5Employee Age          :  32

Employee Code         :  32Employee Name         :  jinshadEmployee Designation  :  programmerEmployee Experience   :  5Employee Age          :  30

Employee Code         :  34Employee Name         :  kiranEmployee Designation  :  managerEmployee Experience   :  5Employee Age          :  28

1.  CREATE2.  LIST3.  SEARCH4.  EDIT5.  SORT6.  EXITEnter your choice   6

++++++++++++++++++++++++++++++++++++++++

Page 59: c++ Lab Exercises

PROGRAM NO: 26

AIM:Program to perform string operations   :

PROGRAM:#include <iostream.h>#include <conio.h>class string{   private:      char str[20];   public:      void init();      void display();      void reverse();      void copy();

Page 60: c++ Lab Exercises

      void concat();};void string :: init(){   cout <<"Enter the string   ";   cin >> str;}

void string :: display(){   cout <<"The string is  "<< str;}

void string :: reverse(){   int i, j, len;   char rev[20];   for (len=0 ; str[len]!='\0' ; len++);   for (i=0, j=len-1 ; str[i]!='\0' ; i++, j--)      rev[i] = str[j];   cout <<"Reverse of  "<< str <<" is  "<<rev;}

void string :: copy(){   char newstr[20];   for (int i=0 ; str[i]!='\0' ; i++)       newstr[i] = str[i];   newstr[i] = '\0';   cout << "The variable str contains " <<str<<endl;   cout << "The variable newstr contains " <<newstr<<endl;}

void string :: concat(){   int i, j, len;   char str2[20];   display();   cout << "\nEnter the second string  ";   cin >> str2;   for (len=0 ; str[len]!='\0' ; len++);   for (i=len, j=0 ; str2[j]!='\0' ; i++, j++)   {        if (i > 19)        {           cout <<"Insufficient space for second string";

Page 61: c++ Lab Exercises

           getch();           break;        }        str[i] = str2[j];   }   str[i] = '\0';   cout <<"\nThe concatenated string is  "<<str;}void main(){   int n;   string s;   clrscr();   s.init();   do   {       clrscr();       cout <<"STRING OPERTAIONS "<<endl;       cout <<"1. Display "<<endl;       cout <<"2. Reverse "<<endl;       cout <<"3. Copy "<<endl;       cout <<"4. Concatenation "<<endl;       cout <<"5. Exit "<<endl;       cout <<"Enter your choice [1 - 5]  ";       cin >> n;       switch (n)       {          case 1: s.display(); break;          case 2: s.reverse(); break;          case 3: s.copy(); break;          case 4: s.concat(); break;          case 5: return;          default: cout <<"Incorrect Choice !!!";       }       getch();   }while(1);}

OUTPUT :Enter the string   vasantham

STRING OPERTAIONS1. Display2. Reverse

Page 62: c++ Lab Exercises

3. Copy4. Concatenation5. ExitEnter your choice [1 - 5]  1The string is  vasantham

STRING OPERTAIONS1. Display2. Reverse3. Copy4. Concatenation5. ExitEnter your choice [1 - 5]  2Reverse of  vasantham is  mahtnasav

STRING OPERTAIONS1. Display2. Reverse3. Copy4. Concatenation5. ExitEnter your choice [1 - 5]  3The variable str contains vasanthamThe variable newstr contains vasantham

STRING OPERTAIONS1. Display2. Reverse3. Copy4. Concatenation5. ExitEnter your choice [1 - 5]  4The string is  vasanthamEnter the second string  neyyattinkaraInsufficient space for second stringThe concatenated string is  vasanthamneyyattinka

STRING OPERTAIONS1. Display2. Reverse3. Copy4. Concatenation5. Exit

Page 63: c++ Lab Exercises

Enter your choice [1 - 5]  5

++++++++++++++++++++++++++++++++++++++++

PROGRAM NO: 27

AIM:Program to add 2 times  (call a function by sending object as function arguments ) :

PROGRAM:#include <iostream.h>#include <conio.h>class TIME{                    int hr, min;          public:                    void gettime();                    void put();                    void addtime( TIME t1, TIME t2);};

void TIME :: gettime(){         cout << "Enter the time in hrs and minutes   ";         cin >> hr >> min;}

void TIME :: put(){         cout << "\n" << hr << " HOURS" << min << "MINUTES" ;}

void TIME :: addtime(TIME t1, TIME t2){         min = t1.min + t2.min;         hr = min / 60;

Page 64: c++ Lab Exercises

         min = min % 60;         hr = hr + t1.hr + t2.hr;}

void main(){         TIME tm1, tm2, tm3;         tm1.gettime();         tm2.gettime();         tm3.addtime(tm1, tm2);         tm3.put();}

OUTPUT :Enter the time in hrs and minutes   1223Enter the time in hrs and minutes   645

19 HOURS  8 MINUTES

++++++++++++++++++++++++++++++++++++++++

Page 65: c++ Lab Exercises

PROGRAM NO: 28

AIM:Program to process employee details (using constructor and destructor)   :

PROGRAM:#include <iostream.h>#include <conio.h>#include <string.h>

class employee{   private:      int code[20];      char name[20][20];      char desig[20][20];      int exp[20];      int age[20];      int n;   public:      employee();      void list();      void search();      void edit();      void sort();      ~employee()      {          cout << "EXITING .......";          getch();      }};void employee :: employee(){   clrscr();   cout <<"Enter the no: of employees   : ";

Page 66: c++ Lab Exercises

   cin >> n;   for (int i=0 ; i<n ; i++)   {      cout << "Enter the details of employee  "<< i+1<< endl;      cout <<"Enter the employee code : ";      cin >> code[i];      cout <<"Enter the employee name : ";      cin >> name[i];      cout <<"Enter the designation   : ";      cin >> desig[i];      cout <<"Enter the experience    : ";      cin >> exp[i];      cout <<"Enter the age           : ";      cin >> age[i];   }}void employee :: list(){   clrscr();   cout <<"\t\t EMPLOYEE DETAILS"<<endl;   cout <<"\t\t ----------------"<<endl;   for (int i=0 ; i<n ; i++)   {      cout <<"Employee Code         :  "<< code[i] << endl;      cout <<"Employee Name         :  "<< name[i] << endl;      cout <<"Employee Designation  :  "<< desig[i] << endl;      cout <<"Employee Experience   :  "<< exp[i] << endl;      cout <<"Employee Age         :  "<< age[i] << endl <<endl;   }   getch();}void employee :: search(){   int cod;   cout << "Enter the code no: to be searched  ";   cin >> cod;   for (int i=0 ; i<n ; i++)   {      if (code[i] == cod)      {       cout <<"Employee Code         :  "<< code[i] << endl;       cout <<"Employee Name         :  "<< name[i] << endl;       cout <<"Employee Designation:  "<< desig[i] << endl;       cout <<"Employee Experience   :  "<< exp[i] << endl;       cout <<"Employee Age          :  "<< age[i] << endl;       getch();

Page 67: c++ Lab Exercises

      }   }}void employee :: edit(){   int cod;   cout << "Enter the employee code no: to be edited  ";   cin >> cod;   for (int i=0 ; i<n ; i++)   {      if (code[i] == cod)      {       cout <<"Employee Code         :  "<< code[i] <<endl;       cout <<"Employee Name         :  "<< name[i] <<endl;       cout <<"Employee Designation:  "<< desig[i] <<endl;       cout <<"Employee Experience   :  "<< exp[i] <<endl;       cout <<"Employee Age        :  "<< age[i] << endl;       cout << "ENTER THE DETAILS TO MODIFY "<<endl;       cout <<"Enter the employee name : ";       cin >> name[i];       cout <<"Enter the designation   : ";       cin >> desig[i];       cout <<"Enter the experience    : ";       cin >> exp[i];       cout <<"Enter the age           : ";       cin >> age[i];      }   }}void employee :: sort(){   char a[20], b[20];   int p, q, r;   for (int i=0 ; i<n ; i++)   {     for (int j=0 ; j<n ; j++)     {        if (code[i] < code[j])        {            p = code[i];            code[i] = code[j];            code[j] = p;

            strcpy(a, name[i]);            strcpy(name[i], name[j]);            strcpy(name[j], a);

Page 68: c++ Lab Exercises

            strcpy(b, desig[i]);            strcpy(desig[i], desig[j]);            strcpy(desig[j], a);

            q = exp[i];            exp[i] = exp[j];            exp[j] = q;

            r = age[i];            age[i] = age[j];            age[j] = r;        }     }   }   cout <<"ALL RECORDS SORTED. PRESS 2";}void main(){    employee e;    int ch;    do    {        clrscr();        cout << "1.  LIST "<<endl;        cout << "2.  SEARCH "<<endl;        cout << "3.  EDIT "<<endl;        cout << "4.  SORT "<<endl;        cout << "5.  EXIT "<<endl;        cout << "Enter your choice   ";        cin >> ch;        switch(ch)        {            case 1:                e.list();                break;            case 2:                e.search();                break;            case 3:                e.edit();                break;            case 4:                e.sort();                break;        }

Page 69: c++ Lab Exercises

    }while(ch>0 && ch < 5);    getch();}

OUTPUT :Enter the no: of employees   : 3

Enter the details of employee  1Enter the employee code : 12Enter the employee name : jacobEnter the designation   : programmerEnter the experience    : 4Enter the age           : 34

Enter the details of employee  2Enter the employee code : 11Enter the employee name : devanEnter the designation   : subinspectorEnter the experience    : 5Enter the age           : 35

Enter the details of employee  3Enter the employee code : 5Enter the employee name : yedukrishnanEnter the designation   : teacherEnter the experience    : 10Enter the age           : 35

1.  LIST2.  SEARCH3.  EDIT4.  SORT5.  EXITEnter your choice   1

                 EMPLOYEE DETAILS                 ----------------Employee Code         :  12Employee Name         :  jacobEmployee Designation  :  programmerEmployee Experience   :  4

Page 70: c++ Lab Exercises

Employee Age          :  34

Employee Code         :  11Employee Name         :  devanEmployee Designation  :  subinspectorEmployee Experience   :  5Employee Age          :  35

Employee Code         :  5Employee Name         :  yedukrishnanEmployee Designation  :  teacherEmployee Experience   :  10Employee Age          :  35

1.  LIST2.  SEARCH3.  EDIT4.  SORT5.  EXITEnter your choice   2Enter the code no: to be searched  11Employee Code         :  11Employee Name         :  devanEmployee Designation  :  subinspectorEmployee Experience   :  5Employee Age          :  35

1.  LIST2.  SEARCH3.  EDIT4.  SORT5.  EXITEnter your choice   3Enter the employee code no: to be edited  11Employee Code         :  11Employee Name         :  devanEmployee Designation  :  subinspectorEmployee Experience   :  5Employee Age          :  35ENTER THE DETAILS TO MODIFYEnter the employee name : DEVANANDEnter the designation   : ACCOUNTANTEnter the experience    : 5

Page 71: c++ Lab Exercises

Enter the age           : 40

1.  LIST2.  SEARCH3.  EDIT4.  SORT5.  EXITEnter your choice   1

                 EMPLOYEE DETAILS                 ----------------Employee Code         :  12Employee Name         :  jacobEmployee Designation  :  programmerEmployee Experience   :  4Employee Age          :  34

Employee Code         :  11Employee Name         :  DEVANANDEmployee Designation  :  ACCOUNTANTEmployee Experience   :  5Employee Age          :  40

Employee Code         :  5Employee Name         :  yedukrishnanEmployee Designation  :  teacherEmployee Experience   :  10Employee Age          :  35

1.  LIST2.  SEARCH3.  EDIT4.  SORT5.  EXITEnter your choice   4

1.  LIST2.  SEARCH3.  EDIT4.  SORT5.  EXIT

Page 72: c++ Lab Exercises

Enter your choice   1

                 EMPLOYEE DETAILS                 ----------------Employee Code         :  5Employee Name         :  yedukrishnanEmployee Designation  :  yedukrishnanEmployee Experience   :  10Employee Age          :  35

Employee Code         :  11Employee Name         :  DEVANANDEmployee Designation  :  DEVANANDEmployee Experience   :  5Employee Age          :  40

Employee Code         :  12Employee Name         :  jacobEmployee Designation  :  programmerEmployee Experience   :  4Employee Age          :  34

1.  LIST2.  SEARCH3.  EDIT4.  SORT5.  EXITEnter your choice   5EXITING .......

++++++++++++++++++++++++++++++++++++++++

Page 73: c++ Lab Exercises

PROGRAM NO: 29

AIM:Function Program to find the roots of a Quadratic equation:

PROGRAM:#include <iostream.h>#include <conio.h>#include <math.h>

void main(){       float a, b, c ;       clrscr();

Page 74: c++ Lab Exercises

       cout << "Enter the 3 co-efficients :" ;       cin >> a >> b >> c;       void QUADRATIC (float, float, float);       QUADRATIC (a, b, c);       getch();}

void QUADRATIC (float a, float b, float c){       float d;                    void distinct (float , float , float, float );       void equal (float , float , float );       void imaginary ( );       d = (b * b) - (4 * a * c);       if (d > 0)       {              distinct (a,b,c,d);       }       else if (d == 0)       {              equal (a,b,c);       }       else       {              imaginary ( );       }}void distinct (float a, float b, float c, float d){       float r1, r2;       r1 = (-b + sqrt(d)) / (2 * a);       r2 = (-b - sqrt(d)) / (2 * a);       cout << "\n The roots are Real and Distinct \n" ;       cout << "\n The roots are "<< r1 <<" and " << r2 ;}

void equal (float a, float b, float c){       float r ;       r = (-b) / (2 * a);       cout << "\n The roots are Real and Equal \n" ;       cout << "\n The roots are " << r ;}

void imaginary (  ){

Page 75: c++ Lab Exercises

       cout << "\n The roots are Imaginary \n" ;}

OUTPUT :Enter the 3 co-efficients :321

 The roots are Imaginary

=================================Enter the 3 co-efficients :253

 The roots are Real and Distinct

 The roots are -1 and -1.5

=================================Enter the 3 co-efficients :121

 The roots are Real and Equal

 The roots are -1

++++++++++++++++++++++++++++++++++++++++

Page 76: c++ Lab Exercises

PROGRAM NO: 30

AIM:Program to perform function overloading (find the area of circle, triangle, rectangle, square.)  :

PROGRAM:#include <iostream.h>#include <conio.h>#include <math.h>

float area(float);float area(float, float);float area(float, float, float);int area(int);

void main()

Page 77: c++ Lab Exercises

{    int choice;    do    { clrscr();       cout << "MAIN MENU \n";       cout << "1. Area of Circle \n";       cout << "2. Area of Triangle \n";       cout << "3. Area of Rectangle \n";       cout << "4. Area of Square \n";       cout << "5. Exit \n";       cout << "Enter your choice ";       cin >> choice;       switch(choice)       {    case 1:               float r;               cout << "Enter the radius  ";               cin >> r;               cout <<"Area = " << area(r);               break;           case 2:               float a, b, c;               cout << "Enter 3 sides  ";               cin >> a >> b >> c;               cout <<"Area = " << area(a, b, c);               break;

           case 3:               float len, bre;               cout << "Enter the length and breadth  ";               cin >> len >> bre;               cout <<"Area = " << area(len, bre);               break;

           case 4:               int aa;               cout << "Enter the length of a side   ";               cin >> aa;               cout <<"Area = " << area(aa);               break;

           case 5:               return;           default:               cout << "INVALID CHOICE !!! ";       }    getch();   }while (1);

Page 78: c++ Lab Exercises

}

float area (float r){     return (3.14 * r * r);}

float area (float l, float b){     return (l * b);}

float area (float a, float b, float c){     float s, x;     s = (a + b + c ) / 2;     x = sqrt(s * (s-a)* (s-b) * (s-c));     return (x);}

int area (int n){     return (n * n);}

OUTPUT :MAIN MENU1. Area of Circle2. Area of Triangle3. Area of Rectangle4. Area of Square5. ExitEnter your choice 1Enter the radius  5Area = 78.5

MAIN MENU1. Area of Circle2. Area of Triangle3. Area of Rectangle4. Area of Square5. ExitEnter your choice 2Enter 3 sides  234Area = 2.904737

Page 79: c++ Lab Exercises

MAIN MENU1. Area of Circle2. Area of Triangle3. Area of Rectangle4. Area of Square5. ExitEnter your choice 3Enter the length and breadth  1214Area = 168

MAIN MENU1. Area of Circle2. Area of Triangle3. Area of Rectangle4. Area of Square5. ExitEnter your choice 4Enter the length of a side   40Area = 1600

++++++++++++++++++++++++++++++++++++++++

Page 80: c++ Lab Exercises

PROGRAM NO: 31

AIM:Program to display the string “GV&HSS KULATHOOR” in triangular form   :

PROGRAM:#include <iostream.h>#include <stdio.h>#include <conio.h>void main(){    clrscr();   int i,j, len;   char str[] = "GV&HSSKULATHOOR";   for (i=0 ; str[i] !='\0' ; i++ )  ;   len = i;   for (i=0 ; i<= len ; i++)  {        for (j=0 ; j<=i ; j++ )              cout << str[i] << "   ";        cout << endl;

Page 81: c++ Lab Exercises

   }

    getch();}

OUTPUT :GV   V&   &   &H   H   H   HS   S   S   S   SS   S   S   S   S   SK   K   K   K   K   K   K   KU   U   U   U   U   U   U   U   UL   L   L   L   L   L   L   L   L   LA   A   A   A   A   A   A   A   A   A   AT   T   T   T   T   T   T   T   T   T   T   TH   H   H   H   H   H   H   H   H   H   H   H   HO   O   O   O   O   O   O   O   O   O   O   O   O   OO   O   O   O   O   O   O   O   O   O   O   O   O   O   OR   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R

++++++++++++++++++++++++++++++++++++++++

Page 82: c++ Lab Exercises

PROGRAM NO: 32

AIM:Program to check whether the elements in an integer array are Prime or not (function program) :

PROGRAM:

#include<iostream.h>#include<conio.h>

void main(){       clrscr();       int a[10],n, num;       int i;       int isprime(int);       cout<<"Enter the no of elements in the array:    ";       cin>>n;       for(i=0;i<n;i++)              cin>>a[i];           for(i=0;i<n;i++)       {              num=a[i];              if(isprime(num))

Page 83: c++ Lab Exercises

                     cout<<a[i]<<"\tPrime\n";              else                     cout<<a[i]<<"\tComposite\n";       }       getch();}

int isprime(int m){       for(int i=2;i<m/2;i++)              if(m%i==0)                     return (0);       return (1);}

OUTPUT :Enter the no of elements in the array:    52342256723      Prime42      Composite25      Composite6       Composite7       Prime

++++++++++++++++++++++++++++++++++++++++