The complete program code is given below :

 

 

# include <iostream.h>

 

main ( )

{

            double amount, discount, netPayable ;

            amount = 0 ;

            netPayable = 0 ;

            discount = 0 ;

            // prompt the user to enter the bill amount

            cout << "Please enter the amount of the bill     " ;

            cin >> amount ;

            //test the conditions and calculate net payable

           

            if ( amount > 5000 )

            {

                   //calculate amount at 15 % discount

                   discount = amount * (15.0 / 100);                 

                   netPayable = amount - discount;

                   cout << "The discount at the rate 15 % is Rupees     " << discount << endl;                                 cout << "The payable amount is Rupees    " << netPayable ;                     

            }

           else

            {

                   // calculate amount at 10 % discount

                  discount = amount * (10.0 / 100);                   

                  netPayable = amount - discount;

                  cout << "The discount at the rate 10 % is Rupees     " << discount << endl ;  

                  cout << "The payable amount is Rupees    " << netPayable ;                     

            }        

  

}

 

 

 

In the program we declared the variables as double. We do this to get the correct results (results may be in decimal points) of the calculations. Look at the statement which calculates the discount. The statement is

discount = amount * (15.0  / 100) ;

Here in the above statement we write 15.0 instead of 15. If we write here 15  then the division 15 / 100 will be evaluated as integer division and the result of division (0.15) will be truncated and we get 0 and this will result the whole calculation to zero. So it is necessary to write at least one operand in decimal form to get the correct result by division and we should also declare the variables as float or double. We do the same in the line discount = amount * (10.0 / 100);

 

A sample execution of the program is given below

 

 

 

 

Previous

 

 

TOC

 

 

Next