Exercise (1)

Chapter-2

no.2

Write a program that generate the following table:

1990                  135
1991                 7290
1992                11300
1993                16200

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
int main()
{
clrscr();
cout<<1990<<setw(8)<<135<<endl;
<<1991<<setw(8)<<7290<<endl;
<<1992<<setw(8)<<1130<<endl;
<<1993<<setw(8)<<16200<<endl;
getch();
return 0;
}

n၀.5

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 functin 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.

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
int main()
{
clrscr();
char letter;
int ans;
cout<<“Enter a letter: “;
cin>>letter;
ans=islower(letter);
cout<<“Answer is “<<ans<<endl;
getch( );
return 0;
}

Output is

no.8

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 fields. 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>
int main()
{
clrscr();
long pop1=2425785, pop2=47, pop3= 9761;
cout<<setw(8)<<“LOCATION”<<setw(12)<<setfill(‘.’)<<“POPULATION”<<endl; cout<<setw(8)<<“Protcity” <<setw(12)<<setfill(‘.’)<<pop1<<endl;
cout<<setw(8)<<“Hightown” <<setw(12)<<setfill(‘.’)<<pop2<<endl;
cout<<setw(8)<<“Lowville” <<setw(12)<<setfill(‘.’)<<“pop3<<endl;
getch( );
return 0;
}

no.10

In the heyday of the British empire. Great Britain used a monetary system based on pounds, shillings and pence. There was 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 with 100pence 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-shilling-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 compliers you can use the decimal number 156 (hex character constant ‘\x9c’ ) to represent the pound sign ( £ ). In some compliers, you can put the pound sign into your program directly by pasting it from the Windows Character Map accessary.

#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int pounds,shillings,pence;
float decp;
cout<<“Enter pounds: “;
cin>>pounds;
cout<<“Enter shillings: “;
cin>>shillings;
cout<<“Enter pence: “;
cin>>pence;
decp=pounds+(float)shillings/20+(float)pence/240;
cout<<“\n Decimal pounds= “<<‘\x9c'<<decp<<endl;
getch();
return 0;
}

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. 99 th Ave.   Bigcity     NY
Wong       Harry       121-A Alabama St.  Lakeville   IL

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
int main()
{
clrscr();
cout<<setiosflags(ios::left)<<setw(9)<<“Last Name”<<setw(3)<<“”
<<setw(10)<<“First Name”<<setw(3)<<“”
<<setw(17)<<“Street Address”<<setw(3)<<“”
<<setw(10)<<“Town”<<setw(3)<<“”
<<setw(5)<<“State”<<setw(3)<<“”<<endl;
cout<<setiosflags(ios::left)<<setw(9)<<“Jones”<<setw(3)<<“”
<<setw(10)<<“Bernard”<<setw(3)<<“”
<<setw(17)<<“109 Pine Lane”<<setw(3)<<“”
<<setw(10)<<“Littletown”<<setw(3)<<“”
<<setw(5)<<“MI”<<setw(3)<<“”<<endl;
cout<<setiosflags(ios::left)<<setw(9)<<“O’Brian”<<setw(3)<<“”
<<setw(10)<<“Coleen”<<setw(3)<<“”
<<setw(17)<<“42 E. 99th Ave.”<<setw(3)<<“”
<<setw(10)<<“Bigcity”<<setw(3)<<“”
<<setw(5)<<“NY”<<setw(3)<<“”<<endl;
cout<<setiosflags(ios::left)<<setw(9)<<“Wong”<<setw(3)<<“”
<<setw(10)<<“Harry”<<setw(3)<<“”
<<setw(17)<<“121-A Alabama St.”<<setw(3)<<“”
<<setw(10)<<“Lakeville”<<setw(3)<<“”
<<setw(5)<<“IL”<<setw(3)<<“”<<endl;
getch();
return 0;
}

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

#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
float decpound,decfrac;
int pounds,shilling,pence;
cout<<“Enter decimal pounds: “; cin>>decpound;
pounds=(int)decpound;
decfrac=decpound-pounds;
shilling=(int)(decfrac20); pence=(int)(((decfrac20)-shilling)*12);
cout<<“Eqivalent in old notation: “;
cout<<‘\x9c'<<pounds<<‘.'<<shilling<<‘.'<<pence<<endl;
getch();
return 0;
}

no.9

If you have two fractions a/b and c/d , their sum can be obtained from the formula
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 if 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

  a          c        a*d + b*c
.....   +  .....  = .............
  b          d           b*d
Design a site like this with WordPress.com
Get started