1. 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();
}
2. 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();
}
3. 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();
}
4. 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();
}
5. 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();
}
6. 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();
}
7. 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();
}
9. 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();
}
10. 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();
}
11. 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;
}
12. 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;
}
13. 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();
}
14. 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();
}
15. 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;
}
16. 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();
}
17. 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];
}
18. 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();
}
19. 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';
}
20. 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();
}
22. 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();
}
23. 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();
}
24. 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();
}
26. 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();
}
27. 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();
}
28. 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();
}
39. 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();
}
41. 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();
}
42. 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();
}
43. 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();
}
44. 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();
}