Monday 8 April 2013

WRITE A C++ PROGRAM THAT USES A SINGLE FILE FOR BOTH READING AND WRITING THE DATA


/* WRITE A C++ PROGRAM THAT USES A SINGLE FILE FOR BOTH READING
AND WRITING THE DATA*/
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
char name[20];
int regno;
double perc;
clrscr();
ofstream writefile("nagaraj.dat");
cout<<"\nENTER YOLUR NAME:";
cin>>name;
writefile<<name<<endl;
cout<<"\nENTER YOUR REGNO:";
cin>>regno;
writefile<<regno<<endl;
cout<<"\nENTER YOUR PERCENTAGE:";
cin>>perc;
writefile<<perc<<endl;
writefile.close();
cout<<"\nFILE CREATED SUCCESSULLY........\n";
ifstream readfile("nagaraj.dat");
readfile>>name;
readfile>>regno;
readfile>>perc;
cout<<"\nCONTENTS OF THE FILE NAGARAJ.DAT IS READ AS FOLLOWS......\n";
cout<<"\nSTUDENT NAME:"<<name<<endl;
cout<<"\nSTUDENT REGISTER NUMBER:"<<regno<<endl;
cout<<"\nSTUDENT PERCENTAGE:"<<perc<<endl;
getch();
}

C++ Program To Illustrate Manipulators


/*WRITE A C++ PROGRAM TO ILLUSTRATE THE USE OF MANIPULATORS AND I/OS FUNCTION*/
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<math.h>
void main()
{
clrscr();
long int sqrno;
int i;
float sq;
cout<<"TO DISPLAY SQUARE AND SQUARE ROOT OF THE NUMBERS 2 TO 20\n";
cout<<"----------------------------------------------------------------------";
cout<<setw(20)<<"NUMBER"<<setw(20)<<"SQUARE"<<setw(20)<<"SQUARE ROOT"<<endl;
cout<<"----------------------------------------------------------------------\n";
for(i=2;i<=20;i+=2)
{
cout.fill('.');
cout.width(15);
cout.setf(ios::left);
cout<<i;
cout.setf(ios::left);
cout.width(15);
sqrno=i*i;
cout<<sqrno;
cout.setf(ios::showpoint);
cout.setf(ios::right);
sq=sqrt(i);
cout<<sq<<endl;
}
getch();
}

C++ Program To Add Two Complex Numbers


/*CREATE A CLASS "COMPLEX" TO HOLD A COMPLEX NUMBER. WRITE A FRIEND FUNCTION
TO ADD TWO COMPLEX NUMBERS. WRITE A MAIN PROGRAM TO CHECK THE SAME.*/
#include<iostream.h>
#include<conio.h>
class complex
{
private:float rp;
float ip;
public:void getdata();
void putdata();
friend complex add(complex c1,complex c2);
};
void complex::getdata()
{
cout<<"\nENTER THE REAL PART AND IMAGINARY PART:";
cin>>rp>>ip;
}
void complex::putdata()
{
cout<<rp<<"+i"<<ip<<endl;
}
complex add(complex c1,complex c2)
{
complex c3;
c3.rp=c1.rp+c2.rp;
c3.ip=c1.ip+c2.ip;
return c3;
}
void main()
{
complex c1,c3,c2;
clrscr();
cout<<"\nENTER THE FIRST COMPLEX NUMBER\n";
c1.getdata();
cout<<"\nENTER THE SECOND COMPLEX NUMBER\n";
c2.getdata();
cout<<"\nTHE FIRST COMPLEX NUMBER IS:";
c1.putdata();
cout<<"\nTHE SECOND COMPLEX NUMBER IS:";
c2.putdata();
cout<<"\nTHE ADDITION OF TWO COMPLEX NUMBERS IS:";
c3=add(c1,c2);
c3.putdata();
getch();
}

C++ Program To Illustrate Multiple Inheritence


#include<iostream.h>
#include<conio.h>
class shape
{
protected:double x;
double y;
public: void getdata()
{
cin>>x>>y;
}
virtual void displayarea(){};
};
class triangle:public shape
{
double area;
public:void displayarea()
{
area=0.5*x*y;
cout<<"area of triangle is:"<<area<<endl;
}
};
class rectangle:public shape
{
double area;
public: void displayarea()
{
area=x*y;
cout<<"area of a rectangle is:"<<area<<endl;
}
};
void main()
{
shape *s;
triangle t;
rectangle r;
clrscr();
cout<<"\nenter the values to find area of triangle\n";
s=&t;
s->getdata();
s->displayarea();
cout<<"\nenter the values to find area of rectangle\n";
s=&r;
s->getdata();
s->displayarea();
getch();
}

