Fibonacci
numbers
#include<iostream.h>
#include<conio.h>
int
fibo(int n)
{
int f;
if(n==0)
return 0;
else if(n==1)
return 1;
else
f=fibo(n-1)+fibo(n-2);
return f;
}
main()
{
int m,f;
clrscr();
cout<<"\n Enter a no.";
cin>>m;
for(int i=0;i<m;i++)
{
f=fibo(i);
cout<<f<<endl;
}
getch();
return 0;
}
Simple
Interest
#include<iostream.h>
#include<conio.h>
float
si(float p1,float n1,float r1)
{
float i;
i=p1*n1*r1/100;
return i;
}
main()
{
float p,n,r,s;
clrscr();
cout<<"\n Enter the Principal,No.
of years,Rate of Interest";
cin>>p>>n>>r;
s=si(p,n,r);
cout<<"\n Simple Interest =
"<<s;
getch();
return 0;
}
Prime
numbers between 1 to 100
#include<iostream.h>
#include<conio.h>
main()
{
int r;
clrscr();
for(int i=1;i<100;i++)
{
for(int j=2;j<=i;j++)
{
r=i%j;
if(r==0)
break;
}
if(i==j)
cout<<j<<endl;
}
getch();
return 0;
}
Sorting
#include<iostream.h>
#include<conio.h>
main()
{
int a[5],i,j,t,n;
clrscr();
cout<<"\n Enter size of the
array";
cin>>n;
cout<<"\n Enter the array
values";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
} }
}
cout<<"\n Sorted Array";
for(i=0;i<5;i++)
cout<<a[i]<<"\n";
getch();
// return 0;
}
Bank
Transactions
#include<iostream.h>
#include<conio.h>
class
bank
{
public:
char name[20],type[10];int ch;
long int no,nbalance,ebalance,deposit,debit;
void get()
{
cout<<"\n\nA/C Holder
Name:";
cin>>name;
cout<<"\n\nA/C Number:";
cin>>no;
cout<<"\n\nA/C type:";
cin>>type;
cout<<"\n\nA/C Balance:";
cin>>ebalance;
}
void trans();
};
void bank::trans()
{
cout<<"\n1.Deposit
\n\n2.Debit";
cout<<"\n\nEnter choice:";
cin>>ch;
switch(ch)
{
case 1: cout<<"\n\nEnter amount
to be deposited:";
cin>>deposit;
nbalance=ebalance+deposit;
cout<<"\n\nnew balance :"<<nbalance;
break;
case 2: cout<<"\n\nenter the
debiting amount:";
cin>>debit;
if(debit>ebalance)
{
cout<<"\n\namount can't be withdrawn:";
}
else
{
nbalance=ebalance-debit;
cout<<"\n\nnew balance:"<<nbalance;
}
break;
}
}
main()
{
bank b;
clrscr();
b.get();
b.trans();
getch();
return 0;
}
Quick
sort using class template
#include<iostream.h>
#include<conio.h>
template<class
T>
class
sort
{
public:
T
a[20];
//public:
int
n;
void
get();
void
quick(T a[],int lb,int ub);
void
disp();
};
template<class
T>
void
sort <T>::get()
{int
i;
cout<<"Enter
array size:";
cin>>n;
1cout<<"\nEnter
values:";
for(i=0;i<n;i++)
cin>>a[i];
}
template<class
T>
void
sort<T>::quick(T a[],int lb,int ub)
{
int i,j,temp,pivot;
if(lb<ub)
{
i=lb+1;
j=ub;
pivot=a[lb];
while(1)
{
while((a[i]<pivot)&&(i<=ub))
i++;
while((a[j]>pivot)&&(j>lb))
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
i++;j--;
}
else
break;
}
a[lb]=a[j];
a[j]=pivot;
quick(a,lb,j-1);
quick(a,j+1,ub);
}
}
template<class
T>
void
sort<T>::disp()
{ int i;
cout<<"\n
Elements after sorting:";
for(i=0;i<n;i++)
cout<<a[i];
}
int
main()
{
clrscr();
sort<int>s1;
s1.get();
s1.quick(s1.a,0,s1.n-1);
s1.disp();
sort<float>s2;
s2.get();
s2.quick(s2.a,0,s2.n-1);
s2.disp();
sort<char>s3;
s3.get();
s3.quick(s3.a,0,s3.n-1);
s3.disp();
getch();
}
Implementation
of dynamic casting
#include<iostream.h>
#include<conio.h>
class
sample1
{
public:
virtual
void disp()
{
}
};
class
sample2:public sample1
{
public:
void
disp()
{
cout<<"Hi";
}
};
main()
{
sample1
*ptr1,s1;
sample2
*ptr2,s2;
ptr1=&s2;
ptr2=dynamic_cast<sample2
*>(ptr1);
ptr1=dynamic_cast<sample1
*>(&s2);
if(!ptr2)
cout<<"\nCasting
succeeded";
else
cout<<"\nCasting
failed";
getch();
return
0;
}
Dynamic
polymorphism using friend functions
#include<iostream.h>
#include<conio.h>
class
matrix
{
public:
int **a;
int c,r,i,j,k;
matrix()
{
}
matrix(int r1,int c1);
matrix operator =(matrix );
friend matrix operator *(matrix,matrix );
void disp();
};
matrix::matrix(int
r1,int c1)
{
r=r1;c=c1;
*a=new int[r];
for(i=0;i<r;i++)
{
a[i]=new int[c];
}
cout<<"enter elements:";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
}
matrix
matrix :: operator=(matrix m)
{
r=m.r;c=m.c;
*a=new int[r];
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
a[i][j]=m.a[i][j];
}
}return *this;
}
matrix
operator *(matrix m1,matrix m2)
{
matrix m;int i,j,k;
m.r=m2.r;m.c=m2.c;
for(i=0;i<m.r;i++)
{
for(j=0;j<m.c;j++)
{
m.a[i][j]=0;
for(k=0;k<m.c;k++)
{
m.a[i][j]+=m1.a[i][k]*m2.a[k][j] ;
}
}
}return m;
}
void
matrix :: disp()
{
cout<<"resultant matrix:\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}
}
int
main()
{
int a,b;
clrscr();
cout<<"enter row &
column:";
cin>>a>>b;
matrix m1(a,b);
matrix m2=m1;
matrix m3;
m3=m1*m2;
m3.disp();
getch();
return 0;
}
Generation
of random numbers for Operations on Complex numbers
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
class
complex
{
public:
int real,img;
complex()
{ }
complex(int i,int j)
{
real=i;
img=j;
;
void
main()
{
clrscr();
ofstream out("file1.txt");
int s,n;
cout<<"\n Enter the range ";
cin>>n;
randomize();
int real=random(n);
int img=random(n);
out<<"("<<real<<"+i"<<img<<")";
s=random(4);
switch(s)
{
case 0:
out<<"+"; break;
case 1:
out<<"-"; break;
case 2:
out<<"*"; break;
case 3:
out<<"/"; break;
default: break;
}
real=random(n);
img=random(n);
out<<"("<<real<<"+i"<<img<<")";
out.close();
ifstream in("file1.txt");
char sy,op;
in>>sy>>real>>sy>>sy>>img>>sy;
in>>op;
complex c1(real,img);
in>>sy>>real>>sy>>sy>>img>>sy;
complex c2(real,img);
switch(op)
{
case '+': real=c1.real+c2.real;
img=c1.img+c2.img;
break;
case '-': real=c1.real-c2.real;
img=c1.img-c2.img;
break;
case '*':
real=(c1.real*c2.real)-(c1.img*c2.img);
img=(c1.real+c2.img)+(c1.img*c2.real);
break;
case '/':
real=((c1.real*c2.real)+(c1.img*c2.img))/((c2.real*c2.real)+(c2.img*c2.img));
img=((c1.img*c2.real)-(c1.real*c2.img))/((c2.real*c2.real)+(c2.img*c2.img));
break;
default : break;
}
ofstream nout("file2.txt");
nout<<"("<<real<<"+i"<<img<<")";
cout<<"\nOutput File has been Created!";
getch();
}
Implementation
of function overloading
#include<iostream.h>
#include<conio.h>
class func
{
public:
int a,l,b;
int a1,a2;
float r;
float a3;
void area(int );
void area(int ,int );
void area(float );
};
void func::area(int a)
{
a1=a*a;
cout<<"\n\narea of
square:"<<a1;
}
void func::area(int l,int b)
{
a2=l*b;
cout<<"\n\narea of
rectangle:"<<a2;
}
void func::area(float r)
{
a3=3.14*r*r;
cout<<"\n\narea of
circle:"<<a3;
}
int main()
{
func f;
clrscr();
cout<<"\n\nenter side a:";
cin>>f.a;
cout<<"\n\nenter length l:";
cin>>f.l;
cout<<"\n\nenter breadth b:";
cin>>f.b;
cout<<"\n\nenter radius r:";
cin>>f.r;
f.area(f.a);
f.area(f.l,f.b);
f.area(f.r);
getch();
return(0);
}
Implementation
of function template in various types of sorting
#include<iostream.h>
#include<conio.h>
/*BUBBLE SORT*/
template<class T>
void bubble(T a[],int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
T temp=a[i];
a[i]=a[j];
a[j]=temp;
} } }
cout<<"\n Elements after sorting
:";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
} /*INSERTION SORT*/
template<class T1>
void insert(T1 a[],int n)
{
int j,p,i;
for(p=1;p<n;p++)
{
T1 temp=a[p];
for(j=p;j>0 && a[j-1]>temp;j--)
a[j]=a[j-1];
a[j]=temp;
}
cout<<"\n Elements after sorting
:";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
}
/* MERGE SORT*/
int leftend,tmppos,numelem,i;
template<class T2>
void mergesort(T2 a[],int n)
{ int i;
T2 *tmparray;
tmparray=new T2;
if(tmparray!=NULL)
{
msort(a,tmparray,0,n-1);
delete tmparray;
}
cout<<"\n Elements after sorting
:";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
}template<class T2>
void msort(T2 a[],T2
tmparray[],int left,int right)
{ int center;
if(left<right)
{
center=(left+right)/2;
msort(a,tmparray,left,center);
msort(a,tmparray,center+1,right);
merge(a,tmparray,left,center+1,right);
}}
template<class T2>
void merge(T2 a[],T2
tmparray[],int lpos,int rpos,int rightend)
{ leftend=rpos-1;
tmppos=lpos;
numelem=rightend-lpos+1;
while(lpos<=leftend && rpos<=rightend)
{
if(a[lpos]<=a[rpos])
tmparray[tmppos++]=a[lpos++];
else
tmparray[tmppos++]=a[rpos++];
}
while(lpos<=leftend)
tmparray[tmppos++]=a[lpos++];
while(rpos<=rightend)
tmparray[tmppos++]=a[rpos++];
for(i=0;i<numelem;i++,rightend--)
a[rightend]=tmparray[rightend];
}
int main()
{
int n,i,ch;int a[10];float b[10];char c[10];
clrscr();
cout<<"\n Enter size of array
";
cin>>n;
cout<<"\n Enter integer
elements:";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\n Enter float
elements:";
for(i=0;i<n;i++)
cin>>b[i];
cout<<"\n Enter character
elements:";
for(i=0;i<n;i++)
cin>>c[i];
cout<<"\n1.Bubble
Sort\n2.Insertion\n3.Merge Sort\n";
do
{
cout<<"\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1: bubble(a,n);
bubble(b,n);
bubble(c,n);
break;
case 2: insert(a,n);
insert(b,n);
insert(c,n);
break;
case 3: mergesort(a,n);
mergesort(b,n);
mergesort(c,n);
break;
default:
cout<<"\n\nEnter correct
choice";
break;
} }while(ch<4);
getch();
return 0;
}
Implementation of stack using class
#include<iostream.h>
#include<conio.h>
//STACK
class stack
{
int a[20],top,size;
};
main()
{
stack
s;
int
ch,n,i;
top=-1;
cout<<"\nEnter
the size of the stack: ";
cin>>s.size;
cout<<"\n1.Push 2.Pop
3.Display";
do
{
cout<<"\nEnter
the choice:";
cin>>ch;
switch(ch)
{
case
1:try
{
if(s.top<s.size-1)
{
cout<<"\nEnter the no to
push:";
cin>>n;
s.top=s.top+1;
s.a[top]=n;
}
else
{
throw(s.size);
}
}
catch(int)
{
cout<<"\nStack
overflow";
}
break;
case
2:try
{
if(s.top!=-1)
{
cout<<"\nPoped element is
",s.a[top];
s.top--;
}
else
{
throw(s.top);
}
}
catch(int)
{
cout<<"\nStack underflow";
}
break;
case
3:try
{
if(s.top!=-1)
{
cout<<"\nElements in the
stack:";
for(i=s.top;i>=0;i--)
cout<<s.a[i]<<"\t";
}
else
{
throw(s.top);
}
}
catch(int)
{
cout<<"Stack Underflow";
}
break;
default:cout<<"\nEnter valid
choice...";
break;
}
}
while(ch<4);
return 0;
getch();
}
Overloading of [] operator
#include<iostream.h>
#include<conio.h>
#include<assert.h>
class sample
{
int a[10];
public:
int& operator[](int );
};
int& sample::operator[](int
n)
{
assert(n>=0 &&
n<10);
return a[n];
}
main()
{
sample s;
int i;
clrscr();
cout<<"\nEnter the
index:";
cin>>i;
s[i]=5;
cout<<"\nValue
is"<<s[i];
getch();
return 0;
}
OUTPUT:
Enter the index:15
Assertion failed: n>=0 && n<10,
file F:OOPS\INDEX.CPP, line 12
Abnormal program termination
Enter the index:5
Value is 5
Student
mark list using class
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student
{
public:
char name[50], result[10];
int flag,rno,i,j,m[5],tot;
float avg;
void get();
void calc();
void disp();
};
void student::get()
{
cout<<"Enter name:";
cin>>name;
cout<<"\n\nenter rno:";
cin>>rno;
for(i=0;i<5;i++)
{
cout<<"\n\nenter mark
"<<(i+1)<<": ";
cin>>m[i];
}
}
void student::calc()
{
tot = 0;
for(i=0;i<5;i++)
tot=tot+m[i];
avg=float(tot/5);
for(i=0;i<5;i++)
{
if(m[i]<50)
{
flag = 0;
strcpy(result,"fail");
}
}
if(flag!=0)
{
if((avg>=85)&&(avg<=100))
strcpy(result,"distinction");
else if((avg>=65)&&(avg<85))
strcpy(result,"first class");
else if((avg>=50)&&(avg<65))
strcpy(result,"second class");
}
}
void student::disp()
{
cout<<rno<<"\t"<<name;
for(i=0;i<5;i++)
{
cout<<"\t"<<m[i];
}
cout<<"\t"<<tot<<"\t"<<avg;
cout<<"\t"<<result;
}
int main()
{
student s[23];
int n,j;
clrscr();
cout<<"enter no of students :
";
cin>>n;
for(j=0;j<n;j++)
{
cout<<endl<<endl<<"Enter
details of student "<<(j+1)<<endl<<endl;
s[j].get();
s[j].calc();
}
cout<<"\n\nrno\tname\tm1\tm2\tm3\tm4\tm5\ttotal\tavg\tclass\n\n";
for(j=0;j<n;j++)
{
s[j].disp();
}
getch();
return 0;
}
Matrix
multiplication using friend functions and default arguments
#include<iostream.h>
#include<conio.h>
class vector;
class matrix
{
public:
int a[5][5],r,c;
void get1(int r1,int c1=2);
void disp1();
friend void multiply(matrix ,vector );
};
class vector
{
public:
int b[5];int j,n;
void get2();
void disp2();
friend void multiply(matrix ,vector );
};
void matrix::get1()
{
//cout<<"\n\nenter
rows & columns m & n:";
//cin>>m>>n;
cout<<"\n\nenter
matrix values : \n\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
}
void matrix::disp1()
{
cout<<"\n\nmatrix: \n\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}
}
void vector::get2()
{
cout<<"\n\nenter value equal to
n:";
cin>>n;
cout<<"\n\nenter matrix values :
\n\n";
for(j=0;j<n;j++)
{
cin>>b[j];
}
}
void vector::disp2()
{
cout<<"\n\nvector: \n\n";
for(j=0;j<n;j++)
{
cout<<b[j]<<"\n";
}
}
void multiply(matrix m1,vector
v1)
{
if(m1.c==v1.n)
{
int c[5],i,j;
cout<<"\n\nresultant
vector:\n\n";
for(i=0;i<m1.r;i++)
{
c[i]=0;
for(j=0;j<v1.c;j++)
{
c[i]+=m1.a[i][j]*v1.b[j];
}
cout<<c[i]<<"\n";
}
}
else
cout<<"multiplication
is not possible:";
int main()
{
int p;
cout<<"enter row:";
cin>>p;
cout<<"enter "
matrix m1;
vector v1;
clrscr();
m1.get1(p);
m1.disp1();
v1.get2();
v1.disp2();
multiply(m1,v1);
getch();
return 0;
}
Overloading of new and delete operator
#include<iostream.h>
#include<conio.h>
class sample
{
char name[20];
int rno;int i,m[5],total;float
avg;
public:
void *operator new(size_t s)
{
void *ptr=::new sample[s];
cout<<"\n\nMemory
Allocated:"<<s<<"bytes";
return ptr;
}
void get();
void calc();
void disp();
void operator delete(void *p)
{
::delete p;
cout<<"\n\n Memory deleted";
}
};
void sample::get()
{
cout<<"\n\n enter name:";
cin>>name;
cout<<"\n enter roll no:";
cin>>rno;
cout<<"\nEnter 3 marks:\n";
for(i=0;i<3;i++)
{
cin>>m[i];
}
}
void sample::calc()
{
total=0;
for(i=0;i<3;i++)
{
total=total+m[i];
}
avg=total/3;
}
void sample::disp()
{
cout<<"name\trollno\tm1\tm2\tm3\ttotal\taverage";
cout<<"\n"<<name<<"\t"<<rno<<"\t";
for(i=0;i<3;i++)
{
cout<<m[i]<<"\t";
}
cout<<total<<"\t"<<avg<<"\n";
}
int main()
{
int n,j;
clrscr();
cout<<"\nEnter nof students:";
cin>>n;
sample *s=new sample;
for(j=0;j<n;j++)
{
s[j].get();
s[j].calc();
s[j].disp();
}
delete s;
getch();
return 0;
}
Implementation of typeid using non
polymorphic objects
#include<iostream.h>
#include<conio.h>
#include<typeinfo>
class sample
{
public:
void disp()
{
cout<<"Hai";
}
};
int main()
{
int x,y;
char a;
sample s;
clrscr();
cout<<"typeid of x:"<<typeid(x).name ;
if(typeid(x)==typeid(y))
cout<<"Same data type";
if(typeid(a)!=typeid(y))
cout<<"Different data type";
return 0;
getch();
}
Implementation
of Arithmetic operator overloading
#include<iostream.h>
#include<conio.h>
class complex
{
int a,b;
//double a2,b2;
public:
complex(int x)
{
a=x;
b=x;
}
complex(double y)
{
a=y;
b=y;
}
complex()
{
}
void disp()
{
//if(b>0)
cout<<"\n\ncomplex no : ";
cout<<a<<"+i"<<b;
}
complex operator +(complex c2)
{
complex c;
c.a=a+c2.a;
c.b=b+c2.b;
return c;
}
complex operator -(complex c2)
{
complex c;
c.a=a-c2.a;
c.b=b-c2.b;
return c;
}
complex operator *(complex c2)
{
complex c;
c.a=((a*c2.a)-(b*c2.b));
c.b=((a*c2.b)+(b*c2.a));
return c;
}
complex operator /(complex c2)
{
complex c;
c.a=((a*c2.a)+(b*c2.b))/((c2.a*c2.a)+(c2.b*c2.b));
c.b=((a*c2.b)-(b*c2.a))/((c2.a*c2.a)+(c2.b*c2.b));
return c;
}
};
main()
{
int n;double m;
clrscr();
cout<<"\n\nenter integer
value:";
cin>>n;
cout<<"\n\nenter float
value:";
cin>>m;
complex c1=complex(n);
complex c2=complex(m);
complex c3;
c1.disp();
c2.disp();
cout<<"\n\ncomplex addition :
\n";
c3=c1.operator +(c2);
c3.disp();
cout<<"\n\ncomplex subtraction
:\n";
c3=c1.operator -(c2);
c3.disp();
cout<<"\n\ncomplex multiplication
:\n";
c3=c1.operator *(c2);
c3.disp();
cout<<"\n\ncomplex division
:\n";
c3=c1.operator /(c2);
c3.disp();
getch();
return 0;
}
Implementation of static functions and
static data members
#include<iostream.h>
#include<conio.h>
/*static data members*/
class sample
{
public:
static int a;
void get()
{
a++;
}
static void disp1()
{
cout<<"Using static
d"
cout<<"a:"<<a<<"\n";
}
void disp()
{
cout<<"a:"<<a<<"\n";
}
};
int sample::a=5;
main()
{
sample s1;
clrscr();
s1.get();
s1.disp();
sample::disp1();
getch();
return 0;
}
Implementation of namespace
#include<iostream>
using
namespace std;
namespace
sample
{
int ub;
int lb;
class counter
{
int count;
public:
counter (int n)
{
if(n<=ub)
count=n;
else
count=ub;
}
void reset(int n)
{
if(n<=ub)
count=n;
}
int run()
{
if(count>lb)
return count--;
else
return lb;
}
};
}
int
main()
{
using namespace sample;
ub=100;
lb=0;
counter c1(10);
int i;
cout<<"\n Values of object
c1:";
do
{
i=c1.run();
cout<<i<<"\t";
}while(i>lb);
counter c2(20);
cout<<"\n Values of object
c2:";
do
{
i=c2.run();
cout<<i<<"\t";
}while(i>lb);
c2.reset(100);
lb=90;
cout<<”\n Values of object c2:
cout<<"\n After Resetting :";
do
{
i=c2.run();
cout<<i<<"\t";
}while(i>lb);
}
OUTPUT:
Values of object c1 :10
9 8 7
6 5 4
3 2 1
0
Values
of object c2 :20 19 18
17 16 15
14 13 12
11 10 9 8
7 6 5
4 3 2
1 0
Values of object c2:
After
Resetting :100 99
98 97 96
95 94 93
92 91 90
No comments:
Post a Comment