Вы находитесь на странице: 1из 21

PRACTICAL - 1

Aim :- Raising a number n to a power p is the same as multiplying n by itself p


times. Write a function called power ( ) that takes a double value for n and an int
value for p, and returns the result as double value. Use a default argument of 2 for
p, so that if this argument is omitted, the number will be squared. Write a main ( )
function that gets values from the user to test this function.

Program :-
#include<iostream.h>
#include<conio.h>
#include<math.h>
double power(double a,int b=2)
{
double x;
x=pow(a,b);
return x;
}
void main()
{
clrscr();
double r1,r2,n;
int p;
cout<<"Enter the Value For N & P :- ";
cin>>n>>p;
r1=power(n,p);
r2=power(n); // For Default Argument.
cout<<"Result= "<<r1;
cout<<"\nDefault Argument= "<<r2;
getch();
}
OUTPUT:-
PRACTICAL - 2
Aim :- A point on the two dimensional plane can be represented by two numbers: an X
coordinate and a Y coordinate. For example, (4,5) represents a point 4 units to the right of
the origin along the X axis and 5 units up the Y axis. The sum of two points can be defined
as a new point whose X coordinate is the sum of the X coordinates of the points and whose
Y coordinate is the sum of their Y coordinates. Write a program that uses a structure called
point to model a point. Define three points, and have the user input values to two of them.
Then set the third point equal to the sum of the other two, and display the value of the new
point. Interaction with the program might look like this:
Enter coordinates for P1: 3 4
Enter coordinates for P2: 5 7
Coordinates of P1 + P2 are : 8, 11

Program :-
#include<iostream.h>
#include<conio.h>
struct point
{
int x,y;
}p1,p2,p3;
void main()
{
clrscr();
cout<<"ENTER THE VALUE OF p1.(x):- ";
cin>>p1.x;
cout<<"ENTER THE VALUE OF p2.(x):- ";
cin>>p2.x;
cout<<"ENTER THE VALUE OF p1.(y):- ";
cin>>p1.y;
cout<<"ENTER THE VALUE OF p2.(y):- ";
cin>>p2.y;
p3.x=p1.x+p2.x;
p3.y=p1.y+p2.y;
cout<<"The value of P3(x)= "<<p3.x;
cout<<"\nThe value of P3(y)= "<<p3.y;
getch();
}

OUTPUT:-
PRACTICAL - 3
Aim :- Create the equivalent of a four function calculator. The program should request
the user to enter a number, an operator, and another number. It should then carry out the
specified arithmetical operation: adding, subtracting, multiplying, or dividing the two
numbers. (It should use a switch statement to select the operation). Finally it should display
the result. When it finishes the calculation, the program should ask if the user wants to do
another calculation. The response can be Y or N. Some sample interaction with the
program might look like this.
Enter first number, operator, and second number: 10/ 3
Answer = 3.333333
Do another (Y/ N)? Y
Enter first number, operator, second number 12 + 100
Answer = 112
Do another (Y/ N) ? N

Program :-
#include<iostream.h>
#include<conio.h>
void main()
{
float a,b,c;
char ch,d;
clrscr();
do{ cout<<"Enter First number,operator,second number :=";
cin>>a>>ch>>b;
switch(ch)
{
case '+': { c=a+b;
cout<<"Answer = "<<c;
break; }
case '-': { c=a-b;
cout<<"Answer = "<<c;
break; }
case '*': { c=a*b;
cout<<"Answer = "<<c;
break; }
case '/': { c=a/b;
cout<<"Answer = "<<c;
break; }
default: cout<<"WRONG KEYWORD ENTERED";
}
cout<<"\nDo another(Y/N)? :- ";
cin>>d;
}while(d=='Y'||d==y);
getch();
}

