You are on page 1of 21

CPP LAB MANUAL

Part A
Write a C++ program to calculate area and circumference of circle.
Write a C++ program to find the largest of 3 numbers.
Write a program to illustrate logical operators.
Write a program to perform addition, subtraction, multiplication and division using switch case.
Write a program to calculate the sum of odd & even numbers in any given array.
Write a program to print the following pattern using nested for loop.

Write a program to reverse a given number.


Write a program to find the sum of N natural numbers.
Write a program to illustrate call by value and call by address.
Write a program to illustrate inline function.
Write a program to demonstrate the use of structure.
Part B
Write a program to generate a bill. The bill contents serial no, description, quantity, rate and amount.
Write a program to find the factorial of a number using function overloading.
Write a program to demonstrate the overloaded constructor and destructor.
Write a program to overload binary + to concatenate two strings.
Writer a program for date validation.
Write a program to print the sum of two complex numbers using friend function.
Write a program to add two matrices by overloading binary + operator.
Write a program to demonstrate the multilevel inheritance.
Write a program to demonstrate static data member and static member function.
WAP to demonstrate nested member function.

1. Write a C++ program to calculate area and circumference of circle.

include <iostream.h>

SSCASCW,TUMKUR Page 1
include <conio.h> const
float PI = 3.142;
void main()
{
float r,area,cir;
clrscr();
cout<<endl<<"Enter radius of a circle
"; cin>>r;
area = PI * r * r; cir = 2 * PI * r;
cout<<endl<<"Area of Circle = "
<<area;
cout<<endl<<"Circumference of Circle =
"<<cir; getch();
}
2. // Write a C++ program to find the largest of 3 numbers.
#include <iostream.h>
include <conio.h>
void main()
{
int a,b,c,lar; clrscr();
cout<<endl<<"Enter 3 integers
"; cin >>a>>b>>c;

if ((a>b)&&(a>c))
lar = a;
else if ( (b>c)&&(b>a))
lar = b;
else
lar = c;
cout<<endl<<"Largest of three number
is :"<<lar; getch();
}

SSCASCW,TUMKUR Page 2
~~~ C++ Lab Manial ~~~
3. // Write a program to illustrate logical operators
include <iostream.h>
include <conio.h>
void main()
{
int a=5,b=10,c=6;
clrscr();
cout<<endl<<" And operator ="<<(a>b)&&(a>c);
cout<<endl<<" OR operator = "<<(a>b)||(a>c);
cout<<endl<<" NOT operator = "<< !(a>b);
getch();
}
// Write a program to perform addition, subtraction, multiplication and division
using switch case
include<iostream.h>
include <conio.h>
void main()
{
int a,b;
float res;
char opr;
clrscr();
cout<<endl<<"Enter two
numbers :"; cin>>a>>b;
cout<<endl<<"Enter operator:
"; cin >>opr;
switch(opr)
{
case '+' : cout<<endl<<"Result = "<<a+b;break;
case '-' : cout<<endl<<"Result = "<<a-b;break;
case '*' : cout<<endl<<"Result = "<<a*b;break;
case '/' : if (b!=0)
cout<<"Result = "<<(float)a/b;
else
cout<<"Divide by Zero Error";
break;
default : cout<<"Invalid operator";
}
getch();
}

