fork(1) download
  1. #include <iostream>
  2. #include <iomanip> // For std::setprecision
  3. #include <cmath> // For std::M_PI
  4.  
  5. int main() {
  6. // --- Step A: Ask the user for the diameter of the pizza ---
  7. double diameter;
  8. std::cout << "Enter the diameter of the pizza in inches: ";
  9. std::cin >> diameter;
  10.  
  11. // --- Constants and Intermediate Calculations ---
  12. const double AREA_PER_SLICE = 14.125; // Area for each slice in square inches [1]
  13. const double PI = M_PI; // Use the more precise value of PI from cmath [2]
  14. double radius = diameter / 2.0; // Calculate the radius from the diameter [2]
  15. double pizzaArea = PI * radius * radius; // Calculate the total pizza area [2]
  16.  
  17. // --- Step B: Calculate the number of slices ---
  18. // Divide the total pizza area by the area of one slice [1]
  19. double numberOfSlices = pizzaArea / AREA_PER_SLICE;
  20.  
  21. // --- Step C: Display the number of slices ---
  22. // Use static_cast to convert to an integer (whole number of slices) [2]
  23. // Use std::fixed and std::setprecision to control output format [2]
  24. std::cout << "The " << diameter << "-inch pizza can be cut into "
  25. << static_cast<int>(numberOfSlices) << " slices." << std::endl;
  26.  
  27. return 0;
  28. }
  29.  
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
Enter the diameter of the pizza in inches: The 6.95265e-310-inch pizza can be cut into 0 slices.