C++ Program To Implement == Operator Overloading


#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
private:char s[20];
public:string()
{
}
string(char a[])
{
strcpy(s,a);
}
int operator==(string a)
{
if(strcmp(s,a.s)==0)
return (1);
else
return (0);
}
};
void main()
{
char s1[20],s2[20];
clrscr();
cout<<"ENTER THE FIRST STRING:";
cin>>s1;
string obj1(s1);
cout<<"ENTER THE SECOND STRING:";
cin>>s2;
string obj2(s2);
if(obj1==obj2)
cout<<"TWO STRINGS ARE EQUAL";
else
cout<<"TWO STRINGS ARE NOT EQUAL";
getch();
}

C++ Program To Implement Matrix Operations


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int m,n,p,q;
class mat
{
int mt[10][10];
public: void add(mat m1,mat m2);
void sub(mat m1,mat m2);
void mul(mat m1,mat m2);
void transpose();
void read_matrix();
void display_matrix();
};

void mat::add(mat m1,mat m2)
  {
    for(int i=0;i<m;i++)
      for(int j=0;j<n;j++)
mt[i][j]=m1.mt[i][j]+m2.mt[i][j];
  }
void mat::sub(mat m1,mat m2)
  {
  for(int i=0;i<m;i++)
    for(int j=0;j<n;j++)
      mt[i][j]=m1.mt[i][j]-m2.mt[i][j];
  }
void mat::mul(mat m1,mat m2)
  {
  for(int i=0;i<m;i++)
    {
      for(int j=0;j<q;j++)
{
 mt[i][j]=0;
   for(int k=0;k<p;k++)
     mt[i][j]=mt[i][j]+m1.mt[i][k]*m2.mt[k][j];
}
    }
}
void mat::transpose()
{
  for(int i=0;i<n;i++)
    {
      for(int j=0;j<m;j++)
{
 cout<<"\t\t"<<mt[j][i];
}
cout<<"\n";
    }
}

void mat::read_matrix()
  {
    for(int i=0;i<m;i++)
      for(int j=0;j<n;j++)
cin>>mt[i][j];
  }

void mat::display_matrix()
  {
    for(int i=0;i<m;i++)
      {
for(int j=0;j<n;j++)
 {
   cout<<"\t\t"<<mt[i][j];
 }
      cout<<"\n\n";
     }
}

void main()
{
  mat m1,m2,msum,msub,mmul,mtrans;
  int ch;
  clrscr();
  cout<<"\nmatrix operations\n";
  cout<<"\nenter your choice\n";
  cout<<"\n1.addition\n2.subtraction\n3.multiplication\n4.transpose\n5.exit\n";
  cin>>ch;
  switch(ch)
  {
  case 1: cout<<"\nmatrix addition\n";
  cout<<"\nenter the row & column values for 1st matrix:";
  cin>>m>>n;
  cout<<"\nenter the row &column values for 2nd matrix:";
  cin>>p>>q;
       if((m!=p)||(n!=q))
       {
cout<<"\nmatric addition is not possible\n";
       getch();
     exit(0);
  }
  cout<<"\nenter the elements for 1st matrix\n";
  m1.read_matrix();
  cout<<"enter the elementes for 2nd matrix\n";
  m2.read_matrix();
  cout<<"the 1st matrix is\n";
  m1.display_matrix();
  cout<<"the 2nd matrix is \n";
  m2.display_matrix();
  cout<<"\naddition of two matrices\n";
  msum.add(m1,m2);
  msum.display_matrix();
  break;
 case 2: cout<<"\nmatrix subtraction \n:";
 cout<<"\nenter the row & column values for 1st matrix:";
  cin>>m>>n;
  cout<<"\nenter the row &column values for 2nd matrix:";
  cin>>p>>q;
  if((m!=p)||(n!=q))
   {
   cout<<"\nmatrix subtractioin not possible\n";
   getch();
   exit(0);
   }
 cout<<"\nenter the elements for 1st matrix\n";
 m1.read_matrix();
 cout<<"\nenter the elements for 2nd matrix\n";
 m2.read_matrix();
 cout<<"the 1st matrix is\n";
  m1.display_matrix();
  cout<<"the 2nd matrix is \n";
  m2.display_matrix();
 cout<<"\nsubtraction of two matrices is \n";
 msub.sub(m1,m2);
 msub.display_matrix();
 break;
 case 3: cout<<"\nmatrix multiplication\n";
 cout<<"\nenter the row & column values for 1st matrix:";
  cin>>m>>n;
  cout<<"\nenter the row &column values for 2nd matrix:";
  cin>>p>>q;
 if(n!=p)
   {
   cout<<"\nmatrix multiplication not possible\n";
   getch();
   exit(0);
   }
 cout<<"\nenter the elements for 1st matrix\n";
 m1.read_matrix();
 cout<<"\nenter the elements for 2nd matrix\n";
 int tm=m;
 m=p;
 n=q;
 m2.read_matrix();
 m=tm;
 cout<<"the 1st matrix is\n";
  m1.display_matrix();
  cout<<"the 2nd matrix is \n";
  m2.display_matrix();
 cout<<"\nmultiplication of two matrices is \n";
 mmul.mul(m1,m2);
 n=q;
 mmul.display_matrix();
 break;
 case 4: cout<<"\nmatrix transpose\n";
 cout<<"\nenter the row & column values for 1st matrix:";
  cin>>m>>n;
  cout<<"\nenter the row &column values for 2nd matrix:";
  cin>>p>>q;
  cout<<"\nenter the elements for matrix\n";
 mtrans.read_matrix();
 cout<<"\nthe entered matrix is\n";
 mtrans.display_matrix();
 cout<<"\nafter transposing matrix is \n";
 mtrans.transpose();
 break;
 default:exit(0);
 }
 getch();
}

