c++ chap.3 examples...


Example.1
#include <iostream.h>
void main(void)
{
int numb;
cout <<”enter a number: ”;
cin>>numb;
cout<<”numb<10 is “<<(numb<10)<<endl;
cout<<”numb>10 is “<<(numb>10)<<endl;
cout<<”numb==10 is “<<(numb==10)<<endl;
}
Output
Enter number: 20                                          //20 is the assume number in the example
numb<10 is 0
numb<10 is 1
numb<10 is 0

E xample.2

#include <iostream.h>
void main(void)
{
int    j ;
for(j=0; j<15; j++)
cout <<j*j <<endl;
}
Output
0  1   4   9   16   25   36   49  64   81  100   121   144  169  196

Example.3
#include <iostream.h>
#include <iomanip.h>
void main(void)
{
int    j;
for (j==0;j<=10;j++)
cout << setw(4)<< j;
int cube =j*j*j;
cout <<setw(6)<<cube<<endl;
}
Output
1                       1
2                        8
3                       27
4                       64
5                       125
6                       216
7                       343
8                       512
9                       729
10                   1000



Example.4
#include <iostream.h>
void main(void)
{
unsigned   int numb;
unsigned  long fact=1;
cout <<”Enter a number:” ;
cin>>numb;
for(int j=numb; j>0; j--)
fact   =fact*j;
cout<<”factorial  is ”<<fact<<endl;
}
Output
Enter a number 10                                   //10 is the assume number in this example    
Factorial is 3628800
Example.5
#include <iostream.h>
void main(void)
{
int n=99;
while(n  !=0)
cin>>n;
cout <<endl;
}

Output
1
27
33
144
9
0


Example.6
#include <iostream.h>
void main(void)
{
int pow=1;
int numb=1;
while(pow<1000)
{
cout <<setw(2)<<numb;
cout<<setw(5)<<pow<<endl;
++numb;
power=numb*numb*numb*numb;
}
cout<<endl;
}


Output
1          1
2         16
3        81
4       256
5      625
6     1296
7    2401
8   4096
9  6561


Example.7
#include <iostream.h>
void main(void)
{
const   unsigned long  limit=4294967295;
unsigned  long next=6;
unsigned  long  last=1;
while(next <limit/2)
}
cout <<last<< “   “;
long sum=next+last;
next =last;
last= sum;
}
cout<<endl;
}
Output
See the book
Example.8
#include <iostream.h>
void main(void)
{
long dividend, divisor;
char  ch;
do
{
cout <<”Enter dividend:  “;cin>>dividend;
cout <<”Enter divisor: “;cin>>divisor;
cout <<”Quotient is“ <<dividend/divisor;
cout <<”.remainder  is “ << dividend % divisor;
cout  <<”\n Do an other? (y/n): “;
cin >> ch;
}
while (ch != ‘n’);
}
Output
See the book



Example.9
#include <iostream.h>
void main(void)
{
int x;
cout<<”Enter  a  numer:”;
cin>>x;
if(x>100)
cout<<”that number is greater than 100\n”;
}
Output
Enter a number:2000
That number is greater than 100

Example.10
#include <iostream.h>
void main(void)
{
int x;
unsigned long n,j;
cout <<”Enter a number:”;
cin >>n;
for (j=2;j<=n/2;j++)
if (n%j==0)
{
cout <<”It is not prime; divisible by “<<j<<endl;
exit(0);
}
cout <<”It is prime\n”;
}
Output
See the book

Example.11
#include <iostream.h>
void main(void)
{
int x;
cout<<”Enter  a  numer:”;
cin>>x;
if(x>100)
cout<<”that number is greater than 100\n”;
else
cout<<”that number is greater than 100\n”;
}
Output
See the book




Example.12
#include <iostream.h>
#include <conio.h>
void main(void)
{
int chcount=0;
int wdcount=1;
char ch= ‘a’;
cout <<”Enter a phrase :“;
while(ch!=’\r’)
{
ch= getche();
if (ch==’  ‘)
wdcount++;
}
cout”\nwords=”   <<wdcount<<endl;
<<”Letters=’’<<(chcount-1)<<endl;
}
Output
See the book

