fork download
  1. #include <stdio.h>
  2.  
  3. // **************************************************
  4. // Function: calcDogToHumanYears
  5. //
  6. // Description:
  7. // Converts a dog's age in years to approximate human years.
  8. // The conversion is based on the following rules:
  9. // 1) First dog year = 15 human years
  10. // 2) Second dog year = 9 additional human years (total 24)
  11. // 3) Each additional dog year = 5 additional human years
  12. //
  13. // Parameters:
  14. // dogYear - dog's age in years
  15. //
  16. // Returns:
  17. // The equivalent human years as a float
  18. // **************************************************
  19. float calcDogToHumanYears(int dogYear)
  20. {
  21. float humanYears; /* result to return */
  22.  
  23. if (dogYear <= 0)
  24. {
  25. humanYears = 0.0;
  26. }
  27. else if (dogYear == 1)
  28. {
  29. humanYears = 15;
  30. }
  31. else if (dogYear == 2)
  32. {
  33. humanYears = 24;
  34. }
  35. else
  36. {
  37. humanYears = 24 + (dogYear - 2) * 5;
  38. }
  39.  
  40. return humanYears;
  41. }
  42.  
  43. // **************************************************
  44. // Function: main
  45. //
  46. // Description:
  47. // Tests the calcDogToHumanYears function with
  48. // several sample dog ages and prints the results.
  49. // **************************************************
  50. int main(void)
  51. {
  52. printf("Dog age: 1 -> Human years: %.1f\n", calcDogToHumanYears(1));
  53. printf("Dog age: 2 -> Human years: %.1f\n", calcDogToHumanYears(2));
  54. printf("Dog age: 3 -> Human years: %.1f\n", calcDogToHumanYears(3));
  55. printf("Dog age: 5 -> Human years: %.1f\n", calcDogToHumanYears(5));
  56. printf("Dog age: 0 -> Human years: %.1f\n", calcDogToHumanYears(0));
  57. return 0;
  58. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Dog age: 1  -> Human years: 15.0
Dog age: 2  -> Human years: 24.0
Dog age: 3  -> Human years: 29.0
Dog age: 5  -> Human years: 39.0
Dog age: 0  -> Human years: 0.0