Advertisement
Shaun_B

Working with percentages in C

Jun 14th, 2013
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1. /**
  2.  * A quick demonstration of working out percentages using C
  3.  * will probably work on all other problem-orientated languages.
  4.  * Written with Code::Blocks, tested on Windows 7 and cmd.exe
  5.  *
  6.  * @Author:     Shaun B
  7.  * @Version:    1.0.1 - 2013-06-14
  8.  * @Todo:       Look into floating point guides and why
  9.  *              there are rounding errors
  10.  *
  11.  **/
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15.  
  16. /// Function prototypes:
  17. int main();
  18. float percent (float pc);
  19. float taxToAdd (float number, float percent);
  20.  
  21. /// Variables:
  22. float taxRate   = 20.00f;     // ie VAT, PAYE etc...
  23. float percentage = 0.00f;
  24. float taxToPay  = 0.00f;
  25. float priceWithTax = 0.00f;
  26.  
  27. int main()
  28. {
  29.     float originalPrice = 59.99f;
  30.     unsigned char pound = 156;
  31.  
  32.     percentage   = percent (taxRate);
  33.     taxToPay    = taxToAdd (originalPrice, percentage);
  34.     priceWithTax = originalPrice + taxToPay;
  35.  
  36.     printf("Tax rate set to: %.2f\%%\n", taxRate);
  37.     printf("Original price of goods less tax: %c%.2f\n", pound, originalPrice);
  38.     printf("Tax on goods: %c%.2f\n", pound, taxToPay);
  39.     printf("Total payable: %c%.2f\n", pound, priceWithTax);
  40.  
  41.     printf("Fin. Press any key.");
  42.     _getch();
  43.  
  44.     return 0;
  45. }
  46.  
  47. float percent (float pc)
  48. {
  49.     return (float)pc/100.00f;
  50. }
  51.  
  52. float taxToAdd (float price, float percent)
  53. {
  54.     return (float)price*percent;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement