C++ Lab Manual
C++ Lab Manual
PROGRAMMING IN C++
LAB MANUAL
38 5
1. Program to test arithmetic operators.
Program:-
#include<iostream.h>
#include<conio.h>
Void main()
{
int num1,num2;
clrscr();
cout<<”Enter two numbers to be operated with arithmetic operators:”;
cin>>num1>>num2;
cout<<endl;
cout<<”Num1+Num2=”<<num1+num2<<endl;
cout<<”Num1*Num2=”<<num1*num2<<endl;
cout<<”Num1-Num2=”<<num1-num2<<endl;
if(num2!=0)
cout<<”Num1/Num2=”<<num1/num2<<endl;
else
cout<<”num2 is not non-zer. Division is not defined.”<<endl;
getch();
}
Output
38 5
2. Program to swap two numbers.
Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,temp;
clrscr();
cout<<"enter two number to swap";
cin>>a>>b;
cout<<"\n Before swapping"<<endl;
cout<<"\n a is="<<a<<"\n b is="<<b<<endl;
temp=a;
a=b;
b=temp;
cout<<"\n After swapping"<<endl;
cout<<"\n a is="<<a<<"\n b is="<<b<<endl;
getch();
}
Output
38 5
3. Program to demonstrate switch statement.
getch();
}
38 5
Output
Enter month number 12
Monthname = December
38 5
4. Program to find roots of quadratic equations.
38 5
5. Program to check whether the given number is palindrome or not
Output
38 5
6. Program to convert binary number to decimal number.
Output
38 5
7. Program to print the following format.
1
2 3
4 5 6
7 8 9 10
Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int rows,i,j,r,k=0;
clrscr();
cout<<"enter the number of rows";
cin>>rows;
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;++j)
cout<<k+j<<" ";
++k;
cout<<endl;
}
getch();
}
Output
1
2 3
4 5 6
7 8 9 10
38 5
8. Program to search an element in a given list.
Output
Enter no of elements 5
11 22 33 44 55
Enter the elements to be searched 44
Number found at the location 4
38 5
9. Program to perform addition of two matrices.
38 5
Output
38 5
10. Program to perform multiplication of two matrices.
mult[i][j]=a[i][k]*b[k][j];
//Displaying the result
cout<<"Multiplication of two matrix is"<<endl;
for(i=0;i<r1;i++)
38 5
for(j=0;j<c2;j++)
{
cout<<mult[i][j]<<" ";
if(j==c2-1)
cout<<endl;
}
getch();
}
OUTPUT
38 5
11. Program to find factorial of a given number using recursion.
Output
38 5
12. Program to demonstrate pointer arithmetic.
Output
Enter number 1 or 2
1
Addition of pointer
New value of ptr : 0x8f731392
38 5
13. Program to demonstrate call by value, call by address and call by reference.
Program:
// Using call by value.
#include<iostream.h>
#include<conio.h>
void interchange(int number1,int number2);
void main()
{
int num1=50,num2=70;
clrscr();
interchange(num1,num2);
cout<<"\n Number1:"<<num1<<"\n Number2:"<<num2;
getch();
}
void interchange(int number1,int number2)
{
int temp;
temp=number1;
number1=number2;
number2=temp;
}
Output
Number1=50
Number2=70
38 5
// Using call by reference
Program:
#include<iostream.h>
#include<conio.h>
void interchange(int *num1,int *num2);
void main()
{
int num1=50,num2=70;
clrscr();
interchange(&num1,&num2);
cout<<"\n Number1:"<<num1<<"\n Number2:"<<num2;
getch();
}
void interchange(int *num1,int *num2)
{
int temp;
temp=*num1;
*num1=*num2;
*num2=temp;
}
Output
Number1: 70
Number2: 50
38 5
14. Program to demonstrate structure data type.
Program:
#include<iostream.h>
#include<conio.h>
struct Person
{
char name[50];
int age;
float salary;
};
void main()
{
Person p1;
clrscr();
cout<<"enter full name";
cin.get(p1.name,50);
cout<<"\n enter age";
cin>>p1.age;
cout<<"\n enter salary";
cin>>p1.salary;
cout<<"\n Displaying Information"<<endl;
cout<<"\n Name:="<<p1.name;
cout<<"\n Age:="<<p1.age;
cout<<"\n Salary:="<<p1.salary;
getch();
}
Output
38 5
15. Program to demonstrate enumeration.
Program:
#include<iostream.h>
#include<conio.h>
Enum Seasons {spring, summer, autumn, winter};
Void main()
{
Seasons S;
S=spring;
Cout<<”spring;=”<<s;
S=summer;
Cout<<”summer:=”<<s;
S=autumn;
Cout<<”autumn:=”<<s;
S=winter;
Cout<<”winter:=”<<s;
Getch();
}
Output
Spring = 0
Summer= 1
Autumn = 2
Winter = 3
38 5
10. Program to perform multiplication of two matrices.
mult[i][j]=a[i][k]*b[k][j];
//Displaying the result
cout<<"Multiplication of two matrix is"<<endl;
for(i=0;i<r1;i++)
38 5
for(j=0;j<c2;j++)
{
cout<<mult[i][j]<<" ";
if(j==c2-1)
cout<<endl;
}
getch();
}
OUTPUT
38 5
38 5
Output
38 5
12. Program to demonstrate pointer arithmetic.
Output
Enter number 1 or 2
1
Addition of pointer
New value of ptr : 0x8f731392
38 5
13. Program to demonstrate call by value, call by address and call by reference.
Program:
// Using call by value.
#include<iostream.h>
#include<conio.h>
void interchange(int number1,int number2);
void main()
{
int num1=50,num2=70;
clrscr();
interchange(num1,num2);
cout<<"\n Number1:"<<num1<<"\n Number2:"<<num2;
getch();
}
void interchange(int number1,int number2)
{
int temp;
temp=number1;
number1=number2;
number2=temp;
}
Output
Number1=50
Number2=70
38 5
38 5
// Using call by reference
Program:
#include<iostream.h>
#include<conio.h>
void interchange(int *num1,int *num2);
void main()
{
int num1=50,num2=70;
clrscr();
interchange(&num1,&num2);
cout<<"\n Number1:"<<num1<<"\n Number2:"<<num2;
getch();
}
void interchange(int *num1,int *num2)
{
int temp;
temp=*num1;
*num1=*num2;
*num2=temp;
}
Output
Number1: 70
Number2: 50
38 5
14. Program to demonstrate structure data type.
Program:
#include<iostream.h>
#include<conio.h>
struct Person
{
char name[50];
int age;
float salary; 38 5
};
void main()
{
Person p1;
clrscr();
cout<<"enter full name";
cin.get(p1.name,50);
cout<<"\n enter age";
cin>>p1.age;
cout<<"\n enter salary";
cin>>p1.salary;
cout<<"\n Displaying Information"<<endl;
cout<<"\n Name:="<<p1.name;
cout<<"\n Age:="<<p1.age;
cout<<"\n Salary:="<<p1.salary;
getch();
}
Output
38 5
15. Program to demonstrate enumeration.
Program:
#include<iostream.h>
#include<conio.h>
Enum Seasons {spring, summer, autumn, winter};
Void main()
{
Seasons S;
S=spring;
Cout<<”spring;=”<<s;
S=summer;
Cout<<”summer:=”<<s;
S=autumn;
Cout<<”autumn:=”<<s;
S=winter;
Cout<<”winter:=”<<s;
Getch();
}
Output
Spring = 0
Summer= 1 38 5
Autumn = 2
Winter = 3
Winter 3
38 5
16. Program to demonstrate inline functions.
Program:
#include<iostream.h>
#include<conio.h>
class line
{
public:
inline float mul(float x, float y)
{
return(x*y);
}
inline float cube(float x)
{
return(x*x*x);
}
};
void main()
{
line obj;
float val1,val2;
clrscr();
cout<<"Enter two values";
cin>>val1>>val2;
cout<<"\n Multiplication value is:"<<obj.mul(val1,val2);
cout<<"\n cube value is:"<<obj.cube(val1)<<"\t"<<obj.cube(val2);
getch();
}
Output
Program:
#include<iostream.h>
38 5
#include<conio.h>
#include<stdlib.h>
#define pi 3.14
class fn
{
public:
void area(int); //circle
void area(int,int); //rectangle
void area(float,int,int); //triangle
};
void fn::area(int a)
{
cout<<"Area of circle"<<pi*a*a;
}
void fn::area(int a, int b)
{
cout<<"Area of rectangle "<<a*b;
}
void fn::area(float t,int a , int b)
{
cout<<"Area of triangle"<<t*a*b;
}
void main()
{
int ch,a,b,r;
clrscr();
fn obj;
cout<<"\n function overloading";
cout<<"\n 1. Area of circle \n 2. Area of rectangle \n 3. Area of triangle\n 4.exit";
cout<<"\n Enter your choice";
cin>>ch;
switch(ch)
{
case 1: cout<<"enter radious of the circle";
cin>>r;
obj.area(r);
break;
case 2: cout<<"enter sides of the rectangle";
cin>>a>>b;
obj.area(a,b);
break;
case 3: cout<<"enter sides of the triangle";
38 5
cin>>a>>b;
obj.area(0.5,a,b);
break;
case 4: exit(0);
}
getch();
}
Output
Function overloading
1.Area of circle
2. Area of rectangle
3. Area of triangle
4.exit
Enter your choice
2
Enter sides of the rectangle 38 5
5 5
Area of rectangle is : 25
Area of rectangle is : 25
38 5
18. Program to demonstrate class content.
Program:
#include<iostream.h>
#include<conio.h>
class stud
{
public:
char name[30],cname[10];
int rol,age;
void enter()
{
cout<<"enter student name:";
cin>>name;
cout<<"enter student age:";
cin>>age;
cout<<"enter student roll number";
cin>>rol;
cout<<"enter student student course";
cin>>cname;
}
void display()
{
cout<<"\n Age \t Name \t R.no \t course";
cout<<"\n"<<age<<"\t"<<name<<"\t"<<rol<<"\t"<<cname;
}
};
void main()
{
stud s;
clrscr();
s.enter();
s.display();
38 5
getch();
}
29. Program for Multiple and Hybrid Inheritance.
Program:
#include<iostream.h>
#include<conio.h>
class Stu
{
protected:
int rno;
public:
void get_no(int a)
{
rno=a;
}
void put_no(void)
{
cout<<"Roll no"<<rno<<"\n";
}
};
class test:public Stu
{
protected:
float part1,part2;
public:
void get_marks(float x,float y)
{
part1=x;
part2=y;
}
void put_marks()
{
cout<<"Marks obtained:"<<"part1="<<part1<<"\n"<<"part2="<<part2<<"\n";
}
};
class sports
{
protected:
float score;
public:
void getscore(float s)
{
score=s;
}
void putscore(void)
{
cout<<"sports"<<score<<"\n";
38 5
}
};
class result:public test,public sports
{
float total;
public:
void display(void);
};
void result::display(void)
{
total=part1+part2+score;
put_no();
put_marks();
putscore();
cout<<"Total score="<<total<<"\n";
}
void main()
{
result stu;
clrscr();
stu.get_no(123);
stu.get_marks(27.5,33.0);
stu.getscore(6.0);
stu.display();
getch();
}
Output
38 5
30. Program to demonstrate pure virtual function implementation.
Program:
#include<iostream.h>
#include<conio.h>
class base
{
private:
int x;
float y;
public:
virtual void getdata();
virtual void display();
};
class der:public base
{
private:
int roll;
char name[20];
public:
void getdata();
void display();
};
void base::getdata()
{
}
void base::display()
{
}
void der::getdata()
{
cout<<"Enter roll of the student";
cin>>roll;
cout<<"Enter name of the student";
cin>>name;
}
void der::display()
{
cout<<"Name is:"<<name;
cout<<"\n Roll no is"<<roll;
}
void main()
{
base *ptr;
der obj;
clrscr();
38 5
ptr=&obj;
ptr->getdata();
ptr->display();
getch();
}
Output
38 5