100420825160 Ankit Dixit
1 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 1 : Write a program to check whether a given number is even or odd.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
cout<<"Enter number: ";
cin>>n;
if(n%2==0)
cout<<"Number is even";
else
cout<<"Number is odd";
getch();
}
100420825160 Ankit Dixit
2 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 2: Write a program to find the greatest of three numbers using conditional
operator.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
cout<<"Enter a: ";
cin>>a;
cout<<"Enter b: ";
cin>>b;
cout<<"Enter c: ";
cin>>c;
if(a>b)
{
if(a>c)
cout<<"Largest is "<<a;
else
{
if (b>c)
cout<<"Largest is "<<b;
}}
else
cout<<"Largest is "<<c;
getch();
}
100420825160 Ankit Dixit
3 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 3 : Write a program to find the sum of digits of a number.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,num,sum=0;
cout<<"n Enter the number: ";
cin>>n;
num=n;
while(num>0)
{
sum+=num%10;
num/=10;
}
cout<<"n The sum of digits of a number is "<<sum;
getch();
}
100420825160 Ankit Dixit
4 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 4 : Write a program to generate Fibonacci series of first n numbers.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
int f1,f2,f3;
cout<<"Enter how many number series you need: ";
cin>>n;
f1=1;
f2=1;
cout<<f1<<endl;
cout<<f2<<endl;
int c=2;
while(c<n)
{
f3=f1+f2;
f1=f2;
f2=f3;
cout<<f3<<endl;
c++;
}
getch();
}
100420825160 Ankit Dixit
5 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 5: Write a program to print the multiplication table of a given number.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i;
cout<<"enter a no. for which you need a table";
cin>>n;
for(i=1;i<=10;i++)
cout<<n<<"*"<<i<<"="<<n*i<<endl;
getch();
}
100420825160 Ankit Dixit
6 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 6 : Write a program to calculate the factorial of a number.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,num,fact=1;
cout<<"Enter the number: ";
cin>>num;
i=num;
while (num>0)
{
fact=fact*num;
--num;
}
cout<<"Factorial of "<<i<<" is "<<fact;
getch();
}
100420825160 Ankit Dixit
7 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 7 : Write a program to add, subtract, multiply & division o two numbers
using switch statement.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
char opr;
cout<<"Enter any two numbers: ";
cin>>a>>b;
cout<<"Choose any operator(+,-,*,/): ";
cin>>opr;
switch(opr)
{
case'+':
int c=a+b;
cout<<"The addition of two numbers is: "<<c;
break;
case'-':
int d=a-b;
cout<<"The subtraction of two numbers is: "<<d;
break;
case'*':
int e=a*b;
cout<<"The multiplication of two numbers is: "<<e;
break;
case'/':
int f=a/b;
cout<<"The division of two numbers is: "<<f;
break;
default:
cout<<"Wrong operator";
}
getch();
}
100420825160 Ankit Dixit
8 Submitted To: Ms. Achhardeep Kaur
100420825160 Ankit Dixit
9 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 8 : Write a program to check whether a given number is prime or not.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num,i,c=0;
cout<<"enter number: ";
cin>>num;
for(i=2;i<=num-1;i++)
{
if(num%i==0)
c++;
}
if(c!=0)
cout<<"it is not prime no.";
else
cout<<"it is prime no.";
getch();
}
100420825160 Ankit Dixit
10 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 9 : Write a program to print the given pattern.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
for(int i=1;i<6;i++)
{
for(int j=6-i;j>=0;j--)
cout<<" ";
for(int k=i;k>=2;k--)
cout<<k;
for(int l=1;l<=i;l++)
cout<<l;
cout<<"n";
}
getch();
}
100420825160 Ankit Dixit
11 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 10: Write a program to demonstrate the working of call by value.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y;
x=10;
y=20;
void swap(int,int);
swap(x,y);
cout<<"x= "<<x<<endl;
cout<<"y= "<<y;
getch();
}
void swap(int a, int b)
{
int t=a;
a=b;
b=t;
}
100420825160 Ankit Dixit
12 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 11: Write a program to demonstrate the working of call by reference.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y;
x=10;
y=20;
void swap(int&, int&);
swap(x,y);
cout<<"x= "<<x<<endl;
cout<<"y= "<<y;
getch();
}
void swap(int &a, int &b)
{
int t=a;
a=b;
b=t;
}
100420825160 Ankit Dixit
13 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 12: Write a program to demonstrate the working of pointers.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *p;
int x;
cout<<"Enter a value: ";
cin>>x;
p=&x;
*p=*p+10;
cout<<"Now x is: "<<x<<endl;
cout<<"Value of x through p: "<<*p<<endl;
cout<<"The address of x is: "<<&x<<endl;
cout<<"The address of x is: "<<p<<endl;
getch();
}
100420825160 Ankit Dixit
14 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 13: Write a program to demonstrate the use of inline functions.
#include<iostream.h>
#include<conio.h>
inline double cube(double x)
{
return(x*x*x);
}
void main()
{
clrscr();
double a;
cout<<"Enter a number: "
cin>>a;
cout<<"The cube of "<<a<<"is: "
cout<<cube(a);
getch();
}
100420825160 Ankit Dixit
15 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 14: Write a program to demonstrate the working of default arguments.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum(int=30,int=30);
int a,b;
cout<<"Enter value of a: ";
cin>>a;
cout<<"Enter value of b: ";
cin>>b;
sum(a,b);
sum(a);
sum();
getch();
}
int sum(int x,int y)
{
int s=x+y;
cout<<"sum= "<<s<<endl;
return 0;
}
100420825160 Ankit Dixit
16 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 15: Write a program to demonstrate the use of recursion.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int fact(int);
int f,n;
cout<<"Enter the number: ";
cin>>n;
f=fact(n);
cout<<"FACTORIAL= "<<f;
getch();
}
int fact(int n)
{
int value=1;
if(n==1)
return(value);
else
{
value=n*fact(n-1);
return(value);
}
getch();
}
100420825160 Ankit Dixit
17 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 16: Write a program which reads two matrices and then print a matrix
which is addition of these two.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3],b[3][3],s[3][3];
cout<<"Enter first matrix elements: "<<endl;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
cin>>a[i][j];
}
cout<<"n Enter second matrix: "<<"nn";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
cin>>b[i][j];
}
cout<<"n The first matrix is:"<<"nn";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<a[i][j];
cout<<"t";
}
cout<<endl;
}
cout<<"n The second matrix is:"<<"nn";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<b[i][j];
cout<<"t";
}
cout<<endl;
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
s[i][j] = a[i][j] + b[i][j];
}
100420825160 Ankit Dixit
18 Submitted To: Ms. Achhardeep Kaur
cout<<"n The addition of two matrices is: "<<"nn";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<s[i][j]<<'t';
}
cout<<endl;
}
getch();
}
100420825160 Ankit Dixit
19 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 17: Write a program to read a string and then copy that string into another.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char a[80],b[80];
void string_copy(char b[],char a[]);
cout<<"Enter a string: "<<endl;
cin.get(a,80);
string_copy (b,a);
cout<<"First string is: "<<endl;
cout<<a<<endl;
cout<<"Second string is: "<<endl;
cout<<b<<endl;
getch();
}
void string_copy (char b[],char a[])
{
int i,j;
i=0;
while(a[i]!='0')
{
b[i]=a[i];
i++;
}
b[i]='0';
}
100420825160 Ankit Dixit
20 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 18: Write a program to demonstrate the working of structure within a
structure.
#include<iostream.h>
#include<conio.h>
struct date
{
int day;
char month_name[20];
int year;
};
struct biodata
{
char name[80];
date dob;
};
void main()
{clrscr();
biodata student;
cout<<"Enter name: "<<endl;
cin>>student.name;
cout<<"ENTER DATE OF BIRTH:"<<endl;
cout<<"Enter day: ";
cin>>student.dob.day;
cout<<"Enter month name: ";
cin>>student.dob.month_name;
cout<<"Enter year: ";
cin>>student.dob.year;
cout<<"The biodata is: "<<endl;
cout<<"Name: "<<student.name<<endl;
cout<<"Date of birth: ";
cout<<student.dob.day<<"-"<<student.dob.month_name<<"-";
cout<<student.dob.year<<endl;
getch();
}
100420825160 Ankit Dixit
21 Submitted To: Ms. Achhardeep Kaur
100420825160 Ankit Dixit
22 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 19: Write a program to calculate the area of rectangle using the concept of
object class.
#include<iostream.h>
#include<conio.h>
class area
{
public:
int l,b,area;
void getdata()
{
cout<<"Enter the length of the rectangle: ";
cin>>l;
cout<<"Enter the breadth of the rectangle: ";
cin>>b;
}
void display()
{
area=l*b;
cout<<"The area of rectangle is: "<<area;
}};
void main()
{
clrscr();
area s1;
s1.getdata();
s1.display();
getch();
}
100420825160 Ankit Dixit
23 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 20 : Write a program to demonstrate data hiding, encapsulation and
abstraction.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
class date
{
public:
int day;
int month;
int year;
};
class date today;
today.day=12;
today.month=1;
today.year=1992;
cout<<"Today's date is "<<today.day<<"/";
cout<<today.month<<"/"<<today.year<<endl;
getch();
}
100420825160 Ankit Dixit
24 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 21 : Write a program to show the working of static data members and static
member functions.
#include<iostream.h>
#include<conio.h>
class alpha
{
private:
static int x;
public:
alpha();
static void display()
{
cout<<"Content of x ";
cout<<"after incremented by one= "<<x<<endl;
}};
class beta
{
private:
int y;
public:
void getdata()
{
cout<<"Enter a value for y= n";
cin>>y;
}
void display()
{
cout<<"Content of y= "<<this->y<<endl;
}};
int alpha::x=10;
alpha::alpha()
{
++x;
}
void main()
{
clrscr();
alpha objx;
beta objy;
objy.getdata();
alpha::display();
objy.display();
getch();
}
100420825160 Ankit Dixit
25 Submitted To: Ms. Achhardeep Kaur
100420825160 Ankit Dixit
26 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 22 : Write a program to demonstrate the working of constructor.
#include<iostream.h>
#include<conio.h>
class sample
{
int a,b;
public:
sample()
{
cout<<"This is constructor. "<<endl;
a=100;
b=200;
}
int add()
{
return(a+b);
}};
void main()
{
clrscr();
sample s;
cout<<"Output is: "<<s.add()<<endl;
getch();
}
100420825160 Ankit Dixit
27 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 23: Write a program to show the working of parameterized constructor.
#include<iostream.h>
#include<conio.h>
class student
{
int a,b;
public:
student(int a1,int b1)
{
a=a1;
b=b1;
}
int mul()
{
return(a*b);
}};
void main()
{
clrscr();
int i,j;
cout<<"Enter first number: ";
cin>>i;
cout<<"Enter second number: ";
cin>>j;
student c1(i,j);
cout<<"Multiplication is: ";
cout<<c1.mul()<<endl;
getch();
}
100420825160 Ankit Dixit
28 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 24: Write a program to demonstrate constructor overloading.
#include<iostream.h>
#include<conio.h>
class complex
{
int x,y;
public:
complex(int a)
{
x=a;
y=a;
}
complex(int a,int b)
{
x=a;
y=b;
}
void add(complex c1,complex c2)
{
x=c1.x-c2.x;
y=c1.x-c2.y;
}
void show()
{
cout<<x<<"+i"<<y<<endl;
}};
void main()
{
clrscr();
complex a(3,5);
complex b(2);
complex c(0);
c.add(a,b);
a.show();
b.show();
cout<<"a-b is: ";
c.show();
getch();
}
100420825160 Ankit Dixit
29 Submitted To: Ms. Achhardeep Kaur
100420825160 Ankit Dixit
30 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 25: Write a program to demonstrate copy constructor.
#include<iostream.h>
#include<conio.h>
class complex
{
int r,i;
public:
complex(int a,int b)
{
r=a;
i=b;
}
complex(complex&c)
{
r=c.r;
i=c.i;
}
void show()
{
cout<<r<<"+i"<<i<<endl;
}};
void main()
{
clrscr();
int x,y;
cout<<"Enter real part: ";
cin>>x;
cout<<"Enter imaginary part: ";
cin>>y;
complex c1(x,y);
complex c2(c1);
cout<<"First number is ";
c1.show();
cout<<"Second number is ";
c2.show();
getch();
}
100420825160 Ankit Dixit
31 Submitted To: Ms. Achhardeep Kaur
100420825160 Ankit Dixit
32 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 26 : Write a program to demonstrate destructor.
include<iostream.h>
#include<conio.h>
class sample
{
int a,b;
public:
~sample()
{
cout<<"This is destructor"<<endl;
a=100;
b=200;
}
int add()
{
return(a+b);
}};
void main()
{
clrscr();
sample s;
cout<<"Output is: "<<s.add()<<endl;
getch();
}
100420825160 Ankit Dixit
33 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 27: Write a program to demonstrate hybrid inheritance.
#include<iostream.h>
#include<conio.h>
class b1
{
protected:
int x;
public:
void assignx()
{
x=20;
}};
class d1:public b1
{
protected:
int y;
public:
void assigny()
{
y=40;
}};
class d2:public d1
{
protected:
int z;
public:
void assignz()
{
z=60;
}};
class b2
{
protected:
int k;
public:
void assignk()
{
k=80;
}};
class d3:public b2,public d2
{
private:
int total;
public:
100420825160 Ankit Dixit
34 Submitted To: Ms. Achhardeep Kaur
void output()
{
total=x+y+z+k;
cout<<"x+y+z+k= "<<total<<endl;
}};
void main()
{
clrscr();
d3 s;
s.assignx();
s.assigny();
s.assignz();
s.assignk();
s.output();
getch();
}
100420825160 Ankit Dixit
35 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 28: WRITE A PROGRAMwhich concatenates two strings by overloading
the binary operator ‘+’.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char str[80];
public:
string()
{
strcpy(str," ");
}
string(char s[])
{
strcpy(str,s);
}
void display()
{
cout<<str<<endl;
}
string operator+(string s4)
{
if((strlen(str)+strlen(s4.str))<80)
{
string temp;
strcpy(temp.str,str);
strcat(temp.str,s4.str);
return(temp);
}
else
cout<<"String overflow"<<endl;
}};
void main()
{
clrscr();
string s1("ECE");
string s2=("ROCKS");
string s3;
s3=s1+s2;
s3.display();
getch();
}
100420825160 Ankit Dixit
36 Submitted To: Ms. Achhardeep Kaur
100420825160 Ankit Dixit
37 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 29: Write a program to show function overloading.
#include<iostream.h>
#include<conio.h>
void sum(int,int);
void sum(int,int,int);
void sum(float,float);
void main()
{
clrscr();
int a,b,c;
float p,q;
cout<<"Enter integer values: ";
cin>>a>>b>>c;
cout<<"Enter float values: ";
cin>>p>>q;
sum(a,b,c);
sum(a,b);
sum(a,c);
sum(p,q);
getch();
}
void sum(int x,int y)
{
int z=x+y;
cout<<"sum 1= "<<z<<endl;
}
void sum(int x,int y,int z)
{
int s=x+y+z;
cout<<"sum 2= "<<s<<endl;
}
void sum(float x,float y)
{
float z=x+y;
cout<<"sum 3= "<<z<<endl;
}
100420825160 Ankit Dixit
38 Submitted To: Ms. Achhardeep Kaur
100420825160 Ankit Dixit
39 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 30: Write a program to demonstrate virtual functions.
#include<iostream.h>
#include<conio.h>
class b
{
public:
virtual void display()
{
cout<<"This is class b."<<endl;
}};
class d1:public b
{
public:
void display()
{
cout<<"This is class d1."<<endl;
}};
class d2:public b
{
public:
void display()
{
cout<<"This is class d2."<<endl;
}};
void main()
{
clrscr();
b*p;
d1 obj1;
d2 obj2;
b obj;
p=&obj;
p->display();
p=&obj1;
p->display();
p=&obj2;
p->display();
getch();
}
100420825160 Ankit Dixit
40 Submitted To: Ms. Achhardeep Kaur
100420825160 Ankit Dixit
41 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 31: Write a program to demonstrate friend function.
#include<iostream.h>
#include<conio.h>
class sample
{
int x;
int y;
public:
sample(int a,int b)
{
x=a;
y=b;
}
friend int add(sample s1);
};
int add(sample s1)
{
return(s1.x+s1.y);
}
void main()
{
clrscr();
sample s2(20,30);
int sum;
sum=add(s2);
cout<<"Sum of private data of object s2= ";
cout<<sum<<endl;
getch();
}
100420825160 Ankit Dixit
42 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 32: Write a program to read multiple line string using get function.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char str[80];
cout<<"Enter multiple lines. "<<endl;
cout<<"Press enter after each line."<<endl;
cout<<"For terminating program,enter $.";
cout<<"and then press enter. "<<endl;
cin.get(str,80,'$');
cout<<"Entered multiple lines: "<<endl;
cout<<str<<endl;
getch();
}
100420825160 Ankit Dixit
43 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 33: Write a program to demonstrate class template with constructor.
#include<iostream.h>
#include<conio.h>
template<class T>
class sample
{
private:T a,b;
public:sample (Ta1,Tb1)
{
a=a1;
b=b1;
}
T small()
{
if(a<b)
return(a);
else
return(b);
}};
void main()
{
sample<int>obj1(10,5);
cout<<"smaller no. is "<<obj1.small();
sample<float>obj2(10.5,20.5);
cout<<"smaller no. is "<<obj2.small();
getch();
}
100420825160 Ankit Dixit
44 Submitted To: Ms. Achhardeep Kaur
PRACTICAL 34: Write a program to show function template with multiple parameters.
#include<iostream.h>
#include<conio.h>
template<class T1,class T2>
void show(T1a,T2b)
{
cout<<a<<"t"<<b<<endl;
}
void main()
{
clrscr();
show(1234,"kimmy");
show(5678,"rimmy");
show(9109,9019);
getch();
}