Example.13
#include <iostream.h>
#include <conio.h>

void main(void)
{
char dir=’a’;
int x=10,y=10;
cout<<”Type enter to quite\n”;
while(dir!= ‘\r’)
{
cout<<”\nyour location is “<<x<<”,”<<y;
cout <<”\npress directiion key (n,s,e,w):”;
dir=getche();
if(dir==’n’)
y--;
else
if(dir==’s’)
y++;
else
if(dir==’e’)
x++;
else
if(dir==”w’)
x--;
}
}
Output
See the book


Example.14
#include <iostream.h>
void main(void)
{
int a,b,c;
cout<<”Enter three numbers,a,b,c:\n;
cin>>a>>b>>c;
if(a==b)
if (b==c)
cout<<”a,b,c are the same \n”;
else
cout<<”a ,b are different \n”;
}
Output
See the book

Example.15
#include <iostream.h>
#include <conio.h>
void main(void)
{
int speed ;
cout<<”\nEnter33,45,or 78:”;
cin>>speed ;
switch(speed)
           {
    case 33;
           cout<<:LP album\n;
          break;
    case 45;
            cout<<”single  selection\n”;
            break;
           case 78;
          cout  <<”obsolete format\n”;
          break;
        }
}
Example.16
#include <iostream.h>
#include <conio.h>
void main(void)
{
char  dir=’a’;
int x=10,y= 10;
while (dir!=’\r’)
cout <<”\nyour location is “ <<x<<”,”<<y;
cout <<” \ nEnter direction (n,s,e,w):”;
dir=getche();
switch (dir)
{
case ‘n’: y--;break;
case ‘s’:y++;break;
case ‘e’:x++;break;
case’w’ :x--:break;
case’\r’:cout <<”Exiting \n”;break;
default:
          cout <<”try again\n”;
}
}
}
Example.17
#include <iostream.h>
#include <process.h>
#include <conio.h>
void main(void)
{
char  dir=’a’;
intx=10,y=10;
while (dir!=’\r’)
cout <<”\nyour location is “ <<x<<”,”<<y;
cout <<” \ nEnter direction (n,s,e,w):”;
dir=getche();
switch (dir)
{
case ‘n’: y--;break;
case ‘s’:y++;break;
case ‘e’:x++;break;
case’w’ :x--:break;
}
if(x==7&&y==11)
{
cout <<”\nyou found  the treasure!\n;
exit(0);
}
}
}
Example.18
#include <iostream.h>
#include <process.h>
#include <conio.h>
void main(void)
{
char  dir=’a’;
int x=10,y=10;

while (dir!=’\r’)
{
cout <<”\nyour location is “ <<x<<”,”<<y;
if(x<5llx>15)
cout <<”\nBeware:  dragons lurk  here”;
cout <<” \ nEnter direction (n,s,e,w):   ”;
dir=getche();
switch (dir)
{
case ‘n’: y--;break;
case ‘s’:y++;break;
case ‘e’:x++;break;
case’w’ :x--:break;
}
}
}
Example.19
#include <iostream.h>
#include <conio.h>
void main(void)
{
const unsigned char WHITE=219;
const unsigned char GRAY;
unsigned char ch;
for(int count =0;count<80*25-1; count++)
{
fh=WHITE;
For (int  j=2;  j<count;  j++)
if(count%j==0)
{
ch=GRAY;
break;
}
cout <<ch;
}
getch();
}
Example.20
#include <iostream.h>
void main(void)
{
long dividend ,divisor;
char ch;
do
{
cout<<”Enter dividend:”;cin>>  dividend;
cout <<”Enter divisor:  ”; cin >>divisor;
if (divisor==0)
{
cout<<”111egal divisor\n”;
continue ;
}
 cout<<”quotient is “<<dividend/divisor;
cout <<”remainder is “ << dividend %divisor;
cout <<”n\doanother?(y/n):  ”;
cin>>ch;
}
while (ch!=’n’);
}

