Sei sulla pagina 1di 42

Question#1

#include<iostream>
#include<conio.h>
using namespace std;
struct phone{int area_code, exchange,
number;};
int main()
{

phone input, mine={212, 767, 8900};


cout<<"Enter your area code, exchange, and
number: ";

cin >>input.area_code>>input.exchange>>inp
ut.number;
cout<<"My number is
("<<mine.area_code<<")
"<<mine.exchange<<"-"<<mine.number<<endl
;
cout<<"Your number is
("<<input.area_code<<")
"<<input.exchange<<"-"<<input.number;

}
Question#2

#include<iostream>
#include<conio.h>
using namespace std;
struct point{int x, y;};
int main()
{
point p1, p2, sum;
cout<<"Enter coordinates for p1: ";
cin >>p1.x>>p1.y;
cout<<"Enter coordinates for p2: ";
cin >>p2.x>>p2.y;
sum.x=p1.x+p2.x; sum.y=p1.y+p2.y;
cout<<"Coordinates of p1+p2 are:
"<<sum.x<<", "<<sum.y;

}
Question#3
#include<iostream>
#include<conio.h>
using namespace std;
struct Distance{int feet; float inches;};
struct Volume{Distance x, y, z;};
int main()
{

Volume dimension;
char c;
cout<<"Enter x, y & z ...\n(EX: 3.6=3 feet and
6 inches, default: 'x.0 y.0 z.0' x, y & z are in
feet) : \n";
cin >>dimension.x.feet>>c>>dimension.x.inche
s
>>dimension.y.feet>>c>>dimension.y.inches
>>dimension.z.feet>>c>>dimension.z.inches
;
cout<<"the volume is : "

<<(dimension.x.feet+dimension.x.inches/12)*

(dimension.y.feet+dimension.y.inches/12)*

(dimension.z.feet+dimension.z.inches/12);
}
Question#4
#include<iostream>
#include<conio.h>

using namespace std;


struct employee{int number; float
compensation;};
int main()
{
employee e[3]; int i;

for(i=0; i<3; i++)


{
cout<<"Enter the number of employee
number "<<i+1<<" : ";
cin>>e[i].number;
cout<<"Enter the compensation of employee
number "<<i+1<<" : ";
cin>>e[i].compensation;}
cout<<"Employee number"<<" Employee
compensation\n";
for(i=0; i<3; i++)
cout<<setw(15)<<e[i].number<<setw(24)<<e[i
].compensation<<endl;
}
Question#5
#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
struct date{int day; int month; int year;};
int main()
{

date x;
char c;
cout<<"Enter the date : ";

cin >>x.day>>c>>x.month>>c>>x.year;
cout<<"The date is : ";

cout<<x.day<<c<<x.month<<c<<x.year;
}
Question#6

#include<iostream>
using namespace std;
#include<conio.h>
enum etype{laborer, secretary, manager,
accountant, executive, researcher};
int main()
{
etype x; char *ret;
cout<<"Enter employee type (first letter
only)"<<endl
<<"(laborer, secretary, manager,
accountant, executive, researcher): ";
switch(getche())
{
case 'l': x=laborer ;
break;
case 's': x=secretary ;
break;
case 'm': x=manager ;
break;
case 'a': x=accountant;
break;
case 'e': x=executive ;
break;
case 'r': x=researcher;
}
switch(x)
{
case 0: ret = "laborer" ;
break;
case 1: ret = "secretary" ;
break;
case 2: ret = "manager" ;
break;
case 3: ret = "accountant";
break;
case 4: ret = "executive" ;
break;
case 5: ret = "researcher";
}
cout<<"\nEmployee type is "<<ret<<".";

}
Question#7

