fork download
  1. // Renee V. Lopez CS1A Chapter 3 P.146 #17
  2. /******************************************************************************
  3.  * Calculate Pizza Slices
  4.  * _____________________________________________________________________________
  5.  * This program calculates the number of slices a pizza can be divided into,
  6.  * based on its diameter. Each slice should have an area of 14.125 square inches.
  7.  *
  8.  * _____________________________________________________________________________
  9.  * INPUT
  10.  * diameter : The diameter of the pizza in inches
  11.  * _____________________________________________________________________________
  12.  * OUTPUT
  13.  * slices : Number of slices the pizza can be divided into
  14.  * ****************************************************************************/
  15. #include <iostream>
  16. #include <iomanip> // For setting output precision
  17.  
  18. using namespace std;
  19.  
  20. int main() {
  21. // Define constants
  22. const double PI = 3.14159; // Named constant for PI
  23. const double SLICE_AREA = 14.125; // Area of each pizza slice in square inches
  24.  
  25. // Declare variables
  26. double diameter; // Diameter of the pizza
  27. double radius; // Radius of the pizza
  28. double area; // Area of the pizza
  29. double slices; // Number of slices
  30.  
  31. // Prompt user for diameter of the pizza
  32. cout << "Enter the diameter of the pizza in inches: ";
  33. cin >> diameter;
  34.  
  35. // Calculate the radius of the pizza
  36. radius = diameter / 2.0;
  37.  
  38. // Calculate the area of the pizza
  39. area = PI * radius * radius;
  40.  
  41. // Calculate the number of slices
  42. slices = area / SLICE_AREA;
  43.  
  44. // Display the number of slices, rounded to one decimal place
  45. cout << fixed << setprecision(1);
  46. cout << "Number of slices: " << slices << endl;
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0s 5284KB
stdin
34
stdout
Enter the diameter of the pizza in inches: Number of slices: 64.3