c++ chap.2 exercise solution


CHAPTER NO.02
SOLUTIONS OF EXERCISE

Q. No.01:- Assuming there are 7.481 gallons in a cubic foot, write a program that asks the user to enter a number of gallons, and then displays the equivalent in cubic feet.
---------------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
void main(void)
   {
   clrscr();
   float galons,cubic_foot;
   cout<<"Enter the quantity in galons:";
   cin>> galons;
   cubic_foot=galons/7.481;
   cout<<"Quanity in cubic_foot:"<<cubic_foot;
    }
---------------------------------------------------------------------------------------------------------------------
Q. No.02:- Write a program that generates the following table:
1990 135
1991 7290
1992 11300
1993 16200
Use a single cout statement for all output.
---------------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
void main(void)
   {
   clrscr();
   cout<<"1990"<<” "<<"135"<<"\n"<<"1991"<<" "<<"7290"<<"\n"<<"1992"<<"  "<<"11300"<<"\n"<<"1993"<<" "<<"16200"<<"\n";
   }
---------------------------------------------------------------------------------------------------------------------
Q. No.03:- Write a program that generates the following output:
10
20
19
Use an integer constant for the 10, an arithmetic assignment operator to generate the 20,and a decrement operator to generate the 19.
---------------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
void main(void)
   {
   clrscr();
   int number=10,number1,number2;
   cout<<number<<"\n";
   number1=number+10;
   cout<<number1<<"\n";
   number2=number1-1;
   cout<<number2;
   }
---------------------------------------------------------------------------------------------------------------------
Q.04:- Write a program that displays your favorite poem. Use an appropriate escape sequence for the line breaks. If you don’t have a favorite poem, you can borrow this one by Ogden Nash:
Candy is dandy,
But liquor is quicker.
Q.No.05:- A library function, islower(), takes a single character (a letter) as an argument and returns a nonzero integer if the letter is lowercase, or zero if it is uppercase. This function requires the header file CTYPE.H. Write a program that allows the user to enter a letter, and then displays either zero or nonzero, depending on whether a lowercase or uppercase letter was entered. (See the SQRT program for clues.)
--------------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
void main(void)
 {
  char ch;
  cout<<"Enter the character:";
  cin>>ch;
  int a=islower(ch);
  cout<<a;
 }
------------------------------------------------------------------------------------------------------------------
Q. No.06:- On a certain day the British pound was equivalent to $1.487 U.S., the French franc was $0.172, the German deutschemark was $0.584, and the Japanese yen was $0.00955.Write a program that allows the user to enter an amount in dollars, and then displays this value converted to these four other monetary units.
--------------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
void main(void)
{
   clrscr();
   float British,French,German,Japanies,US;
    cout<<"enter your amount in US dollar..";
    cin>>US;
    British=US/1.487;
    French=US/1.72;
    German=US/.584;
    Japanese=US/.00955;
    cout<<British<<" "<<French<<"  "<<German<<"  "<<Japanese;
}
---------------------------------------------------------------------------------------------------------------------
Q.No.07:- You can convert temperature from degrees Celsius to degrees Fahrenheit by multiplying by 9/5 and adding 32. Write a program that allows the user to enter a floating-point number representing degrees Celsius, and then displays the corresponding degrees Fahrenheit.
---------------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
void main(void)
{
  clrscr();
  float Fahrenheit,Celsius;
  cout<<"enter the degree in Celsius:";
  cin>>Celsius;
  Fahrenheit =((Celsius+32)*5/9);
  cout<<"Degree in Fahrenheit:"<<Fahrenheit;
}
---------------------------------------------------------------------------------------------------------------------
Q.No.08:- When a value is smaller than a field specified with setw(), the unused locations are, by default, filled in with spaces. The manipulator setfill() takes a single character as an argument and causes this character to be substituted for spaces in the empty parts of a field. Rewrite the WIDTH program so that the characters on each line between the location name and the population number are filled in with periods instead of spaces, as in
Portcity.....2425785
---------------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main(void)
{
   clrscr();
   long population=2425785;
  cout<<"Portcity";
  char x='-';
  cout<<setw(12);
  cout<<setfill(x);
  cout<<population;
}
---------------------------------------------------------------------------------------------------------------------
Q.No.09:- If you have two fractions, a/b and c/d, their sum can be obtained from the formula
a       c       a*d + b*c
--- + ---  = --------------
b      d          b*d
For example, 1/4 plus 2/3 is
1       2      1*3 + 4*2   3 + 8    11
--- + --- = ----------- = ------- = ----
4       3      4*3            12          12
Write a program that encourages the user to enter two fractions, and then displays their
sum in fractional form. (You don’t need to reduce it to lowest terms.) The interaction
with the user might look like this:
Enter first fraction: 1/2
Enter second fraction: 2/5
Sum = 9/10
You can take advantage of the fact that the extraction operator (>>) can be chained to read in more than one quantity at once: cin >> a >> dummychar >> b;
---------------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
#include<conio.h>
void main(void)
  {
 clrscr();
 int a,b,c,d;
 char ch;
 int result,result1,result2;
 cout<<"Enter the first fraction:"<<"\n";
 cin>>a;cout<<"/"<<"\n";cin>>b;
 cout<<"Enter the first fraction:"<<"\n";
 cin>>c;cout<<"/"<<"\n";cin>>d;
 result=a*d;
 result1=b*c;
 result2=b*d;
 cout<<"Result:"<<result+result1<<"/"<<result2;
  }
