fork download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. public static void Main()
  6. {
  7. double[] coins = {0.05,0.10,0.20,0.50,1.00,1.42,2.00};
  8.  
  9. Console.WriteLine("Change is as follows:");
  10.  
  11. for (int j = 0; j < coins.Length; j++)
  12. {
  13. var amount = coins[j];
  14. var dollars = Math.Floor(amount);
  15. var change = amount - dollars;
  16. var cents = 100*change;
  17.  
  18. string ds = dollars == 1 ? String.Empty : "s";
  19. string cs = cents == 1 ? String.Empty : "s";
  20.  
  21. if (amount >= 0 && amount < 1)
  22. {
  23. Console.WriteLine("{0} cents", cents);
  24. }
  25. else if (dollars >= 1 && cents == 0)
  26. {
  27. Console.WriteLine("{0} dollar{1}", dollars, ds);
  28. }
  29. else
  30. {
  31. Console.WriteLine("{0} dollar{1} and {2} cent{3}",
  32. dollars, ds, cents, cs);
  33. }
  34. }
  35. }
  36. }
  37.  
Success #stdin #stdout 0.04s 33928KB
stdin
Standard input is empty
stdout
Change is as follows:
5 cents
10 cents
20 cents
50 cents
1 dollar
1 dollar and 42 cents
2 dollars