#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
enum etype{laborer, secretary, manager,
accountant, executive, researcher};
struct date{int day; int month; int year;};
struct employee{int number; float
compensation; date d; char *ret;};
int main()
{

employee e[3];
int i;
char c;
etype x;

for(i=0; i<3; i++)


{
cout<<"\nEnter the number of employee
number "<<i+1<<" : ";
cin>>e[i].number;
cout<<"Enter the compensation of employee
number "<<i+1<<" : ";
cin>>e[i].compensation;
cout<<"Enter employee type (first letter only)
of employee number "<<i+1<<" : "<<endl
<<"(laborer, secretary, manager,
accountant, executive, researcher): ";
switch(getche())
{
case 'l': x=laborer ;
break;
case 's': x=secretary ;
break;
case 'm': x=manager ;
break;
case 'a': x=accountant;
break;
case 'e': x=executive ;
break;
case 'r': x=researcher;
}
switch(x)
{
case 0: e[i].ret = "laborer" ;
break;
case 1: e[i].ret = "secretary" ;
break;
case 2: e[i].ret = "manager" ;
break;
case 3: e[i].ret = "accountant";
break;
case 4: e[i].ret = "executive" ;
break;
case 5: e[i].ret = "researcher";
break;
default: e[i].ret = "Unknow";}
cout<<"\nEnter the date of employee
number "<<i+1<<" : ";
cin >>e[i].d.day>>c>>e[i].d.month>>c>>e[i].d.y
ear;} cout<<"\nEmployee number"<<"
compensation"<<" type"<<"
date of first employment"<<endl;
for(i=0; i<3; i++)
cout<<setw(15)<<e[i].number
<<setw(15)<<e[i].compensation
<<setw(15)<<e[i].ret

<<setw(21)<<e[i].d.day<<c<<e[i].d.month<<c<
<e[i].d.year<<endl;

}
Question#8
#include<iostream>
#include<conio.h>
using namespace std;
struct fraction{int numerator; int
denominator;};
int main()
{

fraction equ[2];
char operation;
cout<<"Enter first fraction: ";

cin >>equ[0].numerator>>operation>>equ[0].
denominator;

cout<<"Enter second fraction: ";

cin >>equ[1].numerator>>operation>>equ[1].
denominator;
cout<<"Sum =
"<<(equ[0].numerator*equ[1].denominator+e
qu[0].denominator*equ[1].numerator)

<<operation<<(equ[0].denominator*equ[1].de
nominator)<<endl;

}
Question#9
#include<iostream>
#include<conio.h>
using namespace std;
struct time{int hours; int minutes; int
seconds;};
int main()
{

time t1; char c;

cout<<"Enter a time value in hours, minutes,


and seconds [hh:mm:ss] format: ";

cin >>t1.hours>>c>>t1.minutes>>c>>t1.second
s;
cout<<"The total number of seconds is:
"<<t1.hours*3600 + t1.minutes*60 +
t1.seconds;

}
Question#10

#include<iostream>
#include<conio.h>
using namespace std;
struct sterling{int pounds; int shillings; int
pence;};
int main()
{

float decpounds; float decfrac;


sterling s1;
cout<<"Enter a money amount in new-style
decimal pounds: ";
cin >>decpounds;
s1.pounds = static_cast<int>(decpounds);
decfrac = 240*(decpounds-s1.pounds);
s1.shillings = (static_cast<int>(decfrac))%12;
decfrac =
static_cast<int>((decfrac-s1.shillings)/12);
cout<<"Equivalent in old notation =
\x9c"<<s1.pounds<<"."<<decfrac<<"."<<s1.shil
lings<<endl;

}
Question#11
#include<iostream>
#include<conio.h>
using namespace std;
struct time{int hours; int minutes; int
seconds;};
int main()
{
time t1, t2, t3; char c; long tmp;
cout<<"In [hh:mm:ss] format;\n";
cout<<"Enter first time value : ";
cin >>t1.hours>>c>>t1.minutes>>c>>t1.second
s;
cout<<"Enter second time value: ";

cin >>t2.hours>>c>>t2.minutes>>c>>t2.second
s;

tmp=t1.hours*3600+t1.minutes*60+t1.second
s+t2.hours*3600+t2.minutes*60+t2.seconds;
t3.seconds=tmp%60;
t3.minutes=((tmp-t3.seconds)%3600)/60;
t3.hours=tmp/3600;

if(t3.seconds>59)
{t3.seconds-=59; t3.minutes++;}
if(t3.minutes>59)
{t3.minutes-=59; t3.hours++;}
cout<<"The result is:
"<<t3.hours<<":"<<t3.minutes<<":"<<t3.secon
ds;
}
Question#12

