fork download
  1. #include <stdio.h>
  2.  
  3. void buildingCarbonFootprint(void)
  4. {
  5. double electricity;
  6. double gas;
  7. double footprint;
  8.  
  9. printf("\nBuilding carbon footprint\n");
  10.  
  11. printf("Enter electricity consumption in kWh: ");
  12. scanf("%lf", &electricity);
  13.  
  14. printf("Enter gas consumption in units: ");
  15. scanf("%lf", &gas);
  16.  
  17. footprint =
  18. electricity * 0.45 +
  19. gas * 2.2;
  20.  
  21. printf("The building carbon footprint is %.2f kg CO2\n",
  22. footprint);
  23. }
  24.  
  25. void carCarbonFootprint(void)
  26. {
  27. double distance;
  28. double fuelConsumption;
  29. double footprint;
  30.  
  31. printf("\nCar carbon footprint\n");
  32.  
  33. printf("Enter distance traveled in kilometers: ");
  34. scanf("%lf", &distance);
  35.  
  36. printf("Enter fuel consumption in liters: ");
  37. scanf("%lf", &fuelConsumption);
  38.  
  39. footprint = fuelConsumption * 2.31;
  40.  
  41. printf("The car carbon footprint is %.2f kg CO2\n",
  42. footprint);
  43.  
  44. printf("Distance traveled = %.2f km\n",
  45. distance);
  46. }
  47.  
  48. void bicycleCarbonFootprint(void)
  49. {
  50. double distance;
  51. double footprint;
  52.  
  53. printf("\nBicycle carbon footprint\n");
  54.  
  55. printf("Enter distance traveled in kilometers: ");
  56. scanf("%lf", &distance);
  57.  
  58. /*
  59.   The bicycle itself does not directly burn fuel,
  60.   so its direct carbon footprint is considered zero.
  61.   */
  62.  
  63. footprint = 0;
  64.  
  65. printf("The bicycle carbon footprint is %.2f kg CO2\n",
  66. footprint);
  67.  
  68. printf("Distance traveled = %.2f km\n",
  69. distance);
  70. }
  71.  
  72. int main(void)
  73. {
  74. void (*carbonFootprints[3])(void) =
  75. {
  76. buildingCarbonFootprint,
  77. carCarbonFootprint,
  78. bicycleCarbonFootprint
  79. };
  80.  
  81. int choice;
  82.  
  83. printf("Enter 0 to calculate building carbon footprint\n");
  84. printf("Enter 1 to calculate car carbon footprint\n");
  85. printf("Enter 2 to calculate bicycle carbon footprint\n");
  86.  
  87. scanf("%d", &choice);
  88.  
  89. if (choice < 0 || choice > 2)
  90. {
  91. printf("Invalid choice.\n");
  92. }
  93. else
  94. {
  95. (*carbonFootprints[choice])();
  96. }
  97.  
  98. return 0;
  99. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Enter 0 to calculate building carbon footprint
Enter 1 to calculate car carbon footprint
Enter 2 to calculate bicycle carbon footprint

Building carbon footprint
Enter electricity consumption in kWh: Enter gas consumption in units: The building carbon footprint is 0.00 kg CO2