C++PROGRAM TO IMPLEMENT EMPLOYEE DATABASE


#include<iostream.h>
#include<conio.h>
#include<math.h>
class employee
{
private: char ename[20];
int eno;
public:void getdata();
void putdata();
};
void employee::getdata()
{
cout<<"\nENTER THE NAME OF THE EMPLOYEE:";
cin>>ename;
cout<<"\nENTER THE EMPLOYEE NUMBER:";
cin>>eno;
}
void employee::putdata()
{
cout<<ename<<"\t\t"<<eno<<endl;
}
void main()
{
clrscr();
int n,i;
employee e[20];
cout<<"ENTER THE NUMBER OF EMPLOYEES REQUIRED\n";
cin>>n;
cout<<"ENTER THE EMPLOYEE DETAILS\n";
for(i=0;i<n;i++)
e[i].getdata();
cout<<"-------------------------------------------------------------------"<<endl;
cout<<"NAME\t\t"<<"ENUMBER"<<endl;
cout<<"-------------------------------------------------------------------"<<endl;
for(i=0;i<n;i++)
e[i].putdata();
getch();
}

C++ Program To Add Two Times



#include<iostream.h>
#include<conio.h>
class time
{
int hr;
int min;
int sec;
public:time()
{
hr=0;
min=0;
sec=0;
}
time(int hh,int mm,int ss)
{
hr=hh;
min=mm;
sec=ss;
}
void display()
{
cout<<hr<<":"<<min<<":"<<sec<<endl;
}
void gettime()
{
cout<<"ENTER THE TIME IN HH-MM-SS FORMAT\n";
cin>>hr>>min>>sec;
}
time addtime(time t1,time t2)
{
time t3;
t3.hr=t1.hr+t2.hr;
t3.min=t1.min+t2.min;
t3.sec=t1.sec+t2.sec;
if(t3.sec>=60)
{
t3.sec=t3.sec-60;
t3.min++;
}
if(t3.min>=60)
{
t3.min-=60;
t3.hr++;
}
return t3;
}
};
void main()
{
time t1,t2,t3;
cout<<"ENTER THE FIRST TIME\n";
t1.gettime();
cout<<"ENTER THE SECOND TIME\n";
t2.gettime();
cout<<"THE FIRST TIME IS:";
t1.display();
cout<<"THE SECOND TIME IS:";
t2.display();
cout<<"ADDITION OF TWO TIMES IS:";
t3=t3.addtime(t1,t2);
t3.display();
getch();
}


C++ PROGRAM TO READ & DISPLAY A MATRIX OF SIZE mXn


/*C++ PROGRAM TO READ & DISPLAY A MATRIX OF SIZE mXn
USING FUNCTIONS WINT ROW PARAMETER AS DEFAULT ARGUMENT FOR
READING & DISPLAYING*/
#include<iostream.h>
#include<conio.h>
int a[10][10];
void read(int n,int m=3);
void display(int n,int m=3);
void read(int n,int m)
{
cout<<"\nENTER ELEMENTS OF ORDER"<<m<<"*"<<n<<":";
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cin>>a[i][j];
}
}
}
void display(int n,int m)
{
cout<<"\nTHE MATRIX TO BE DISPLAYED IS"<<endl;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}
}
void main()
{
int m,n;
clrscr();
cout<<"\nENTER THE ORDER OF MATRIX:";
cin>>m>>n;
read(n,m);
display(n,m);
cout<<"\nENTER THE COLUMN VALUE ONLY:";
cin>>n;
cout<<"\nMATRIX READE & DISPLAYIN WITH "<<"DEFAULT ARGUMENTS FOR ROWS=3";
read(n);
display(n);
getch();
}