#include<iostream>
#include<conio.h>
using namespace std;
struct fraction{int numerator; int
denominator;};
int main()
{
fraction f[2]; char c, op;
cout<<"Enter your task : ";

cin >>f[0].numerator>>c>>f[0].denominator>>
op>>f[1].numerator>>c>>f[1].denominator;
if(!f[0].denominator || !f[1].denominator)
{cout<<"Illeagle fraction !"<<endl; op=false;}
switch(op)
{
case '+':
cout<<"Answer =
"<<(f[0].numerator*f[1].denominator+f[0].den
ominator*f[1].numerator)<<c
<<(f[0].denominator*f[1].denominator);
break;
case '-':
cout<<"Answer =
"<<(f[0].numerator*f[1].denominator-f[0].den
ominator*f[1].numerator)<<c
<<(f[0].denominator*f[1].denominator);
break;
case '*':
cout<<"Answer =
"<<f[0].numerator*f[1].numerator<<c<<f[0].d
enominator*f[1].denominator;
break;
case '/':
if(f[0].numerator != 0) cout<<"Answer =
"<<f[0].numerator*f[1].denominator<<c

<<f[1].numerator*f[0].denominator;
else cout<<"Math
error !"<<endl;
break;
default:
cout<<"Unknow operator please try
again !"<<endl;}

}
Question#13
#include<iostream>
#include<conio.h>
using namespace std;

float circarea(float radius);


int main()
{
float rad;
cout<<"Enter radius of circle: ";
cin>> rad;
cout <<"Area is "<<circarea(rad);

float circarea(float radius)


{ return 3.14159F*radius*radius;}

Question#14
#include<iostream>
#include<conio.h>
using namespace std;
double power(double n, int p=2);

int main(void)
{

double n; int p=2;


cout<<"Enter n: "; cin>> n;
cout<<"Enter p: "; cin>> p;
cout <<"The power is "<<power(n, p);
}

double power(double n, int p)


{ for(int ret=1; p>0; p--)
ret*=n;
}
Question#15

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int n1, n2;
cout<<"Enter n1: ";
cin>> n1;
cout<<"Enter n2: ";
cin>> n2;
cout <<"The number assigned to zero is ";
if (n1, n2) cout<<"n1";
else cout<<"n2";
}

bool zeroSmaller(int& n1, int& n2)


{
if(n1<n2) {n1=0; return true;}
else {n2=0;return false;}
}
Question#16

#include<iostream>
#include<conio.h>
using namespace std;
int distWatch(int d1, int d2);
int main()
{
int d1, d2;
cout<<"Enter d1: ";
cin>> d1;
cout<<"Enter d2: ";
cin>> d2;
cout <<"The largest distance is
"<<distWatch(d1, d2);
}

int distWatch(int d1, int d2)


{ if(d1<d2) return d2;
else return d1;}

Question#17
#include<iostream>
#include<conio.h>
using namespace std;
long hms_to_secs(int hours, int minutes, int
seconds);
int main()
{
int h, m, s;
char sep;
cout<<"Enter the time in format hh:mm:ss :
";
cin>>h>>sep>>m>>sep>>s;
cout <<"The equivalent time in seconds is :
"<<hms_to_secs(h, m, s);
}

long hms_to_secs(int hours, int minutes, int


seconds)
{ return seconds+minutes*60+hours*3600;}

