fork(1) download
  1. // Description: Calculates the total value in euros of a jar
  2. // containing various euro coins.
  3. //
  4. // Euro coin denominations:
  5. // 2 euro, 1 euro,
  6. // 50 cent, 20 cent, 10 cent, 5 cent, 2 cent, 1 cent
  7. //
  8. //**************************************************************
  9.  
  10. #include <stdio.h>
  11.  
  12. // Conversion constants (all values in euro cents)
  13. #define TWO_EURO 200
  14. #define ONE_EURO 100
  15. #define FIFTY_CENT 50
  16. #define TWENTY_CENT 20
  17. #define TEN_CENT 10
  18. #define FIVE_CENT 5
  19. #define TWO_CENT 2
  20. #define ONE_CENT 1
  21.  
  22. #define CENTS_PER_EURO 100.0 // used to convert cents to euros
  23.  
  24. //**************************************************************
  25. // Function: calcEuros
  26. //
  27. // Purpose: Calculates the total value in euros given the count
  28. // of each euro coin denomination in a jar.
  29. //
  30. // Parameters:
  31. //
  32. // twoEuro - number of 2 euro coins
  33. // oneEuro - number of 1 euro coins
  34. // fiftyCent - number of 50 euro cent coins
  35. // twentyCent - number of 20 euro cent coins
  36. // tenCent - number of 10 euro cent coins
  37. // fiveCent - number of 5 euro cent coins
  38. // twoCent - number of 2 euro cent coins
  39. // oneCent - number of 1 euro cent coins
  40. //
  41. // Returns:
  42. //
  43. // totalEuros - the total value of all coins in euros (float)
  44. //
  45. //**************************************************************
  46.  
  47. float calcEuros (int twoEuro, int oneEuro,
  48. int fiftyCent, int twentyCent,
  49. int tenCent, int fiveCent,
  50. int twoCent, int oneCent)
  51. {
  52. int totalCents; // running total in euro cents
  53.  
  54. // sum up the value of all coins in euro cents
  55. totalCents = (twoEuro * TWO_EURO) +
  56. (oneEuro * ONE_EURO) +
  57. (fiftyCent * FIFTY_CENT) +
  58. (twentyCent * TWENTY_CENT) +
  59. (tenCent * TEN_CENT) +
  60. (fiveCent * FIVE_CENT) +
  61. (twoCent * TWO_CENT) +
  62. (oneCent * ONE_CENT);
  63.  
  64. // convert cents to euros and return
  65. return (totalCents / CENTS_PER_EURO);
  66.  
  67. } // calcEuros
  68.  
  69.  
  70. // main function to test calcEuros
  71. int main ()
  72. {
  73. // coin counts for the test case
  74. int twoEuro = 5;
  75. int oneEuro = 2;
  76. int fiftyCent = 2;
  77. int twentyCent = 5;
  78. int tenCent = 15;
  79. int fiveCent = 0;
  80. int twoCent = 0;
  81. int oneCent = 2;
  82.  
  83. float totalEuros; // result from calcEuros
  84.  
  85. // call the function to calculate total euros
  86. totalEuros = calcEuros(twoEuro, oneEuro,
  87. fiftyCent, twentyCent,
  88. tenCent, fiveCent,
  89. twoCent, oneCent);
  90.  
  91. // print the coin counts
  92. printf("\n--- Coins in the Jar ---\n");
  93. printf(" 2 Euro coins : %d\n", twoEuro);
  94. printf(" 1 Euro coins : %d\n", oneEuro);
  95. printf(" 50 Cent coins : %d\n", fiftyCent);
  96. printf(" 20 Cent coins : %d\n", twentyCent);
  97. printf(" 10 Cent coins : %d\n", tenCent);
  98. printf(" 5 Cent coins : %d\n", fiveCent);
  99. printf(" 2 Cent coins : %d\n", twoCent);
  100. printf(" 1 Cent coins : %d\n", oneCent);
  101.  
  102. // print the result
  103. printf("\nTotal Value: %.2f Euros\n", totalEuros);
  104.  
  105. return 0;
  106.  
  107. } // main
  108.  
Success #stdin #stdout 0s 5320KB
stdin
2 euro, 1 euro, 50 cent, 20 cent, 10 cent, 5 cent, 2 cent, 1 cent
stdout
--- Coins in the Jar ---
  2 Euro coins    : 5
  1 Euro coins    : 2
 50 Cent coins    : 2
 20 Cent coins    : 5
 10 Cent coins    : 15
  5 Cent coins    : 0
  2 Cent coins    : 0
  1 Cent coins    : 2

Total Value: 15.52 Euros