Programs of File
Programs of File
the number.
#include<iostream.h>
#include<conio.h>
double pow(double ,int );
void main()
{
double n,r;
clrscr();
int p;
char c;
cout<<"enter any number";
cin>>n;
do
{
cout<<"do u want to enter the
power(y/n)\n";
cin>>c;
if(c=='y')
{
cout<<"enter the power to be raised";
cin>>p;
r=pow(n,p);
}
else
{
if(c=='n')
{
p=2;
r=pow(n,p);
cout<<r;
}
else
cout<<"invalid choice\n";
}
} while(c!='y' && c!='n');
getch();
OUTPUT:-
enter any number4
do u want to enter the power(y/n)
y
enter the power to be raised2
16
PROG 2:-write a program using structure
#include<iostream.h>
#include<conio.h>
struct point
{
int x;
int y;
}
p[2],pt={0,0};
void main()
{
int i;
for(i=0;i<=1;i++)
{
cout<<"enter x"<<i<<"and y coordinate"<<i;
cin>>p[i].x>>p[i].y;
}
for(i=0;i<=1;i++)
{
pt.x=pt.x+p[i].x;
pt.y=pt.y+p[i].y;
}
cout<<"\nsum of x coordinate
is"<<pt.x<<"\nsum of y coordinate is"<<pt.y;
getch();
}
Output:-
#include<iostream.h>
#include<conio.h>
void main()
{
double operand1;
double operand2;
char c,x;
do
{
cout<<"enter the firstno,operator,secondno";
cin>>operand1>>x>>operand2;
switch(x)
{
case
'+':cout<<"\nanswer="<<operand1+operand2;
break;
case '-':cout<<"\nanswer="<<operand1-
operand2;
break;
case
'*':cout<<"\nanswer="<<operand1*operand2;
break;
case
'/':cout<<"\nanswer="<<operand1/operand2;
break;
default: cout<<"\ninvalid choice";
}
do
{cout<<"\nenter choice";
cin>>c;
if(c!='y' && c!='n')
cout<<"\ninvalid choice";
}
while(c!='y' && c!='n');
}
while(c=='y');
getch();
}
PROG 4:-WRITE A PROGRAM USING STRUCTURE
#include<iostream.h>
#include<conio.h>
struct phone
{
char area[10];
char exchange[10];
char phno[10];
};
void main()
{
phone p1={"212","767","89000"};
phone p2;
cout<<"\nenter your phone no.\n";
cin>>p2.area>>p2.exchange>>p2.phno;
cout<<"\n my phoneno
is\n"<<p1.area<<p1.exchange<<p1.phno;
cout<<"\n your phoneno
is\n"<<p2.area<<p2.exchange<<p2.phno;
getch();
}
OUTPUT:-
my phoneno is
21276789000
your phoneno is
34432144444
PROG 5:-WRITE A PROGRAM A HOTDOG STAND THAT
ENABLES THREE KINDS OF ITERATION.
#include<iostream.h>
#include<conio.h>
class hotdogsand
{
private:
int hotdog_one_hand;
int buns_one_hand;
public:
void display()
{
cout<<hotdog_one_hand;
cout<<buns_one_hand;
}
void soldonehand()
{
--hotdog_one_hand;
--buns_one_hand;
}
void initdata()
{
cout<<"\nenter the hotdog on hand\n";
cin>>hotdog_one_hand;
cout<<"\nenter the buns on hand\n";
cin>>buns_one_hand;
}
}; //end of class
void main()
{
hotdogsand stand1;
hotdogsand stand2;
hotdogsand stand3;
clrscr();
cout<<"\nintilizes the data of stand1\n";
stand1.initdata();
cout<<"\nintilizes the data of stand2\n";
stand2.initdata();
cout<<"\nintilizes the data of stand3\n";
stand3.initdata();
char choice='x';
while(choice!='q')
{
cout<<"\nenter the choice of stand\n";
cin>>choice;
switch(choice)
{
case 1:cout<<"\n sell the data of
stand1\n";
stand1.soldonehand();
break;
case 2:cout<<"\n sell the data of
stand2\n";
stand2.soldonehand();
break;
case 3:cout<<"\n sell the data of
stand3\n";
stand3.soldonehand();
break;
}
}
cout<<"\n\ndisplay the value of
stand1\n\n";
stand1.display();
cout<<"\n\ndisplay the value of
stand2\n\n";
stand2.display();
cout<<"\n\ndisplay the value of
stand3\n\n";
stand3.display();
getch();
}
Output:-
2354
7654
3287
PROG 6:-WRITE A PROGRAM TO READ THE DATA OF N
NO. OF THE EMPLOYEE AND COMPUTE THE NET
SALARY.
#include<iostream.h>
#include<conio.h>
class employee
{
private:
int empno;
char empname[20];
int bs;
int da;
int incometax;
int ns;
public:
void getdata()
{
cout<<"\nenter the employee number\n";
cin>>empno;
cout<<"\nenter the empname\n";
cin>>name;
cout<<"\nenter the basic salary\n";
cin>>basicsalary;
}
void display()
{
cout<<"employee number is"<<empno;
cout<<"employee name is"<<empname;
cout<<"employee net salary is"<<netsalary;
}
void calculate()
{
da=52/100*bs;
incometax=30/100*gs;
ns=gs-incometax;
}
}
void main()
{
employee x[5];
int i;
clrscr();
for(i=1;i<5;i++)
{
x[i].getdata();
}
for(i=1;i<5;i++)
{
x[i].netsalary();
}
for(i=1;i<5;i++)
{
x[i].display();
}
getch();
}
Output:-
enter the name of the employee
kanchan
enter the employee number
12
enter the basic salary of the employee
12000
enter the name of the employee
juhi
enter the employee number
14
enter the basic salary of the employee
12999
name of the employee is
kanchannumber of employee is
12net salary of employee is
0name of the employee is
juhinumber of employee is
14net salary of employee is
0
#include<iostream.h>
#include<conio.h>
#include<string.h>
const int Maxlength=40;
class fancy_text
{
private:
char text[Maxlength];
public:
void set_text(char tx[])
{
strcpy(text,tx);
}
void box_display();
void box_display(char);
void box_display(char, int);
};
void fancy_text::box_display()
{
cout<<"--------------------------------------
";
cout<<endl<<text<<endl;
cout<<"--------------------------------------
";
}
void fancy_text::box_display(char ch)
{
int j;
for(j=0;j<Maxlength;j++)
cout<<ch;
cout<<endl<<text<<endl;
for(j=0;j<Maxlength;j++)
cout<<ch;
}
void fancy_text::box_display(char ch,int n)
{
int m;
for(m=0;m<n;m++)
cout<<ch;
cout<<endl<<text<<endl;
for(m=0;m<n;m++)
cout<<ch;
}
void main()
{
fancy_text ft;
clrscr();
ft.set_text("gone with wind");
ft.box_display();
ft.box_display('=');
ft.box_display('=',18);
getch();
}
OUTPUT:-
--------------------------------------
gone with wind
--------------------------------------
========================================
gone with wind
=============================================
=============
#include<iostream.h>
#include<conio.h>
#include<string.h>
const int Maxlength=40;
class fancy_text
{
private:
char text[Maxlength];
public:
void set_text(char tx[])
{
strcpy(text,tx);
}
void box_display(char ch ='-', int n=18)
{
int m;
for(m=0;m<n;m++)
cout<<ch;
cout<<endl<<text<<endl;
for(m=0;m<n;m++)
cout<<ch;
}
};
void main()
{
fancy_text ft;
clrscr();
ft.set_text("gone with wind");
ft.box_display('=',18);
getch();
}
OUTPUT:-
==================
gone with wind
PROG 9:-WRITE A PROGRAM TO CREATE FUNCTION
ARGUMENTS.
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
class airtime
{
int hours;
int mins;
public:
void set()
{
char dummy;
cout<<"enter time(format 23:59)";
cin>>hours>>dummy>>mins;
}
void display()
{
cout<<hours<<":"<<setfill('0')<<setw(2)<<mins
;
}
void add(airtime a1,airtime a2)
{
mins=a1.mins+a2.mins;
hours=a1.hours+a2.hours;
if(mins>59)
{
mins=mins-60;
hours=hours+1;
}
if(hours>23)
{
hours=hours-24;
}
}
}; //end of class
void main()
{
airtime t1,t2,t3;
clrscr();
cout<<"for t1";
t1.set();
cout<<"for t2";
t2.set();
t3.add(t1,t2);
cout<<"for t3=";
t3.display();
getch();
}
OUTPUT:-
#include<iostream.h>
#include<conio.h>
class parent
{
public:
parent()
{
cout<<"parent constructor";
}
~parent()
{
cout<<"parent destructor";
}
}
class child:public parent
{
public:
child()
{
cout<<"child constructor";
}
~child()
{
cout<<"child destructor";
}
}
void main()
{
cout<<"\nstarting";
child ch;//creating a child object
cout<<"\nterminating";
}
output:-
starting
parent constructor
child constructor
terminating
child destructor
parent destructor
#include<iostream.h>
#include<conio.h>
class english
{
private:
int feet;
float inch;
public:
english()
{
feet=0;
inch=0.0;
}
english(float metres)
{
const float mt=3.280883;
float ft=mt*metres;
feet= int(ft);
inch= 12*(ft-feet);
}
english(int f, float i)
{
feet=f;
inch=i;
}
void display()
{
cout<<feet<<"'-"<<inch<<'"';
}
};
void main()
{
clrscr();
english e1;
cout<<"e1=";
e1.display();
english e2(1.2);
cout<<"\ne2=";
e2.display();
english e3(3,3.2);
cout<<"\ne3=";
e3.display();
getch();
}
Output:-
e1=0'-0"
e2=3'-11.244719"
e3=3'-3.2"
#include<iostream.h>
#include<conio.h>
class employee
{
private:
char ename[20];
int esno ;
public :
void input()
{
cout<<"enter the name of the employee\n";
cin>>ename;
cout<<"enter the serial number of the
employee\n";
cin>>esno;
}
void output()
{
cout<<"employee name is"<< ename<<endl;
cout<<"serail number of employee is"
<<esno<<endl;
}
};
void swap(employee& m, employee& n)
{
employee vemp=m;
m=n;
n=vemp;
}
void main()
{
void swap(employee&, employee&);
employee e1,e2;
clrscr();
cout<<"enter the value of employee 1\n";
e1.input();
cout<<"enter the value of employee 2\n";
e2.input();
swap(e1,e2);
cout<<"value of employee first is:\n";
e1.output();
cout<<"value of employee second is :\n";
e2.output();
getch();
}
Output:-
enter the value of employee 1
enter the name of the employee
kanchan
enter the serial number of the employee
12
enter the value of employee 2
enter the name of the employee
deepika
enter the serial number of the employee
14
value of employee first is:
employee name isdeepika
serail number of employee is14
value of employee second is :
employee name iskanchan
serail number of employee is12
#include<iostream.h>
#include<conio.h>
class employee
{
private:
char name[20];
int number;
public:
void getdata()
{
cout<<"enter the name";
cin>>name;
cout<<"enter the number";
cin>>number;
}
void putdata()
{
cout<<"name="<<name<<endl;
cout<<"number="<<number<<endl;
}
};
class manager:public employee
{
private:
char title[20];
int dues;
public:
void getdata()
{
employee::getdata();
cout<<"enter the title";
cin>>title;
cout<<"enter the dues";
cin>>dues;
}
void putdata()
{
employee::putdata();
cout<<"title="<<title<<endl;
cout<<"dues="<<dues<<endl;
}
};
class scientist:public employee
{
private:
char publis[20];
public:
void getdata()
{
employee::getdata();
cout<<"enter the publication";
cin>>publis;
}
void putdata()
{
employee::putdata();
cout<<"publis"<<publis<<endl;
}
};
class labour:public employee
{
};
void main()
{
clrscr();
manager m1;
scientist s1;
labour l1;
cout<<"\ninput the object m1 of the class
manager\n";
m1.getdata();
cout<<"\ninput the object s1 of the class
scientist\n";
s1.getdata();
cout<<"\ninput the object l1 of the class
labour\n";
l1.getdata();
cout<<"\noutput the object m1 of the class
manager\n";
m1.putdata();
cout<<"\noutput the object s1 of the class
scientist\n";
s1.putdata();
cout<<"\noutput the object l1 of the class
labour\n";
l1.putdata();
}
Output:-
input the object m1 of the class managerenter
the namekanchan
enter the number12
enter the titletatoo
enter the dues123454
input the object s1 of the class
scientistenter the namedeepali
enter the number13
enter the publicationgdf
input the object l1 of the class labourenter
the namejuhi ▒
enter the number15
output the object m1 of the class
managername=kanchan
number=12
title=tatoo
dues=-7618
output the object s1 of the class
scientistname=deepali
number=13
publisgdf
output the object l1 of the class
labourname=juhi
number=15
#include<iostream.h>
#include<conio.h>
class shared
{
static int a;
int b;
public:
void set(int i,int j)
{
a=i;
b=j;
}
void show();
};
int shared::a;
void shared::show()
{
cout<<"this is static a:"<<a<<endl;
cout<<"this is static b:"<<b<<endl;
cout<<"\n";
}
void main()
{
clrscr();
shared x,y;
x.set(1,1);
x.show();
y.set(2,2);
y.show();
x.show();
getch();
}
Output:-
#include<iostream.h>
#include<conio.h>
class c1
{
static int resources;
public:
static int get_resources();
void free_resources()
{
resources=0;
}
};
int c1::resources;
int c1::get_resources()
{
if(resources)
return 0;
else
return 1;
}
void main()
{
clrscr();
c1 ob1,ob2;
if(c1::get_resources());
cout<<"ob1 has resources\n";
if(!c1::get_resources())
cout<<"ob2 denied resources\n";
ob1.free_resources();
if(ob2.get_resources())
cout<<"ob2 can now use resources";
getch();
}
Output:-