Question#18
#include<iostream>
#include<conio.h>
using namespace std;
struct time{int hours; int minutes; int
seconds;};
long time_to_secs(time t);
time secs_to_time(long s);
int main()
{
time t1, t2, t3;
char c;
cout<<"In [hh:mm:ss] format;\n";
cout<<"Enter first time value : ";

cin >>t1.hours>>c>>t1.minutes>>c>>t1.second
s;
cout<<"Enter second time value: ";
cin >>t2.hours>>c>>t2.minutes>>c>>t2.second
s;

t3=secs_to_time(time_to_secs(t1)+time_to_se
cs(t2));
cout<<"The result is:
"<<t3.hours<<":"<<t3.minutes<<":"<<t3.secon
ds;
}

long time_to_secs(time t){ return


t.hours*3600+t.minutes*60+t.seconds;}
time secs_to_time(long s){
time t;
t.seconds=s%60;
t.minutes=((s-t.seconds)%3600)/60;
t.hours=s/3600;
if(t.seconds>59) {t.seconds-=59;
t.minutes++;}
if(t.minutes>59) {t.minutes-=59; t.hours++;}
return t;
}
Question#19

#include<iostream>
#include<conio.h>
using namespace std;
double power(double n, int p=2);
char power(char n, int p=2);
int power(int n, int p=2);
long power(long n, int p=2);
float power(float n, int p=2);

int main()
{
char sep; int p=2; double d_n; char c_n; int
i_n; long l_n; float f_n;
cout<<"In [n^p] format;\n";
cout<<"Enter a double type n : ";
cin >>d_n>>sep>>p;
cout<<"The power is "<<power(d_n,
p)<<endl;
cout<<"Enter a char type n : ";
cin >>c_n>>sep>>p;
cout<<"The power is "<<power(c_n,
p)<<endl;
cout<<"Enter a int type n : ";
cin >>i_n>>sep>>p;
cout<<"The power is "<<power(i_n, p)<<endl;
cout<<"Enter a long type n : ";
cin >>l_n>>sep>>p;
cout<<"The power is "<<power(l_n, p)<<endl;
cout<<"Enter a float type n : ";
cin >>f_n>>sep>>p;
cout<<"The power is "<<power(f_n,
p)<<endl;
}
double power(double n, int p)
{ for(int ret=1; p>0; p--) ret*=n; }
char power(char n, int p)
{ for(int ret=1; p>0; p--) ret*=n; }
int power(int n, int p)
{ for(int ret=1; p>0; p--) ret*=n; }
long power(long n, int p)
{ for(int ret=1; p>0; p--) ret*=n; }
float power(float n, int p)
{ for(int ret=1; p>0; p--) ret*=n; }

Question#20

#include<iostream>
#include<conio.h>
using namespace std;
void swap(int& a, int& b);
int main()
{
int a, b;
cout<<"Enter a : ";
cin >>a;
cout<<"Enter b : ";
cin >>b; swap(a, b);
cout<<"Now a value is : "<<a<<" and b value
is : "<<b;
}

void swap(int& a, int& b){int c=a; a=b; b=c;}

Question#21

#include<iostream>
#include<conio.h>
using namespace std;
struct time{int hours; int minutes; int
seconds;};
void swap(time& t1, time& t2);
int main()
{
time t1, t2; char c;
cout<<"In [hh:mm:ss] format;\n";
cout<<"Enter first time value : ";

cin >>t1.hours>>c>>t1.minutes>>c>>t1.second
s;
cout<<"Enter second time value: ";

cin >>t2.hours>>c>>t2.minutes>>c>>t2.second
s;
swap(t1, t2);
cout<<"Now first time is : "
<<t1.hours<<c<<t1.minutes<<c<<t1.seconds
<<" and second time is :
"<<t2.hours<<c<<t2.minutes<<c<<t2.seconds;
}
void swap(time& a, time& b){time c=a; a=b;
b=c;}