SSCASCW,TUMKUR Page 3
~~~ C++ Lab Manial ~~~
5. // Write a program to calculate the sum of odd & even numbers in any given array
include <iostream.h>
include <conio.h>
void main()
{
int a[100],n,i,osum,esum;
clrscr(); cout<<endl<<"Enter
n value: "; cin >>n;

cout<<endl<<"Enter array
elements :"; for(i=0;i<n;i++)
cin>>a[i];
osum=esum=0;
for(i=0;i<n;i++)
{
if (a[i]%2 ==0)
esum = esum + a[i];
else
osum = osum + a[i];
}
cout<<endl<<"Odd sum = "<<osum;
cout<<endl<<"Even sum = "<<esum;
getch();
}
6. // Write a program to print the following pattern using nested for loop.
include <iostream.h>
include <conio.h>
void main()
{
int i,j,k,b,n; clrscr();
cout<<endl<<"Enter n
value :"; cin >>n;
for(i=1;i<=n;i++)

{
for(b=1;b<=(n-i);b++)
cout<<" ";
for(j=1;j<=i;j++)
cout<<"*";
for(k=j-2;k>=1;k--)
cout<<"*";
cout<<endl;
}
getch();
}
~~~ C++ Lab Manial ~~~
7. // Write a program to reverse a given number.
# include <iostream.h>
# include <conio.h>

void main()
{
int n,r;
long int rev=0;
clrscr();

cout<<endl<<" Enter an integer: ";


cin>>n;
while(n!=0)
{

r = n % 10;
rev = rev * 10 + r;
n = n / 10;
}
cout<<endl<<"Reverse of a given number is :
"<<rev; getch();
}

8. // WAP to find the sum of N natural number


