Institute of Aeronautical Engineering Dundigal: Oops Through C++ Lab Course File For Mca I/I Sem
Institute of Aeronautical Engineering Dundigal: Oops Through C++ Lab Course File For Mca I/I Sem
DUNDIGAL
PREPARED BY,
1
1.Write a C++ program to find both the largest and smallest number in
a list of integers.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int *a,n;
cout<<"Enter how many number are there ?\t";
cin>>n;
a=new int[n];
cout<<"Enter Numbers"<<endl;
for(int i=0;i<n;i++)
cin>>a[i];
int temp;
for(i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
Output:
Enter how many number are there:
5
Enter Numbers
15 2 34 56 78
The Minimum Number is 2
The Maximum Number is 78
2
2. Write a C++ program to find the sum of individual digits of a positive
integer.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<"Enter The Number\t";
long signed n,temp,sum=0;
cin>>n;
while(n>0)
{
temp=n%10;
sum=temp+sum;
n=n/10;
}
cout<<"The sum of individuval digits" <<"of a entered positive integer is\t"<<sum;
getch();
}
Output:
3
3. Write a C++ program to generate all the prime numbers between 1
and n , where n is a value supplied by the user.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
cout<<"Prime numbers between 1 to \t";
unigned long int n;
cin>>n;
for(unsigned long int i=2;i<=n;i++)
if(prime(i)==1)
cout<<i<<" ";
getch();
return 0;
}
Output:
Prime numbers between 1 to
10
1357
4
4. Write a C++ program to sort a list of numbers in ascending order.
#include<iostream.h>
#include<conio.h>
int order(int &,int &);
int main()
{
clrscr();
int *a,k;
cout<<"How many number are there\t";
cin>>k;
a=new int[k];
cout<<"Enter numbers"<<endl;
for(int i=0;i<k;i++)
cin>>a[i];
for(i=0;i<k;i++)
{cout<<" ";
for(int j=i+1;j<k;j++)
{if(a[i]>a[j])
swap(a[i],a[j]);}
cout<<a[i];}
getch();
return 0;
}
Output:
How many number are there
5
Enter numbers
12 45 79 06 90
5
5. Write a C++ program to find out swapping of two integers.
#include<iostream.h>
#include<conio.h>
void swap(int ,int );
void swap(float ,float );
void swap(char ,char );
int main()
{
clrscr();
swap(2,5);
float a=20.5,b=30.26;
swap(a,b);
swap('a','x');
getch();
return 0;
}
Output:
Before swaping 2,5
After swaping 5,2
Before swaping 20.5 30.26
After swaping 30.26 20.5
6
6.Write a c++ program to find number is Armstrong Number or not
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int no,dig,temp,sum=0;
clrscr();
printf("Enter no : ");
scanf("%d",&no);
temp=no;
while(temp>0)
{
dig=temp%10;
sum=sum+dig*dig*dig;
temp=temp/10;
}
if (no==sum)
printf("\n %d is an Armstrong Number",no);
else
printf("\n %d is not an Armstrong Number",no);
getch();
Output:
7
7. Write a C++ program to generate the first n terms of the fiboncci
sequence.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<"How many terms to be generated ?\t";
double k;
cin>>k;
double f1=0,f2=1,f3;
cout<<f1<<" "<<f2;
f3=f1+f2;
for(double i=1;i<=k-2;i++)
{
cout<<" "<<f3;
f1=f2;
f2=f3;
f3=f1+f2;
}
getch();
}
Output:
8
8.Write a C++ program to implement the matrix ADT using a class. The
operations supported by this ADT are:
a) Reading a matrix. c) Addition of matrices.
b) Printing a matrix. d) Subtraction of matrices.
e) Multiplication of matrices.
#include<iostream.h>
#include<conio.h>
struct mo
{
signed p,q,r,s,b,choice;
};
matrix::matrix(int x,int y)
{
m=x;n=y;
a=new int*[m];
for(int i=0;i<m;i++)
a[i]=new int[n];
}
void matrix::readmatrix()
{
cout<<"Enter Matrix elements"<<endl;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
9
}
void matrix::printmatrix()
{
cout<<"The result Matrix is "<<endl;
for(int i=0;i<m;i++)
{ { for(int j=0;j<n;j++)
{ cout.width(3);
cout<<a[i][j];}
} cout<<endl;}}
matrix c(a.m,a.n);
for(int i=0;i<a.m;i++)
for(int j=0;j<a.n;j++)
c.a[i][j]=a.a[i][j]-b.a[i][j];
return c;
}
matrix c(a.m,b.n);
for(int i=0;i<a.m;i++)
for(int j=0;j<b.n;j++)
{c.a[i][j]=0;
{ for(int k=0;k<a.n;k++)
c.a[i][j]+=a.a[i][k]*b.a[k][j];}}
return c;
}
int main()
{
clrscr();
int a;
struct mo k;
10
do
{
cout<<"1.Addition of two Matrices"<<endl;
cout<<"2.Subtraction of two Matrices"<<endl;
cout<<"3.Multiplication of two Matrices"<<endl;
cout<<"Enter your chioice\t";
cin>>a;
if(a>=0&&a<4)
{k=order(a);
if(!k.b==0)
{if(k.choice==(1||2))
a=matrixr(a,k);
else
a=matrixr(a,k);
}} }while(a>=0&&a<4);
return 0;
}
}return 0;
}
mo order(int a)
{
11
struct mo s;
s.choice=a;
cout<<"Enter 1st Matirx order";
cin>>s.p>>s.q;
cout<<"Enter 2nd Matrix order";
cin>>s.r>>s.s;
if(a==1||a==2)
{
if(s.p==s.r&&s.q==s.s)
return s;
else
s.b=0;
cout<<"Matrix addition/subtraction is not possible"<<endl;
return s;
}
if(s.q==s.r||(s.p==s.r&&s.q==s.s))
return s;
else
s.b=0;
cout<<"Matrix Multiplicatin is not possible"<<endl;
return s;
}
12
9. Write a C++ program to generate Pascal’s triangle.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
for(long unsigned i=0,k=35;i<12;i++,k=k-3)
{
for(long unsigned j=0,l=k;j<=i;j++,l=l+6)
{
gotoxy(l,i+1);
cout.width(3);
cout<<ncr(i,j);
}cout<<endl<<endl; }
getch();
return 0;
}
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
13
10. Write a C++ program that uses a recursive function for solving
Towers of Hanoi problem.
#include<iostream.h>
#include<conio.h>
int main()
{
int n;
cout<<"\nEnter the number of disks\n";
cin>>n;
diskchange('L','R','C',n);
cout<<"\nPress any key to exit\n";
getch();
return 0;
}
Output:
Enter the number of disks
3
Move disk1 from A to C
Move disk2 from A to B
Move disk1 from C to B
Move disk3 from A to C
Move disk1 from B to A
Move disk2 fromB to C
Move disk1 from A to C
14
11. Write a C++ program to determine if the given string is a
palindrome or not.
#include<iostream.h>
#include<conio.h>
#include<string.h>
int main()
{
clrscr();
char *p=new char[20];
cout<<"enter string\t";
cin>>p;
char *q=new char[20];
strcpy(q,p);
char *r=new char[20];
r=strrev(q);
if(!strcmp(r,p))
cout<<p<<"\t The given string is palindrome";
else
cout<<p<<"\ t The given string is not palindrome";
getch();
return 0;
}
Output:
15
12. write a c++ program to find out string ascending order.
#include<iostream.h>
int i,j, min, min_pos;
char min_name[5], temp[5];
for(i=0;i<3;i++)
{
strcpy(min_name, letters[i]); //takes the first letter as the index
min_pos=i;
for(j=i;j<3;j++)
{
if ((strcmp(min_name, letters[j])>0))
{
strcpy(min_name,letters[j]);
min_pos=j; / rember position
}
}
strcpy(temp,letters[i]);
strcpy(letters[i],letters[min_pos]);
strcpy(letters[min_pos],temp);
}
Output:
Enter no of strings
3
Ravi venu sudheer
Sorted names:
ravi sudheer venu
16
13. Write a C++ program to perform single inheritance
#include <iostream>
using namespace std;
class base
{
int i, j;
public:
void set(int a, int b) { i=a; j=b; }
void show()
{
cout << “This is base class”;
}
};
class derived : public base
{
int k;
public:
derived(int x) { k=x; }
void showk() { cout <<"This is derived class”;
}
};
int main()
{
derived ob(3);
ob.set(1, 2); // access member of base
ob.show(); // access member of base
ob.showk(); // uses member of derived class
return 0;
}
Output:
This is base class
This is derived class
17
14. Write a C++ program to perform multiple inheritance
#include <iostream>
using namespace std;
class base1
{
protected:
int x;
public:
void showx() { cout << x << "\n"; }
};
class base2
{
protected:
int y;
public:
void showy() {cout << y << "\n";}
};
// Inherit multiple base classes.
class derived: public base1, public base2
{
public:
void set(int i, int j) { x=i; y=j; }
};
int main()
{
derived ob;
ob.set(10, 20); // provided by derived
ob.showx(); // from base1
ob.showy(); // from base2
return 0;
}
18
15. Write a C++ program to perform multi-level inheritance
#include< iostream.h>
#include< conio.h>
19
void displayresult(void)
{
total = m1 + m2;
putdata();
puttest();
cout< < " Total Of The Two \t: "< < total< < endl;
}
};
void main()
{
clrscr();
int x;
float y,z;
char n[20];
cout< < "Enter Your Name:";
cin>>n;
cout< < "Enter The Roll Number:";
cin>>x;
result r1;
r1.getdata(x,n);
cout< < "ENTER COMPUTER PROGRAMMING MARKS:";
cin>>y;
cout< < "ENTER DRAWING MARKS:";
cin>>z;
r1.gettest(y,z);
cout< < endl< < endl< < "************ RESULT **************"< < endl;
r1.displayresult();
cout< < "**********************************"< < endl;
getch();
OUTPUT
Enter Your Name: Lionel
Enter The Roll Number:44
ENTER COMPUTER PROGRAMMING MARKS:95
ENTER DRAWING MARKS:90
20
16. Write a C++ program to perform hierarichial inheritance
#include <iostream.h>
class vehicle
{
protected:
int wheels;
float weight;
public:
void initialize(int in_wheels, float in_weight);
int get_wheels(void) {return wheels;}
float get_weight(void) {return weight;}
float wheel_loading(void) {return weight/wheels;}
};
Void main()
{
vehicle unicycle;
unicycle.initialize(1, 12.5);
cout << "The unicycle has " <<
unicycle.get_wheels() << " wheel.n";
cout << "The unicycle's wheel loading is " <<
unicycle.wheel_loading() << " pounds on the single tire.n";
21
cout << "The unicycle weighs " <<
unicycle.get_weight() << " pounds.nn";
car sedan;
truck semi;
semi.initialize(18, 12500.0);
semi.init_truck(1, 33675.0);
cout << "The semi weighs " << semi.get_weight() << " pounds.n";
cout << "The semi's efficiency is " <<100.0 * semi.efficiency() << " percent.n";
}
Float truck::efficiency(void)
{
return payload / (payload + weight);
}
22
17. Write a C++ program to perform hybraid inheritance
#include<iostream.h>
class a
{
public:
void f()
{
cout<<"base class\n";
}
};
class b:private a //visibility mode private
{
public:
void f1()
{
cout<<"inheritance\n";
}
};
class c:public a //visibility mode public
{
public:
void f2()
{
cout<<"hybrid\n";
}
};
class d:public b,public c
void main()
{
d w;
w.f(); //giving ambiguity error
}
23
18. Write a C++ program to perform parameterized constructor.
#include <iostream>
using namespace std;
class myclass {
int a, b;
public:
myclass(int i, int j)
{
a=i;
b=j;
}
void show()
{
cout << a << " " << b;
}
};
int main()
{
myclass ob(3, 5);
ob.show();
return 0;
}
Output:
3 5
24
19. Write a C++ program that illustrates virtual functions.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class media
{
protected:
char title[50];
float price;
public:
media(char *s,float a)
{
strcpy(title,s);
price=a;
}
virtual void display(){};
};
25
cout<<"\nPages"<<pages;
cout<<"\nPrice"<<price;
}
void tape::display()
{
cout<<"\nTitle"<<title;
cout<<"\nTime"<<time;
cout<<"\nPrice"<<price;
}
int main()
{
clrscr();
char *title=new char[30];
float price,time;
int pages;
cout<<"Enter book details"<<endl;
cout<<"Title\t:";cin>>title;
cout<<"Price\t:";cin>>price;
cout<<"Pages\t:";cin>>pages;
book b1(title,price,pages);
cout<<"Enter tape ditails"<<endl;
cout<<"Title\t:";cin>>title;
cout<<"Price\t:";cin>>price;
cout<<"Time\t:";cin>>time;
tape t1(title,price,time);
media* list[2];
list[0]=&b1;
list[1]=&t1;
cout<<"_______Book_______"<<endl;
list[0]->display();
cout<<"_______Tape_______"<<endl;
list[1]->display();
getch();
return 0;
}
26
20. Write a C++ program to perform virtual destructors.
#include <iostream.h>
class Base
{
public:
Base(){ cout<<"Constructor: Base"<<endl;}
virtual ~Base(){ cout<<"Destructor : Base"<<endl;}
};
class Derived: public Base
{
//Doing a lot of jobs by extending the functionality
public:
Derived(){ cout<<"Constructor: Derived"<<endl;}
~Derived(){ cout<<"Destructor : Derived"<<endl;}
};
void main()
{
Base *Var = new Derived();
delete Var;
}
Output:
Constructor: Derived
Constructor: Base
Destructor : Derived
Destructor : Base
27
21. Write a C++ program that uses
(A) function templates. (B)class templates
cout<<endl;
ascen(a,n);
cout<<endl;
ascen(b,n);
cout<<endl;
ascen(c,n);
getch();
return 0;
}
28
{cout.precision(2);
cout<<a[i]<<"\t";}
}
(B)class templates
#include <iostream>
using namespace std ;
template <>
class stream<char>
{
public:
void f() { cout << "stream<char>::f()"<< endl ;}
};
int main()
{
stream<int> si ;
stream<char> sc ;
si.f() ;
sc.f() ;
return 0 ;
}
Output:
stream<T>::f()
stream<char>::f()
29
25. write a c++ program to find out friend class.
#include <iostream>
using namespace std;
class MyClass
{
friend class MyFriend; // Declare a friend class...
public MyClass()
{
m_private = true;
cout << "m_private = " << m_private << endl;
}
class MyFriend
{
public:
void main()
{
MyClass myClass;
MyFriend myFriend;
myFriend.change( myClass );
return 0;
}
30
26. write a c++ program to find out pure virtual functions.
#include <iostream>
using namespace std;
class A
{
public:
class B: public A
{
public:
Void main( )
{
//A obj1; // Can't create an object of class type A, but...
Obj2.display();
return 0;
}
Output:
31
27. Write a c++ program to perform Implement the complex number
ADT in C++ using a class. The complex ADT is used to represent
complex numbers of the form c=a+ib, where a and b are real numbers.
The operations supported by this ADT are:
#include<iostream.h>
#include<conio.h>
class complex
{
float r,im;
public:
complex(){}
friend ostream & operator <<(ostream &,complex &);
friend istream & operator >>(istream &,complex &);
complex operator +(complex &);
complex operator -(complex &);
complex operator *(complex &);
complex operator /(complex &);
~complex(){}
};
return read;
}
32
ostream & operator<<(ostream &out,complex &b)
{
out<<b.r;
if(b.im>=0)
cout<<"+";
cout<<b.im<<"i";
return out;
}
33
return c;
int main()
{
clrscr();
complex a,b,c;
cin>>a;
cin>>b;
c=a+b;
cout<<endl<<"Addition result"<<endl;
cout<<c;
cout<<endl<<"Subtraction result"<<endl;
c=a-b;
cout<<c;
c=a*b;
cout<<endl<<"Multiplication result"<<endl;
cout<<c;
cout<<endl<<"Division result"<<endl;
c=a/b;
getch();
return 0;
}
34
29. write a c++ program to find out exception handling by multilple
catch statements.
#include <iostream>
using namespace std;
.
void Xhandler(int test)
{
Try
{
if(test) throw test;
else throw "Value is zero";
}
catch(int i)
{
cout << "Caught Exception #: " << i << '\n';
}
Catch(const char *str) {
Cout << "Caught a string: ";
Cout << str << '\n';
}
}
void main()
{
cout << "Start\n";
Xhandler(1);
Xhandler(2);
Xhandler(0);
Xhandler(3);
cout << "End";
}
output:
Caught Exception #: 1
Caught Exception #: 2
Caught a string: Value is zero
Caught Exception #: 3
End
35
30. Write a C++ program for rethrowing an exception.
#include <iostream>
using namespace std;
void Xhandler()
{
try {
throw "hello"; // throw a char *
}
catch(const char *) { // catch a char *
cout << "Caught char * inside Xhandler\n";
throw ; // rethrow char * out of function
}
}
Note
void main()
{
cout << "Start\n";
try{
Xhandler();
}
catch(const char *) {
cout << "Caught char * inside main\n";
}
cout << "End";
return 0;
}
output:
36
32. Write a C++ program which copies one file to another.
#include<iostream.h>
#include<fstream.h>
Output:
37