Question#22

#include<iostream>
#include<conio.h>
using namespace std;
void caller_counter(void);
int main()
{
int outer_counter=0;
outer_counter++; caller_counter();
cout<<"\nThe main programme counter
value is: "<<outer_counter;
}

void caller_counter(void)
{
static int inner_counter=0; inner_counter++;
cout<<"I have been called
"<<inner_counter<<" times";
}
Question#23

#include<iostream>
#include<conio.h>
using namespace std;
struct sterling{int pounds; int shillings; int
pence;};
sterling psp_to_sterling(int pounds, int
shillings, int pence);
sterling sterling_add(sterling s1, sterling s2);
void sterling_disp(sterling s);
char c;
int main()
{
int x, y, z;
sterling x1, x2;
cout<<"In [9:19:11] format;\n";
cout<<"Enter first money amount in old-style
British : \x9c";
cin>>x>>c>>y>>c>>z;
x1=psp_to_sterling(x, y, z);
cout<<"Enter second money amount in
old-style British : \x9c";
cin>>x>>c>>y>>c>>z; x2=psp_to_sterling(x, y,
z);
sterling_disp(sterling_add(x1, x2));
}

sterling psp_to_sterling(int pounds, int


shillings, int pence)
{
sterling x;
x.pounds=pounds; x.shillings=shillings;
x.pence=pence; return x;
}

sterling sterling_add(sterling s1, sterling s2)


{
s1.pounds += s2.pounds;
s1.shillings += s2.shillings;
s1.pence += s2.pence;
if(s1.pence>11){s1.shillings +=
static_cast<int>(s1.pence/12); s1.pence %=
12;}
if(s1.shillings>19){s1.pounds +=
static_cast<int>(s1.shillings/20);
s1.shillings %= 20;}
return s1;}

void sterling_disp(sterling s){


cout<<"Total
is
: \x9c"
<<s.pounds<<c<<s.shillings<<c<<s.pence;}
Question#24

#include<iostream>
#include<conio.h>
using namespace std;
struct fraction{int numerator; int
denominator;};
fraction fadd(fraction a, fraction b);
fraction fsub(fraction a, fraction b);
fraction fmul(fraction a, fraction b);
fraction fdiv(fraction a, fraction b);
int main()
{
fraction f[3]; char c, op;
cout<<"Enter your task : ";

cin >>f[0].numerator>>c>>f[0].denominator>>
op>>f[1].numerator>>c>>f[1].denominator;
if(!f[0].denominator || !f[1].denominator)
{cout<<"Illeagle fraction !"<<endl; op=false;}
switch(op) {
case '+':
f[2]=fadd(f[0], f[1]);
break;
case '-':
f[2]=fsub(f[0], f[1]);
break;
case '*':
f[2]=fmul(f[0], f[1]);
break;
case '/':
f[2]=fdiv(f[0], f[1]);
break;
default:
cout<<"Unknow operator please try
again !"<<endl;}
cout<<"Answer =
"<<f[2].numerator<<c<<f[2].denominator;
}

fraction fadd(fraction a, fraction b)


{
fraction f;
f.numerator
=a.numerator*b.denominator+a.denominator
*b.numerator;

f.denominator=a.denominator*b.denominator;
return f;
}

fraction fsub(fraction a, fraction b)


{
fraction f;
f.numerator
=a.numerator*b.denominator-a.denominator
*b.numerator;

f.denominator=a.denominator*b.denominator;
return f;
}

fraction fmul(fraction a, fraction b)


{
fraction f;
f.numerator =a.numerator*b.numerator;

f.denominator=a.denominator*b.denominator;
return f;
}

fraction fdiv(fraction a, fraction b)


{
fraction f;
if(b.numerator != 0)
{
f.numerator =a.numerator*b.denominator;
f.denominator=b.numerator*a.denominator;
}
else cout<<"Math
error !"<<endl; return f;
}

Potrebbero piacerti anche