OUTPUT:-
PRACTICAL 4
Aim :- A phone number, such as (212) 767-8900, can be thought of as having three parts:
the area code (212), the exchange (767) and the number (8900). Write a program that uses a
structure to store these three parts of a phone number separately. Call the structure phone.
Create two structure variables of type phone. Initialize one, and have the user input a
number for the other one. Then display both numbers. The interchange might look like
this:
Enter your area code, exchange, and number: 415 555 1212
My number is (212) 767-8900
Your number is (415) 555-1212

Program :-
#include<iostream.h>
#include<conio.h>
void main()
{
struct phone
{
int area,exchange,no;
}p1,p2;
clrscr();
p1.area=212;
p1.exchange=767;
p1.no=8900;
cout<<"Enter your area code,exchange, and number :";
cin>>p2.area>>p2.exchange>>p2.no;
cout<<"My number is ("<<p1.area<<") "<<p1.exchange<<"-"<<p1.no<<endl;
cout<<"Your number is ("<<p2.area<<") "<<p2.exchange<<"-"<<p2.no;
getch();
}
OUTPUT:-
PRACTICAL 5
Aim :- Write a Program to implement Function
Overloading.
Program :-
#include<iostream>
char name[70]; //Global Variable
long roll; //Global Variable
long prn; //Global Variable
char sem; //Global Variable
using namespace std;
void getdata()
{
cout<<"Enter Name of Student := ";
cin>>name;
cout<<"Enter Roll No of Student := ";
cin>>roll;
}
void getdata(long int a)
{ prn=a; }
void getdata(char b)
{
sem=b;
}
void putdata()
{
cout<<"\nName of student := "<<name<<endl;
cout<<"Roll of Student := "<<roll<<endl;
cout<<"Permanent Registration Number := "<<prn<<endl;
cout<<"Semester of Student := "<<sem<<endl;
}
int main()
{
getdata(); //Function Call without Argument.
getdata(20154515L); //Function Call with 1 Long Integer Argument.
getdata('3'); //Function Call with 1 Character Argument.
cout<<"\nDetail of Students :- "<<endl;
putdata();
return 0;
}

OUTPUT:-
PRACTICAL 6
Aim :- Write a Program to implement Classs Private &
Public
Members ( data Members and Member
Functions ) .
Program :-
#include<iostream>
using namespace std;
class emp
{
int id;
char name[30];
int bs_salary;
float incentive;
float gross_salary;
public:
void putdata();
void getdata()
{ cout<<"Enter Employee ID := ";
cin>>id;
cout<<"Enter Name := ";
cin>>name;
cout<<"Enter Basic Salary := ";
cin>>bs_salary; }
private:
void gs()
{
incentive=bs_salary*14.5/100;
gross_salary=bs_salary+incentive;
cout<<"Incentive = "<<incentive<<endl;
cout<<"Gross Salary = "<<gross_salary<<endl;
} };
void emp::putdata()
{
cout<<"\nEmployee ID : "<<id<<"\n";
cout<<"Name : "<<name<<"\n";
cout<<"Basic Salary : "<<bs_salary<<endl;
gs();
}
int main()
{
emp e1,e2;
e1.getdata();
e2.getdata();
cout<<endl<<"Detail of Employee's Salary :-"<<endl;
e1.putdata();
e2.putdata();
return 0;
}

OUTPUT:-
PRACTICAL 7
Aim :- Write a Program to implement Static Member
( Static data
Member & Static Member Functions ) .
Program :-
#include<iostream>
using namespace std;
class ba
{
int static roll;
char name[40];
int fees;
public:
void static getroll()
{
cout<<"\nRoll No = "<<roll;
roll++;
}
void getdata()
{
cout<<"Enter Name of Student :- ";
cin>>name;
cout<<"Enter Fees of Student :- ";
cin>>fees;
}
void putdata()
{
cout<<"\nName = "<<name;
cout<<"\nFees = "<<"Rs "<<fees<<endl;
}
};
int ba::roll=1;
int main()
{
ba s1,s2;
s1.getdata();
ba::getroll();
s1.putdata();
cout<<endl;
s2.getdata();
ba::getroll();
s2.putdata();
return 0;
}