include <iostream.h>
include <conio.h>
void main()
{
int n,i,sum=0;
clrscr();

cout<<endl<<"Enter n value :";


cin>>n;
for(i=1; i<=n; i++)
sum = sum + i;
cout<<endl<<" Sum of N Natural Number is :
"<<sum; getch();
}
~~~ C++ Lab Manial ~~~
9. //Write a program to illustrate call by value and call by address.
include <iostream.h>
include <conio.h>
include <iomanip.h>
void callbyvalue(int m, int n);
void callbyaddress(int *p, int *q);
void main()
{
int a=10,b=20;
clrscr();
cout<<endl<<"Call by Value ";
cout<<endl<<"Before inter change
a="<<a<<setw(5)<<"b="<<b; callbyvalue(a,b);
cout<<endl<<"After inter change a="<<a<<setw(5)<<"b="<<b;
cout<<endl<<"Call by Address ";
cout<<endl<<"Before inter change
a="<<a<<setw(5)<<"b="<<b; callbyaddress(&a,&b);
cout<<endl<<"After inter change
a="<<a<<setw(5)<<"b="<<b; getch();
}
void callbyvalue(int m, int n)
{
int t;
t= m;
m=n;
n=t;
}
void callbyaddress(int *p, int *q)
10. // Write a program to illustrate inline function.
include <iostream.h>
include <conio.h> inline
void check(int n)
{
if (n%2==0)
~~~ C++ Lab Manial ~~~
cout<<"Even Number";
else
cout<<"Odd Number";
}
void main()
{
int n;
clrscr();

11. // Write a program to demonstrate the use of structure.


include<iostream.h>
include <conio.h>
include <iomanip.h>
void main()
{
struct student
{
int rno;
char name[20];
int sem;
};
struct student
p; clrscr();
cout<<endl<<"Enter student rno and
name:"; cin>>p.rno>>p.name;
cout<<endl<<"Enter student semester:";
cin>>p.sem;
cout<<endl<<"Student details is ";
cout<<endl<<"Rno Name Sem";
cout<<endl<<p.rno<<setw(10)<<p.name<<setw(10)<<p.sem;
getch();
~~~ C++ Lab Manial ~~~
Part B
Write a program to generate a bill. The bill contains serial no, description,
quantity, rate/unit and amount.
include <iostream.h>
include <conio.h>
include <iomanip.h>
class Bill
{
private :
int slno,qty;
char desc[20];
float rate,amt;
public:
void getdata(int i)
{
slno=i;
cout<<endl<<"Enter item name: ";
cin>>desc;
cout<<endl<<"Enter rate and quantity:";
cin>>rate>>qty;
amt = qty * rate;
}
float generate()
{
cout<<endl<<slno<<setw(15)<<desc<<setw(8)<<rate<<setw(10)<<qty<<setw(10)<<amt;
return(amt);
}
};
void main()
{
int i,n;
char ch;
float totamt=0;
Bill p[10];
clrscr();
i=1;
do
{
p[i].getdata(i);
cout<<endl<<"Do U Want to Add another Item [y/n] ";
cin>>ch;
i++;
}while(ch=='y');
n=i;
clrscr();
cout<<endl<<setw(10)<<"XYZ Ltd.";
cout<<endl<<setw(10)<<"Cash Invoice";
cout<<endl<<"=================================================";
cout<<endl<<"Sl.No. Name Rate Qty Total";
cout<<endl<<"=================================================";
i=1;
while(i<n)
{
totamt = totamt + p[i].generate();
i++;
}
cout<<endl<<"=================================================";
cout<<endl<<" TOTAL : "<<totamt;
cout<<endl<<"================================================="
; getch();
}
~~~ C++ Lab Manial ~~~
2. Write a program to find the factorial of a number using function overloading.
include <iostream.h>
include <conio.h>
int factorial();
long int factorial(int
n); void main()
{
int n; clrscr();
cout<<endl<<"Enter an integer
"; cin >>n;

if( (n==0) || (n==1) )


cout<<endl<<"Factorial value = "<<factorial();
else
cout<<endl<<"Factorial value =
"<<factorial(n); getch();
}
int factorial()
{
return(1);
}
long int factorial(int n)
{
int i;
long int f=1;
for(i=1;i<=n;i++)
f = f*i;
return(f);
}
3. Write a program to demonstrate the overloaded constructor and destructor.
include <iostream.h>
include <conio.h>
include <string.h>
class student
{
private:
int rno;
char name[20];
float fee;
public:
~~~ C++ Lab Manial ~~~
student()
{
rno = 100;
strcpy(name,"Dhanya");
fee = 3280.50;
}
student(int n, char st[],float amt)
{
rno = n;
strcpy(name, st);
fee = amt;
}
void putdata()
{
cout<<endl<<"Student Roll No : "<<rno;
cout<<endl<<"Student Name : "<<name;
cout<<endl<<" Fee : "<<fee;
}
student()
{
cout<<endl<<"Destructor executed";
}
};
void main()
{
student p;
student q(200,"Niyatha",4500.90);
clrscr();
cout<<endl<<"Default Construtor Details :";
p.putdata();
cout<<endl<<"Overloaded Construtor Details : ";
q.putdata();
getch();
}
~~~ C++ Lab Manial ~~~
4. Write a program to overload binary + to concatenate two strings.
include<iostream.h>
include <conio.h>
include <string.h>
class string
{
private :
char
str[80]; public :
string()
{
strcpy(str,NULL);
}
void getdata()
{
cout<<endl<<"Enter a string = ";
cin>>str;
}
void putdata()
{
cout<<endl<<str;
}
string operator +(string q)
{
string t;
strcpy(t.str, str);
strcat(t.str, q.str);
return(t);
}
};
void main()
{
string p,q,r;
clrscr();
p.getdata();
q.getdata();
r = p + q;
cout<<endl<<"Resultant String is : ";
r.putdata();
getch();
}
~~~ C++ Lab Manial ~~~
5. Writer a program for date validation
include<iostream.h>
include<conio.h>
class date
{
private:
int
d,m,y; public:
void getdate()
{
cout<<endl<<"Enter day , month and year
="; cin>>d>>m>>y;
}
void putdate()
{
cout<<d<<"-"<<m<<"-"<<y;
}
int valid()
{
if((d<1)||(d>31)||(m<1)||(m>12)||(y<1))
return(0);
else if ( ((m==4)||(m==6)||(m==9)||(m==11)) && (d>30) )
return(0);
else if( (m==2)&&(y%4!=0)&&(d>29))
return(0);
else if( (m==2)&&(y%4!=0)&&(d>28))
return(0);
else
return(1);
}
};
void main()
{
clrscr();

date p;
p.getdate();
p.putdate();
int k = p.valid();
if(k==1)
cout<<endl<<"Date is VALID";
else
cout<<endl<<"Date is INVALID";
getch();
}
~~~ C++ Lab Manial ~~~
6. Write a program to print the sum of two complex numbers using friend function.
include <iostream.h>
include <conio.h>
class complex
{
private:
int real,
img; public:
void getdata()
{
cout<<endl<<"Enter real and imginary part
="; cin>>real>>img;
}
void putdata()
{
if (img>=0)
cout<<endl<<real<<"+i "<<img;
else
cout<<endl<<real<<"-i "<<img;
}
friend complex sum(complex p, complex q)
{
complex t;
t.real = p.real + q.real;
t.img = p.img + q.img;
return(t);
}
};
void main()
{
clrscr();
complex p,q,r;
p.getdata();
q.getdata();
r = sum(p,q);
r.putdata();
getch();
}
~~~ C++ Lab Manial ~~~
7. Write a program to add two matrices by overloading binary + operator.
include<iostream.h>
include<conio.h>
include <iomanip.h>
class matrix
{
private:
int m,n,A[10]
[10]; public:
int i,j;
martrix()
{
m = n= 0;
}
void getorder()
{
cout<<endl<<"Enter matrix order ";
cin>>m>>n;
cout<<m<<n;
}
void getmatrix()
{
cout<<endl<<"Enter matrtix elements = ";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>A[i][j];
cout<<A[i][j];
}
void putmatrix()
{
cout<<endl<<"Matrix form is :"<<endl<<setw(5);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
~~~ C++ Lab Manial ~~~
8. Write a program to demonstrate the multilevel inheritance.
include <iostream.h>
include <conio.h>
class company
{
private:
char cname[20];
char caddr[30];
public :
void getdata()
{
cout<<endl<<"Enter Company Name &
Address:"; cin>>cname>>caddr;
}
void putdata()
{
cout<<endl<<"Company Name : "<<cname;
cout<<endl<<"Company Address : "<<caddr;
}
};
class furniture : public company
{
private:
char mtype[20];
char color[10];
public:
void getdata()
{
company :: getdata();
cout<<endl<<"Enter material type and color:
"; cin >>mtype>>color;
}
void putdata()
{
company :: putdata();
cout<<endl<<"Material type= "<<mtype;
cout<<endl<<"Color = "<<color;
}
};
class dimesion : public furniture
{
private :
float h,w;
public :
void getdata()
{
~~~ C++ Lab Manial ~~~
furniture :: getdata();
cout<<endl<<"Enter height and width :";
cin>>h>>w;
}
void putdata()
{
furniture :: putdata();
cout<<endl<<"Height = "<<h;
cout<<endl<<"Width = "<<w;
}
};
void main()
{
clrscr();
dimesion chair;
cout<<endl<<" Enter Chair Details :";
hair.getdata();
chair.putdata();
getch();
}
9. Write a program to demonstrate static data member and static member function.
include <iostream.h>
include <conio.h>
class Date
{
private:
int d,m,y;
static char sep;
public:
Date()
{
d=28;
m=9;
y=2016;
}
~~~ C++ Lab Manial ~~~
void putdata()
{
cout<<endl<<d<<sep<<m<<sep<<y;
}
static void changesep()
{
cout<<endl<<"Enter new Separator :";
cin>>sep;
}
};
char Date :: sep ='-';
void main()
{
clrscr();
Date p;
p.putdata();
Date :: changesep();
p.putdata();
getch();
}
10. WAP to demonstrate nested member function.
include <iostream.h>
include<conio.h>
class circle
{
private: float r,area;
public:
void getdata()
{
cout<<endl<<"Enter radius: ";
cin>>r;
findarea();
}
void findarea()
{
area = 3.142*r*r;
cout<<endl<<"Area of cicle = "<<area;
}
};
void main()
{
clrscr();
circle p;
p.getdata();
getch();
}

You might also like