0% found this document useful (0 votes)
91 views14 pages

Robert Lafore C++,solved Exercise Problems, Chapter-2

The document contains 12 code segments with answers to questions. Each segment contains C++ code to perform calculations, input/output, and formatting. The code demonstrates basic C++ skills like variables, data types, operators, input/output streams, and formatting output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views14 pages

Robert Lafore C++,solved Exercise Problems, Chapter-2

The document contains 12 code segments with answers to questions. Each segment contains C++ code to perform calculations, input/output, and formatting. The code demonstrates basic C++ skills like variables, data types, operators, input/output streams, and formatting output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Robert Lafore

Chapter 2
Answer to the question number-1
The code segment:
#include<iostream>
#include <iomanip>
using namespace std;
int main()
{
double g;
cout<<"Enter the numbers in Gallons:"<<endl;
cin>>g;
cout<<g/7.481<<" cubic feets"<<endl;

}
Answer to the question number-2
The code segment:
#include<iostream>
#include <iomanip>
using namespace std;
int main()
{
double g;
cout<<setw(4)<<1990<<setw(12)<<135<<endl
<<setw(4)<<1991<<setw(12)<<7290<<endl
<<setw(4)<<1992<<setw(12)<<11300<<endl
<<setw(4)<<1993<<setw(12)<<16200<<endl;

}
Answer to the question number-3
The code segment:
#include<iostream>
#include <iomanip>
#include <ios>
using namespace std;
int main()
{
int x=10;
cout<<x<<endl;
x=20;
cout<<x<<endl;
--x;
cout<<x<<endl;
}

Answer to the question number-4


The code segment:
#include<iostream>
#include <iomanip>
#include <ios>
using namespace std;
int main()
{

cout<<"Candy is dandy,\n"<<"But liquor is quicker.";


}
Answer to the question number-5
The code segment:
#include<iostream>
#include<ctype.h>
using namespace std;
int main()
{
char t;
cin>>t;
int d=islower(t);
cout<<d<<endl;
}
Answer to the question number-6
The code segment:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
double usd;
cin>>usd;
double BP=1.487,FF=0.172,GD=0.584,JY=0.00955;
cout<<setw(22)<<"British pound: "<<setw(10)<<usd/BP<<endl
<<setw(22)<<"French franc: "<<setw(10)<<usd/FF<<endl
<<setw(22)<<"German deutschemark: "<<setw(10)<<usd/GD<<endl
<<setw(22)<<"Japanese yen: "<<setw(10)<<usd/JY<<endl;
}
Answer to the question number-7
The code segment:
#include<iostream>
using namespace std;
int main()
{
double celc;
cin>>celc;
double faren;
faren=((celc/5)*9)+32;
cout<<faren<<"F"<<endl;
}
Answer to the question number-8
The code segment:
#include<iostream>
#include <iomanip>
#include <ios>
using namespace std;
int main()
{
double g;
cout<<setw(4)<<1990<<setw(12)<<setfill('.')<<135<<endl
<<setw(4)<<1991<<setw(12)<<setfill('.')<<7290<<endl
<<setw(4)<<1992<<setw(12)<<setfill('.')<<11300<<endl
<<setw(4)<<1993<<setw(12)<<setfill('.')<<16200<<endl;

}
Answer to the question number-9
The code segment:
#include<iostream>
#include <iomanip>
#include <ios>
using namespace std;
int main()
{

int a,b,c,d;
char j;
cout<<"Enter first fraction: "<<endl;
cin>>a>>j>>b;
cout<<"Enter second fraction: "<<endl;
cin>>c>>j>>d;
int m=(a*d)+(b*c);
int k=b*d;
cout<<"Sum= "<<m<<'/'<<k<<endl;
}

Answer to the question number-10


The code segment:
#include<iostream>
#include <iomanip>
#include <ios>
#include<bits/stdc++.h>
using namespace std;
int main()
{

double pound, shell,pence;


cout<<"Enter pounds: "<<endl;
cin>>pound;
cout<<"Enter shillings: "<<endl;
cin>>shell;
cout<<"Enter pence: "<<endl;
cin>>pence;
double sum;
shell=(shell/20);
pence=(pence/(20*12));
sum=pound+shell+pence;
cout<<"Decimal pounds = "<<'\x9c'<<sum<<endl;

}
Answer to the question number-11
The code segment:
#include <ios>
#include<bits/stdc++.h>
using namespace std;
int main()
{

cout<<setw(4)<<"Last name"<<setw(12)<<"First name"<<setw(16)<<"Street


address"<<setw(10)<<"Town"<<setw(14)<<"State"<<endl
<<setw(56)<<"............................................................."<<endl
<<setw(4)<<"Jones"<<setw(13)<<"Bernard"<<setw(18)<<"109 Pine
Lane"<<setw(17)<<"Littletown"<<setw(5)<<"MI"<<endl
<<setw(4)<<"O'Brian"<<setw(10)<<"Coleen"<<setw(21)<<"42 E. 99th
Ave."<<setw(12)<<"Bigcity"<<setw(8)<<"NY"<<endl
<<setw(4)<<"Wong"<<setw(12)<<"Harry"<<setw(24)<<"121-A Alabama
St."<<setw(12)<<"Lakeville"<<setw(6)<<"IL"<<endl;
}

Answer to the question number-12


The code segment:
#include<iostream>
#include <iomanip>
#include <ios>
#include<bits/stdc++.h>
using namespace std;
int main()
{

double user;
cout<<"Enter decimal pounds:"<<endl;
cin>>user;
int j=user;
cout<<"Equivalent in old notation = "<<'\x9c'<<j<<".";
double frac=user-j;
frac*=20;
int newfrac=frac;
cout<<newfrac<<".";
frac=frac-newfrac;
frac*=12;
frac=(int) frac;
cout<<frac<<"."<<endl;

You might also like