C++ PROGRAM TO COMPUTE POWER(m,n) Using Function OverLoading


/*C++ PROGRAM TO IMPLEMENT FUNCTION OVERLOADING IN ORDER TO COMPUTE POWER(m,n) WHERE
i)m IS DOUBLE AND n IS INT
ii)m & n ARE INT */
#include<iostream.h>
#include<conio.h>
#include<math.h>
double power(double m,int n)
{
double res=1.0;
res=pow(m,n);
return res;
}
int power(int m,int n)
{
int res=1;
res=pow(m,n);
return res;
}
void main()
{
double x;
int y;
clrscr();
cout<<"\nENTER THE VALUES AS DOUBLE & INTEGER TYPES:";
cin>>x>>y;
double res=power(x,y);
cout<<"\nPOW("<<x<<","<<y<<")="<<res<<endl;
int a,b;
cout<<"\nENTER THE VALUES AS INTEGER TYPES:";
cin>>a>>b;
int res1=power(a,b);
cout<<"\nPOW("<<a<<","<<b<<")="<<res1<<endl;
getch();
}

C++ PROGRAM TO SORT AN ARRAY OF INTEGER


/*C++ PROGRAM TO SORT AN ARRAY OF INTEGER IN ASCENDING ORDER USIN A
FOUNCTION CALLED EXCHANGE() WHICH ACCEPTS TWO INTEGER
ARGUMENTS BY REFERENCE*/
#include<iostream.h>
#include<conio.h>
void exchange(int &m,int &n)
{
int temp;
temp=m;
m=n;
n=temp;
}
void main()
{
int i,j,n,a[10];
clrscr();
cout<<"\nENTER THE NUMBER OF ELEMENTS:";
cin>>n;
cout<<"\nENTER THE ARRAY ELEMENTS"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
exchange(a[i],a[j]);
}
}
}
cout<<"\nTHE SORTED ARRAY ELEMENTS ARE"<<endl;
for(i=0;i<n;i++)
cout<<a[i]<<endl;
getch();
}

C++ PROGRAM TO FIND THE LARGEST OF THREE NUMBERS USING INLINE FUNCTION


#include<iostream.h>
#include<conio.h>
inline int large(int x,int y,int z)
{
if(x>y&&x>z)return x;
else if(y>z)return y;
else return z;
}
void main()
{
int x,y,z,big;
clrscr();
cout<<"\nENTER 3 NUMBERS:";
cin>>x>>y>>z;
big=large(x,y,z);
cout<<"\nTHE LARGEST NUMBER="<<big;
getch();
}

C++ PROGRAM TO DISPLAY A TABLE OF VALUES FOR A FUNCTION y=e^x WHERE X VARIES FROM 0 TO 1 IN STEPS OF 0.1


#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
double x;
clrscr();
cout<<"-------------------------------------------------------------\n";
cout<<"X\t\t\tY\n";
cout<<"-------------------------------------------------------------\n";
for(x=0;x<=1;x+=0.1)
{
double y=exp(x);
cout<<x<<"\t\t\t"<<y<<endl;
}
getch();
}

C++ PROGRAM WHICH CONVERTS GALLONS TO CUBIC FEET

/*(1 CUBIC FEET=7.481 GALLONS)*/
#include<iostream.h>
#include<conio.h>
void main()
{
float gal,cubf;
clrscr();
cout<<"\nENTER THE NUMBER OF GALLONS:";
cin>>gal;
cubf=gal/7.481;
cout<<"\nTHE VALUE OF "<<gal<<" GALLONS="<<cubf<<"CUBIC FEETS\n";
getch();
}

C PROGRAM TO FIND GCD OF TWO NUMBERS RECURSIVELY USINGEUCLIDIAN ALGORITHM

#include<stdio.h>
int gcd(int m,int n)
{
 return (n==0)?m:gcd(n,(m%n));
}
void main()
{
 int m,n;
 printf("ENTER THE TWO INTEGERS M AND N\n");
 scanf("%d%d",&m,&n);
 printf("THE GCD OF (%d,%d)=%d",m,n,gcd(m,n));
}