Professional Documents
Culture Documents
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.
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();
r = n % 10;
rev = rev * 10 + r;
n = n / 10;
}
cout<<endl<<"Reverse of a given number is :
"<<rev; getch();
}
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();
}