---------------------------------------------------------------------------------------------------------------------
Q.No.10:- In the heyday of the British empire, Great Britain used a monetary system based on pounds, shillings, and pence. There were 20 shillings to a pound, and 12 pence to a shilling. The notation for this old system used the pound sign, £, and two decimal points, so that,  for example, £5.2.8 meant 5 pounds, 2 shillings, and 8 pence. (Pence is the plural of penny.) The new monetary system, introduced in the 1950s, consists of only pounds and pence, with 100 pence to a pound (like U.S. dollars and cents). We’ll call this new system decimal pounds. Thus £5.2.8 in the old notation is £5.13 in decimal pounds (actually £5.1333333). Write a program to convert the old pounds-shillings-pence format to decimal pounds. An example of the user’s interaction with the program would be
Enter pounds: 7
Enter shillings: 17
Enter pence: 9
Decimal pounds = £7.89
In most compilers you can use the decimal number 156 (hex character constant ‘\x9c’) to represent the pound sign (£). In some compilers, you can put the pound sign into your program directly by pasting it from the Windows Character Map accessory.
--------------------------------------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------------------------------------
Q.No.11:- By default, output is right-justified in its field. You can left-justify text output using the manipulator setiosflags (ios::left). (For now, don’t worry about what this new notation means.) Use this manipulator, along with setw(), to help generate the following output:
Last name     First name    Street    address    Town        State
---------------------------------------------------------------------------
Jones              Bernard       109        Pine Lane   Littletown MI
O’Brian         Coleen         42 E.      99th Ave.   Bigcity      NY
Wong            Harry          121-A     Alabama St. Lakeville  IL
--------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------
Q. No.12:- Write the inverse of Exercise 10, so that the user enters an amount in Great Britain’s new decimal-pounds notation (pounds and pence), and the program converts it to the old pounds-shillings-pence notation. An example of interaction with the program might be
Enter decimal pounds: 3.51
Equivalent in old notation = £3.10.2.
Make use of the fact that if you assign a floating-point value (say 12.34) to an integer variable, the decimal fraction (0.34) is lost; the integer value is simply 12. Use a cast to avoid a compiler warning. You can use statements like float decpounds; // input from user (new-style pounds) int pounds; // old-style (integer) pounds float decfrac; // decimal fraction (smaller than 1.0) pounds = static cast<int>(decpounds); // remove decimal fraction decfrac = decpounds - pounds; // regain decimal fraction You can then multiply decfrac by 20 to find shillings. A similar operation obtains pence.