OUTPUT:-
PRACTICAL 8
Aim :- Create two classes DM and DB which store the value of distances. DM stores
distances in Meters and Centimeters and DB in Feet and Inches. Write a program that can
read values for the class objects and add one object of DM with another object of DB. Use a
friend function to carry out the addition operation. The object that stores the results maybe
a DM object or DB objects, depending on the units in which the results are required. The
display should be in the format of feet and inches or Meters and Centimeters depending on
the object on display.

Program :-
#include<iostream>
using namespace std;
class db;
class dm
{
double mt;
double cm;
public:
void getdata()
{
cout<<"Enter the value in Meters : = ";
cin>>mt;
cout<<"Enter the value in Centimeters : = ";
cin>>cm;
}
void putdata()
{
cout<<int(mt)<<" Meters & ";
cout<<cm<<" Centimeters"<<endl;
}
friend dm sum(dm,db);
};

class db
{
double ft;
double in;
public:
void getdata()
{
cout<<"Enter the value in Feet : = ";
cin>>ft;
cout<<"Enter the value in Inches : = ";
cin>>in;
}
void putdata()
{
cout<<ft<<" Feet & ";
cout<<in<<" Inch"<<endl;
}
friend dm sum(dm,db);
};
dm sum(dm x,db y)
{
x.mt=x.mt+x.cm/100+y.ft*0.3048+y.in*0.0254;
x.cm=(x.mt-int(x.mt))*100;
return x;
}
int main()
{
dm a,c;
db b;
a.getdata();
b.getdata();
c=sum(a,b);
cout<<"\nSum After Addition :- "<<endl;
c.putdata();
return 0;
}

OUTPUT:-
PRACTICAL 9
Aim :- A hospital wants to create a database regarding its indoor patients. The
information to store include
a) Name of the patient
b) Date of admission
c) Disease
d) Date of discharge
Create a structure to store the date (year, month and date as its members). Create a base
class to store the above information. The member function should include functions to
enter information and display a list of all the patients in the database. Create a derived
class to store the age of the patients. List the information about all the to store the age of
the patients. List the information about all the pediatric patients (less than twelve years in
age).

Program :-
#include<iostream>
using namespace std;
class base
{
char name[50];
public:
string disease;
struct dt
{
int date;
int month;
int year;
}d1,d2;
void getdata()
{
cout<<"Enter Name of Patient = ";
cin>>name;
cout<<"Enter Date of Admission = ";
cin>>d1.date>>d1.month>>d1.year;
cout<<"Enter Name of Disease = ";
cin>>disease;
cout<<"Enter Date of Discharge = ";
cin>>d2.date>>d2.month>>d2.year;
}
void putdata()
{
cout<<"Name = "<<name<<endl;
cout<<"Date of Admission = "<<d1.date<<"-"<<d1.month<<"-"<<d1.year<<endl;
cout<<"Name of Disease = "<<disease<<endl;
cout<<"Date of Discharge = "<<d2.date<<"-"<<d2.month<<"-"<<d2.year<<endl;
}};
class der:public base
{ int age;
public:
int rage()
{ return age; }
void getage()
{ cout<<"Enter Age = ";
cin>>age; }
void put()
{
cout<<"Age = "<<age<<endl;
}};
int main()
{
int n;
cout<<"Enter the number of Patients : ";
cin>>n;
cout<<"Enter detail of "<<n<<" Patients :-"<<endl;
der a[n];
for(int i=0;i<n;i++)
{ a[i].getdata();
a[i].getage(); }
int m=0;
cout<<"\n"<<"Detail of Pediatric Patients having Age less than 12 Years :- "<<"\n";
for(int j=0;j<n;j++)
{
if(a[j].disease=="pediatric" || a[j].disease=="Pediatric" && a[j].rage()<12)
{
cout<<"\n";
a[j].putdata();
a[j].put();
m++; }}
cout<<endl<<m<<" Pediatric Patient"<<endl;
}

OUTPUT:-

Вам также может понравиться