Oops practical file

  • 1.
    100420825160 Ankit Dixit 1Submitted To: Ms. Achhardeep Kaur PRACTICAL 1 : Write a program to check whether a given number is even or odd. #include<iostream.h> #include<conio.h> void main() { clrscr(); int n; cout<<"Enter number: "; cin>>n; if(n%2==0) cout<<"Number is even"; else cout<<"Number is odd"; getch(); }
  • 2.
    100420825160 Ankit Dixit 2Submitted To: Ms. Achhardeep Kaur PRACTICAL 2: Write a program to find the greatest of three numbers using conditional operator. #include<iostream.h> #include<conio.h> void main() { clrscr(); int a,b,c; cout<<"Enter a: "; cin>>a; cout<<"Enter b: "; cin>>b; cout<<"Enter c: "; cin>>c; if(a>b) { if(a>c) cout<<"Largest is "<<a; else { if (b>c) cout<<"Largest is "<<b; }} else cout<<"Largest is "<<c; getch(); }
  • 3.
    100420825160 Ankit Dixit 3Submitted To: Ms. Achhardeep Kaur PRACTICAL 3 : Write a program to find the sum of digits of a number. #include<iostream.h> #include<conio.h> void main() { clrscr(); int n,num,sum=0; cout<<"n Enter the number: "; cin>>n; num=n; while(num>0) { sum+=num%10; num/=10; } cout<<"n The sum of digits of a number is "<<sum; getch(); }
  • 4.
    100420825160 Ankit Dixit 4Submitted To: Ms. Achhardeep Kaur PRACTICAL 4 : Write a program to generate Fibonacci series of first n numbers. #include<iostream.h> #include<conio.h> void main() { clrscr(); int n; int f1,f2,f3; cout<<"Enter how many number series you need: "; cin>>n; f1=1; f2=1; cout<<f1<<endl; cout<<f2<<endl; int c=2; while(c<n) { f3=f1+f2; f1=f2; f2=f3; cout<<f3<<endl; c++; } getch(); }
  • 5.
    100420825160 Ankit Dixit 5Submitted To: Ms. Achhardeep Kaur PRACTICAL 5: Write a program to print the multiplication table of a given number. #include<iostream.h> #include<conio.h> void main() { clrscr(); int n,i; cout<<"enter a no. for which you need a table"; cin>>n; for(i=1;i<=10;i++) cout<<n<<"*"<<i<<"="<<n*i<<endl; getch(); }
  • 6.
    100420825160 Ankit Dixit 6Submitted To: Ms. Achhardeep Kaur PRACTICAL 6 : Write a program to calculate the factorial of a number. #include<iostream.h> #include<conio.h> void main() { clrscr(); int i,num,fact=1; cout<<"Enter the number: "; cin>>num; i=num; while (num>0) { fact=fact*num; --num; } cout<<"Factorial of "<<i<<" is "<<fact; getch(); }
  • 7.
    100420825160 Ankit Dixit 7Submitted To: Ms. Achhardeep Kaur PRACTICAL 7 : Write a program to add, subtract, multiply & division o two numbers using switch statement. #include<iostream.h> #include<conio.h> void main() { clrscr(); int a,b; char opr; cout<<"Enter any two numbers: "; cin>>a>>b; cout<<"Choose any operator(+,-,*,/): "; cin>>opr; switch(opr) { case'+': int c=a+b; cout<<"The addition of two numbers is: "<<c; break; case'-': int d=a-b; cout<<"The subtraction of two numbers is: "<<d; break; case'*': int e=a*b; cout<<"The multiplication of two numbers is: "<<e; break; case'/': int f=a/b; cout<<"The division of two numbers is: "<<f; break; default: cout<<"Wrong operator"; } getch(); }
  • 8.
    100420825160 Ankit Dixit 8Submitted To: Ms. Achhardeep Kaur
  • 9.
    100420825160 Ankit Dixit 9Submitted To: Ms. Achhardeep Kaur PRACTICAL 8 : Write a program to check whether a given number is prime or not. #include<iostream.h> #include<conio.h> void main() { clrscr(); int num,i,c=0; cout<<"enter number: "; cin>>num; for(i=2;i<=num-1;i++) { if(num%i==0) c++; } if(c!=0) cout<<"it is not prime no."; else cout<<"it is prime no."; getch(); }
  • 10.
    100420825160 Ankit Dixit 10Submitted To: Ms. Achhardeep Kaur PRACTICAL 9 : Write a program to print the given pattern. #include<iostream.h> #include<conio.h> void main() { clrscr(); for(int i=1;i<6;i++) { for(int j=6-i;j>=0;j--) cout<<" "; for(int k=i;k>=2;k--) cout<<k; for(int l=1;l<=i;l++) cout<<l; cout<<"n"; } getch(); }
  • 11.
    100420825160 Ankit Dixit 11Submitted To: Ms. Achhardeep Kaur PRACTICAL 10: Write a program to demonstrate the working of call by value. #include<iostream.h> #include<conio.h> void main() { clrscr(); int x,y; x=10; y=20; void swap(int,int); swap(x,y); cout<<"x= "<<x<<endl; cout<<"y= "<<y; getch(); } void swap(int a, int b) { int t=a; a=b; b=t; }
  • 12.
    100420825160 Ankit Dixit 12Submitted To: Ms. Achhardeep Kaur PRACTICAL 11: Write a program to demonstrate the working of call by reference. #include<iostream.h> #include<conio.h> void main() { clrscr(); int x,y; x=10; y=20; void swap(int&, int&); swap(x,y); cout<<"x= "<<x<<endl; cout<<"y= "<<y; getch(); } void swap(int &a, int &b) { int t=a; a=b; b=t; }
  • 13.
    100420825160 Ankit Dixit 13Submitted To: Ms. Achhardeep Kaur PRACTICAL 12: Write a program to demonstrate the working of pointers. #include<iostream.h> #include<conio.h> void main() { clrscr(); int *p; int x; cout<<"Enter a value: "; cin>>x; p=&x; *p=*p+10; cout<<"Now x is: "<<x<<endl; cout<<"Value of x through p: "<<*p<<endl; cout<<"The address of x is: "<<&x<<endl; cout<<"The address of x is: "<<p<<endl; getch(); }
  • 14.
    100420825160 Ankit Dixit 14Submitted To: Ms. Achhardeep Kaur PRACTICAL 13: Write a program to demonstrate the use of inline functions. #include<iostream.h> #include<conio.h> inline double cube(double x) { return(x*x*x); } void main() { clrscr(); double a; cout<<"Enter a number: " cin>>a; cout<<"The cube of "<<a<<"is: " cout<<cube(a); getch(); }
  • 15.
    100420825160 Ankit Dixit 15Submitted To: Ms. Achhardeep Kaur PRACTICAL 14: Write a program to demonstrate the working of default arguments. #include<iostream.h> #include<conio.h> void main() { clrscr(); int sum(int=30,int=30); int a,b; cout<<"Enter value of a: "; cin>>a; cout<<"Enter value of b: "; cin>>b; sum(a,b); sum(a); sum(); getch(); } int sum(int x,int y) { int s=x+y; cout<<"sum= "<<s<<endl; return 0; }
  • 16.
    100420825160 Ankit Dixit 16Submitted To: Ms. Achhardeep Kaur PRACTICAL 15: Write a program to demonstrate the use of recursion. #include<iostream.h> #include<conio.h> void main() { clrscr(); int fact(int); int f,n; cout<<"Enter the number: "; cin>>n; f=fact(n); cout<<"FACTORIAL= "<<f; getch(); } int fact(int n) { int value=1; if(n==1) return(value); else { value=n*fact(n-1); return(value); } getch(); }
  • 17.
    100420825160 Ankit Dixit 17Submitted To: Ms. Achhardeep Kaur PRACTICAL 16: Write a program which reads two matrices and then print a matrix which is addition of these two. #include<iostream.h> #include<conio.h> void main() { clrscr(); int a[3][3],b[3][3],s[3][3]; cout<<"Enter first matrix elements: "<<endl; for(int i=0;i<3;i++) for(int j=0;j<3;j++) { cin>>a[i][j]; } cout<<"n Enter second matrix: "<<"nn"; for(i=0;i<3;i++) for(j=0;j<3;j++) { cin>>b[i][j]; } cout<<"n The first matrix is:"<<"nn"; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cout<<a[i][j]; cout<<"t"; } cout<<endl; } cout<<"n The second matrix is:"<<"nn"; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cout<<b[i][j]; cout<<"t"; } cout<<endl; } for(i=0;i<3;i++) for(j=0;j<3;j++) { s[i][j] = a[i][j] + b[i][j]; }
  • 18.
    100420825160 Ankit Dixit 18Submitted To: Ms. Achhardeep Kaur cout<<"n The addition of two matrices is: "<<"nn"; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cout<<s[i][j]<<'t'; } cout<<endl; } getch(); }
  • 19.
    100420825160 Ankit Dixit 19Submitted To: Ms. Achhardeep Kaur PRACTICAL 17: Write a program to read a string and then copy that string into another. #include<iostream.h> #include<conio.h> void main() { clrscr(); char a[80],b[80]; void string_copy(char b[],char a[]); cout<<"Enter a string: "<<endl; cin.get(a,80); string_copy (b,a); cout<<"First string is: "<<endl; cout<<a<<endl; cout<<"Second string is: "<<endl; cout<<b<<endl; getch(); } void string_copy (char b[],char a[]) { int i,j; i=0; while(a[i]!='0') { b[i]=a[i]; i++; } b[i]='0'; }
  • 20.
    100420825160 Ankit Dixit 20Submitted To: Ms. Achhardeep Kaur PRACTICAL 18: Write a program to demonstrate the working of structure within a structure. #include<iostream.h> #include<conio.h> struct date { int day; char month_name[20]; int year; }; struct biodata { char name[80]; date dob; }; void main() {clrscr(); biodata student; cout<<"Enter name: "<<endl; cin>>student.name; cout<<"ENTER DATE OF BIRTH:"<<endl; cout<<"Enter day: "; cin>>student.dob.day; cout<<"Enter month name: "; cin>>student.dob.month_name; cout<<"Enter year: "; cin>>student.dob.year; cout<<"The biodata is: "<<endl; cout<<"Name: "<<student.name<<endl; cout<<"Date of birth: "; cout<<student.dob.day<<"-"<<student.dob.month_name<<"-"; cout<<student.dob.year<<endl; getch(); }
  • 21.
    100420825160 Ankit Dixit 21Submitted To: Ms. Achhardeep Kaur
  • 22.
    100420825160 Ankit Dixit 22Submitted To: Ms. Achhardeep Kaur PRACTICAL 19: Write a program to calculate the area of rectangle using the concept of object class. #include<iostream.h> #include<conio.h> class area { public: int l,b,area; void getdata() { cout<<"Enter the length of the rectangle: "; cin>>l; cout<<"Enter the breadth of the rectangle: "; cin>>b; } void display() { area=l*b; cout<<"The area of rectangle is: "<<area; }}; void main() { clrscr(); area s1; s1.getdata(); s1.display(); getch(); }
  • 23.
    100420825160 Ankit Dixit 23Submitted To: Ms. Achhardeep Kaur PRACTICAL 20 : Write a program to demonstrate data hiding, encapsulation and abstraction. #include<iostream.h> #include<conio.h> void main() { clrscr(); class date { public: int day; int month; int year; }; class date today; today.day=12; today.month=1; today.year=1992; cout<<"Today's date is "<<today.day<<"/"; cout<<today.month<<"/"<<today.year<<endl; getch(); }
  • 24.
    100420825160 Ankit Dixit 24Submitted To: Ms. Achhardeep Kaur PRACTICAL 21 : Write a program to show the working of static data members and static member functions. #include<iostream.h> #include<conio.h> class alpha { private: static int x; public: alpha(); static void display() { cout<<"Content of x "; cout<<"after incremented by one= "<<x<<endl; }}; class beta { private: int y; public: void getdata() { cout<<"Enter a value for y= n"; cin>>y; } void display() { cout<<"Content of y= "<<this->y<<endl; }}; int alpha::x=10; alpha::alpha() { ++x; } void main() { clrscr(); alpha objx; beta objy; objy.getdata(); alpha::display(); objy.display(); getch(); }
  • 25.
    100420825160 Ankit Dixit 25Submitted To: Ms. Achhardeep Kaur
  • 26.
    100420825160 Ankit Dixit 26Submitted To: Ms. Achhardeep Kaur PRACTICAL 22 : Write a program to demonstrate the working of constructor. #include<iostream.h> #include<conio.h> class sample { int a,b; public: sample() { cout<<"This is constructor. "<<endl; a=100; b=200; } int add() { return(a+b); }}; void main() { clrscr(); sample s; cout<<"Output is: "<<s.add()<<endl; getch(); }
  • 27.
    100420825160 Ankit Dixit 27Submitted To: Ms. Achhardeep Kaur PRACTICAL 23: Write a program to show the working of parameterized constructor. #include<iostream.h> #include<conio.h> class student { int a,b; public: student(int a1,int b1) { a=a1; b=b1; } int mul() { return(a*b); }}; void main() { clrscr(); int i,j; cout<<"Enter first number: "; cin>>i; cout<<"Enter second number: "; cin>>j; student c1(i,j); cout<<"Multiplication is: "; cout<<c1.mul()<<endl; getch(); }
  • 28.
    100420825160 Ankit Dixit 28Submitted To: Ms. Achhardeep Kaur PRACTICAL 24: Write a program to demonstrate constructor overloading. #include<iostream.h> #include<conio.h> class complex { int x,y; public: complex(int a) { x=a; y=a; } complex(int a,int b) { x=a; y=b; } void add(complex c1,complex c2) { x=c1.x-c2.x; y=c1.x-c2.y; } void show() { cout<<x<<"+i"<<y<<endl; }}; void main() { clrscr(); complex a(3,5); complex b(2); complex c(0); c.add(a,b); a.show(); b.show(); cout<<"a-b is: "; c.show(); getch(); }
  • 29.
    100420825160 Ankit Dixit 29Submitted To: Ms. Achhardeep Kaur
  • 30.
    100420825160 Ankit Dixit 30Submitted To: Ms. Achhardeep Kaur PRACTICAL 25: Write a program to demonstrate copy constructor. #include<iostream.h> #include<conio.h> class complex { int r,i; public: complex(int a,int b) { r=a; i=b; } complex(complex&c) { r=c.r; i=c.i; } void show() { cout<<r<<"+i"<<i<<endl; }}; void main() { clrscr(); int x,y; cout<<"Enter real part: "; cin>>x; cout<<"Enter imaginary part: "; cin>>y; complex c1(x,y); complex c2(c1); cout<<"First number is "; c1.show(); cout<<"Second number is "; c2.show(); getch(); }
  • 31.
    100420825160 Ankit Dixit 31Submitted To: Ms. Achhardeep Kaur
  • 32.
    100420825160 Ankit Dixit 32Submitted To: Ms. Achhardeep Kaur PRACTICAL 26 : Write a program to demonstrate destructor. include<iostream.h> #include<conio.h> class sample { int a,b; public: ~sample() { cout<<"This is destructor"<<endl; a=100; b=200; } int add() { return(a+b); }}; void main() { clrscr(); sample s; cout<<"Output is: "<<s.add()<<endl; getch(); }
  • 33.
    100420825160 Ankit Dixit 33Submitted To: Ms. Achhardeep Kaur PRACTICAL 27: Write a program to demonstrate hybrid inheritance. #include<iostream.h> #include<conio.h> class b1 { protected: int x; public: void assignx() { x=20; }}; class d1:public b1 { protected: int y; public: void assigny() { y=40; }}; class d2:public d1 { protected: int z; public: void assignz() { z=60; }}; class b2 { protected: int k; public: void assignk() { k=80; }}; class d3:public b2,public d2 { private: int total; public:
  • 34.
    100420825160 Ankit Dixit 34Submitted To: Ms. Achhardeep Kaur void output() { total=x+y+z+k; cout<<"x+y+z+k= "<<total<<endl; }}; void main() { clrscr(); d3 s; s.assignx(); s.assigny(); s.assignz(); s.assignk(); s.output(); getch(); }
  • 35.
    100420825160 Ankit Dixit 35Submitted To: Ms. Achhardeep Kaur PRACTICAL 28: WRITE A PROGRAMwhich concatenates two strings by overloading the binary operator ‘+’. #include<iostream.h> #include<conio.h> #include<string.h> class string { char str[80]; public: string() { strcpy(str," "); } string(char s[]) { strcpy(str,s); } void display() { cout<<str<<endl; } string operator+(string s4) { if((strlen(str)+strlen(s4.str))<80) { string temp; strcpy(temp.str,str); strcat(temp.str,s4.str); return(temp); } else cout<<"String overflow"<<endl; }}; void main() { clrscr(); string s1("ECE"); string s2=("ROCKS"); string s3; s3=s1+s2; s3.display(); getch(); }
  • 36.
    100420825160 Ankit Dixit 36Submitted To: Ms. Achhardeep Kaur
  • 37.
    100420825160 Ankit Dixit 37Submitted To: Ms. Achhardeep Kaur PRACTICAL 29: Write a program to show function overloading. #include<iostream.h> #include<conio.h> void sum(int,int); void sum(int,int,int); void sum(float,float); void main() { clrscr(); int a,b,c; float p,q; cout<<"Enter integer values: "; cin>>a>>b>>c; cout<<"Enter float values: "; cin>>p>>q; sum(a,b,c); sum(a,b); sum(a,c); sum(p,q); getch(); } void sum(int x,int y) { int z=x+y; cout<<"sum 1= "<<z<<endl; } void sum(int x,int y,int z) { int s=x+y+z; cout<<"sum 2= "<<s<<endl; } void sum(float x,float y) { float z=x+y; cout<<"sum 3= "<<z<<endl; }
  • 38.
    100420825160 Ankit Dixit 38Submitted To: Ms. Achhardeep Kaur
  • 39.
    100420825160 Ankit Dixit 39Submitted To: Ms. Achhardeep Kaur PRACTICAL 30: Write a program to demonstrate virtual functions. #include<iostream.h> #include<conio.h> class b { public: virtual void display() { cout<<"This is class b."<<endl; }}; class d1:public b { public: void display() { cout<<"This is class d1."<<endl; }}; class d2:public b { public: void display() { cout<<"This is class d2."<<endl; }}; void main() { clrscr(); b*p; d1 obj1; d2 obj2; b obj; p=&obj; p->display(); p=&obj1; p->display(); p=&obj2; p->display(); getch(); }
  • 40.
    100420825160 Ankit Dixit 40Submitted To: Ms. Achhardeep Kaur
  • 41.
    100420825160 Ankit Dixit 41Submitted To: Ms. Achhardeep Kaur PRACTICAL 31: Write a program to demonstrate friend function. #include<iostream.h> #include<conio.h> class sample { int x; int y; public: sample(int a,int b) { x=a; y=b; } friend int add(sample s1); }; int add(sample s1) { return(s1.x+s1.y); } void main() { clrscr(); sample s2(20,30); int sum; sum=add(s2); cout<<"Sum of private data of object s2= "; cout<<sum<<endl; getch(); }
  • 42.
    100420825160 Ankit Dixit 42Submitted To: Ms. Achhardeep Kaur PRACTICAL 32: Write a program to read multiple line string using get function. #include<iostream.h> #include<conio.h> void main() { clrscr(); char str[80]; cout<<"Enter multiple lines. "<<endl; cout<<"Press enter after each line."<<endl; cout<<"For terminating program,enter $."; cout<<"and then press enter. "<<endl; cin.get(str,80,'$'); cout<<"Entered multiple lines: "<<endl; cout<<str<<endl; getch(); }
  • 43.
    100420825160 Ankit Dixit 43Submitted To: Ms. Achhardeep Kaur PRACTICAL 33: Write a program to demonstrate class template with constructor. #include<iostream.h> #include<conio.h> template<class T> class sample { private:T a,b; public:sample (Ta1,Tb1) { a=a1; b=b1; } T small() { if(a<b) return(a); else return(b); }}; void main() { sample<int>obj1(10,5); cout<<"smaller no. is "<<obj1.small(); sample<float>obj2(10.5,20.5); cout<<"smaller no. is "<<obj2.small(); getch(); }
  • 44.
    100420825160 Ankit Dixit 44Submitted To: Ms. Achhardeep Kaur PRACTICAL 34: Write a program to show function template with multiple parameters. #include<iostream.h> #include<conio.h> template<class T1,class T2> void show(T1a,T2b) { cout<<a<<"t"<<b<<endl; } void main() { clrscr(); show(1234,"kimmy"); show(5678,"rimmy"); show(9109,9019); getch(); }