Showing posts with label c plus plus. Show all posts
Showing posts with label c plus plus. Show all posts

Friday, 11 July 2014

Account details using OOP



#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class bank
{
long int accno;
char name[20];
float bal;
public:
void input();
void display();
};
void bank::input()
{
cout<<"Enter Your accont number:";
cin>>accno;
cout<<"Enter Your name:";
gets(name);
cout<<"Enter Your balance amount:";
cin>>bal;
}
void bank::display()
{
cout<<"Your account number is:"<<accno;
cout<<"\n"<<"Name is:";
puts(name);
cout<<"Balance amount:"<<bal;
}
void main()
{
clrscr();
bank obj;
obj.input();
obj.display();
getch();
}

Character is present or not in a string using OOP



#include<conio.h>
#include<iostream.h>
#include<string.h>
#include<stdio.h>
class character
{
char a,str[50];
int i,flag;
public:
void readdata();
void result();
};

void character::readdata()
{
cout<<"Enter a string:";
gets(str);
cout<<"Enter a charecter:";
cin>>a;
flag=0;
for(i=0;str[i]!='\0';i++)

if(str[i]==a)

flag=1;
}
void character::result()
{
if (flag==1)
cout<<"charecter is present";
else
cout<<"charecter is not present:";
}
void main()
{
clrscr();
character obj;
obj.readdata();
obj.result();
getch();
}

Matrix Multiplication using OOP



#include<iostream.h>
#include<conio.h>
class matrix
{
int i,j,a[10][10],b[10][10],c[10][10],row1,row2,col1,col2,k;
public:
void getdata();
void putdata();
void multiply();
};
void matrix::getdata()
{
cout<<"Enter row size & column size of the first matrix:";
cin>>row1>>col1;
cout<<"Enter the elements for"<<row1<<"*"<<col1<<"matrix:";
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
cin>>a[i][j];
}}
cout<<"Enter the row size & column size of second matrix:";
cin>>row2>>col2;
if(col1==row2)
{
cout<<"Enter the elements for"<<row2<<"*"<<col2<<"matrix:";
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
cin>>b[i][j];
}}
multiply();
putdata();
}
else
cout<<"multiplication is not possible";
}
void matrix::multiply()
{
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
c[i][j]=0;
for(k=0;k<row2;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}}}}
void matrix::putdata()
{
cout<<"resultant matrix is:";
for(i=0;i<row1;i++)
{
cout<<"\n";
for(j=0;j<col2;j++)
{
cout<<"\t"<<c[i][j];
}}}
void main()
{
clrscr();
matrix obj;
obj.getdata();
getch();
}

Subtraction of Two matrices using oop



#include<iostream.h>
#include<conio.h>
class matrix
{
public:
int i,j,row,col,a[10][10],b[10][10],c[10][10];
void getdata();
void subdata();
void putdata();
};
void matrix::getdata()
{
cout<<"Enter the size of row and column of Matrices:";
cin>>row>>col;
cout<<"Enter the elements for Ist"<<row<<"x"<<col<<"matrix:";
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cin>>a[i][j];
}}
cout<<"Enter the elements for IInd"<<row<<"X"<<col<<"matrix:";
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cin>>b[i][j];
}}}
void matrix::subdata()
{
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
c[i][j]=a[i][j]-b[i][j];
}}}
void matrix::putdata()
{
cout<<"the Difference of matrix is:";
for(i=0;i<row;i++)
{
cout<<"\n";
for(j=0;j<col;j++)
{
cout<<"\t"<<c[i][j];
}}}
void main()
{
clrscr();
matrix obj;
obj.getdata();
obj.subdata();
obj.putdata();
getch();
}

Addition of Two matrices using oop



#include<iostream.h>
#include<conio.h>
class matrix
{
public:
int i,j,row,col,a[10][10],b[10][10],c[10][10];
void getdata();
void sumdata();
void putdata();
};
void matrix::getdata()
{
cout<<"Enter the size of row and column of Matrices:";
cin>>row>>col;
cout<<"Enter the elements for Ist"<<row<<"x"<<col<<"matrix:";
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cin>>a[i][j];
}}
cout<<"Enter the elements for IInd"<<row<<"X"<<col<<"matrix:";
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cin>>b[i][j];
}}}
void matrix::sumdata()
{
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
c[i][j]=a[i][j]+b[i][j];
}}}
void matrix::putdata()
{
cout<<"the sum of matrix is:";
for(i=0;i<row;i++)
{
cout<<"\n";
for(j=0;j<col;j++)
{
cout<<"\t"<<c[i][j];
}}}
void main()
{
clrscr();
matrix obj;
obj.getdata();
obj.sumdata();
obj.putdata();
getch();
}

