RAVIANS COLLEGE OF SCIENCE PATTOKI: Question no. 12

Question no. 12

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

Answer By WAJID

#include
#include
//Old programme:
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!1 pound = 20 shilling
!1 shilling = 12 pence
!so 1 pound = (20*12) 240 pence [in old system]
!also 1 pound = 100 pence [in new system]
!hence the transformation formula is :
!new_pence = (old_pence*100)/240
---------------------------------------------------------------------
!number of pence{old_pence} = (input_shillings*12) + input_pence
!New: using the inverse function.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void main(void)
{
float decpounds; // input from user (new-style pounds)
int pounds, shillings; // old-style (integer) pounds & shillings
float decfrac; // decimal fraction (smaller than 1.0)

do{
cout<<"Enter decimal pounds: "; //cin >>pounds>>var_char>>decfrac; //I think this way is better!
cin >>decpounds;
pounds = static_cast(decpounds); // remove decimal fraction
//user should enter valid data , pence entred smaller than 100.
decfrac = 240*(decpounds - pounds); // regain decimal fraction
shillings = (static_cast(decfrac))%12; //Ignore fracions in pence.
decfrac = static_cast((decfrac-shillings)/12); //Ignore fracions in pence.
cout<<"Equivalent in old notation = \x9c"< cout<<"\n !Press c to continue or any key to exit."< }while(getch()=='c');
}

BEST OF LUCK

No comments:

Post a Comment

Copyright © RAVIANS COLLEGE OF SCIENCE PATTOKI Urang-kurai