The following program illustrates how to use Objects as Function Arguments

#include<iostream.h>
#include<conio.h>
class distance
          {
          private :
                   int mts ;
                   int cms;
          public :
                   void init (int a, int b)
                             {
                             mts=a; cms = b;
                             }
                   void get();
                   void put();
                   void add(distance, distance) ;
          } ;
void distance::get()
{
                   cout<<"enter two distances in meter:";
                   cin>>mts>>cms;
                   }
void distance :: put ( )
          {
          cout<<"\n"<<mts<<" Meters "<<cms<<"centimeters";
          }
void distance :: add (distance dl, distance d2)
          {
          mts = dl.mts+d2.mts;
          cms = dl.cms+d2.cms;
          if (cms>100)
                             {
                             mts+=cms/100;
                             cms%=100;
                             }
}
void main ()
{
clrscr();
distance ob1,ob2,ob3;
ob1.get();
ob2.init(5,85);
ob3.add(ob1, ob2);
cout<<"The two distances are..";

ob1.put ( ) ;
cout<<"\nAnd";
ob2.put () ;
cout<<"\nSum is ";
ob3. put () ;
getch();
}
Out put
          Enter a distance in meters and cms : 7                 65
          The two distances are:
          7 Meters 65 Centimeters
          And
          5 Meters 85 Centimeters
          Sum is
13 Meters 50 Centimeters.

Saturday, 5 July 2014

Matrix Multiplication


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,a[10][10],b[10][10],c[10][10],row1,row2,col1,col2,k;

cout<<"Enter row size & column size of the first matrix:";
cin>>row1>>col1;
cout<<"Enter the elements for"<<row1<<"*"<<col1<<"matrix:";
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
cin>>a[i][j];
}
}
cout<<"Enter the row size & column size of second matrix:";
cin>>row2>>col2;
if(col1==row2)
{
cout<<"Enter the elements for"<<row2<<"*"<<col2<<"matrix:";
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
cin>>b[i][j];
}
}
}
else
{
cout<<"multiplication is not possible";
}

for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
c[i][j]=0;
for(k=0;k<row2;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}

cout<<"Resultant matrix is:";
for(i=0;i<row1;i++)
{
cout<<"\n";
for(j=0;j<col2;j++)
{
cout<<"\t"<<c[i][j];
}
}
getch();

}

Matrix addition

Matrix addition
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,row,col,a[10][10],b[10][10],c[10][10];

cout<<"Enter the size of row and column of Matrices:";
cin>>row>>col;
cout<<"Enter the elements for Ist"<<row<<"x"<<col<<"matrix:";
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cin>>a[i][j];
}
}
cout<<"Enter the elements for IInd"<<row<<"X"<<col<<"matrix:";
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cin>>b[i][j];
}
}


for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}

cout<<"The sum of matrices is:";
for(i=0;i<row;i++)
{
cout<<"\n";
for(j=0;j<col;j++)
{
cout<<"\t"<<c[i][j];
}
}
getch();
}


Sorting N numbers in ascending order.


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],i,j,limit,temp;
cout<<"ENTER THE LIMIT:";
cin>>limit;
cout<<"ENTER"<<limit<< "NUMBERS:";
for(i=0;i<limit;i++)
{
cin>>a[i];
}

for(i=0;i<limit-1;i++)
{
for(j=i+1;j<limit;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<"ARRAY AFTER SORTING:";
for(i=0;i<limit;i++)
{
cout<<"\t"<<a[i];
}
getch();
}


Print the element in an array in reverse order.


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],i,limit;
cout<<"ENTER THE LIMIT:";
cin>>limit;
cout<<"ENTER"<<limit<< "NUMBERS:";
for(i=0;i<limit;i++)
{
cin>>a[i];
}
cout<<"ARRAY IN REVERSE ORDER:";
for(i=limit-1;i>=0;i--)
{
cout<<"\t"<<a[i];
}
getch();
}


Print English alphabets A to Z



//Alphabets
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch='A';
do
{
cout<<"\t"<<ch;
ch=ch+1;
}while(ch<='Z');
getch();
}