Problem of the Day
A new programming or logic puzzle every Mon-Fri

Pizza Calculator

The final day of June. We've come so far!

Today's problem will prove super useful when ordering pizza. Write a function that takes in an int (diameter of a circle) and a float (price) and determine the cost per unit of area (price/area). Next time you order a pizza you can plug in the sizes and prices and determine which is the better buy.

Permalink: http://problemotd.com/problem/pizza-calculator/

Comments:

  • Emre Ozdemir - 10 years, 6 months ago

    #include <iostream>                               //Solution in C++
    #define PI 3.14159265359                   //To see which pizza is better, you need to try more than once in the same window 
    using namespace std;
    
    void checkCostpArea(int varDia, float varPrice){
        double area = PI * varDia * varDia; 
        cout<<endl<<"You will pay "<< varPrice << "$ for "<< area <<" cm^2 pizza."<<endl;
        cout<<"And the Price/Area ratio is: "<< varPrice/area<<endl;
    };
    
    int main (){
        int diameter;
        double loop=1.0;
        float price;
    
        while(loop!=0){
            for(int i=1;i<=30;i++)      //per try divider
                cout<<"-";
            puts("\n");
        cout<<"Enter the pizza diameter: "; //this will be in centimeters
        cin>>diameter;
    
        cout<<"Enter the price: ";  //this will be in dollars
        cin>>price;
    
        checkCostpArea(diameter,price);
    
        cout<<endl<<"Type 0 to exit, any number to go again.";
        cin>>loop;
        }
        return 0;
    }
    

    reply permalink

  • Anonymous - 10 years, 6 months ago

    #python 2.7
    size = float(raw_input("What's the diameter of the pizza?> "))
    price = float(raw_input("How much is the pizza?> $"))
    
    square_inches = 3.14159 * (price/2.0)**(2.0)
    
    print "Price per square inch = $%f" % (price/square_inches)
    

    reply permalink

  • Anonymous - 10 years, 6 months ago

    Oops. That should be: square_inches = 3.14159 * (size/2.0)**(2.0)

    size not price :/

    reply permalink

  • Anonymous - 10 years, 5 months ago

    Haskell solution

    main :: Int -> Float -> String
    main i f = pizza (toEnum (quot i 2)) f
    
    pizza :: Float -> Float -> String
    pizza z s = "Price per cm^2: " ++ show price
        where
            a = z * z * pi
            price = s / a
    

    reply permalink

  • bumbleguppy - 10 years, 5 months ago

    javascript

      function pizzaValue(diameter, price)
      {
            return '$' + (price / (((diameter / 2) ^ 2) * Math.PI)).toFixed(2).toString() + ' per square inch';
      }
    
      console.log(pizzaValue(10, 12.99));
    

    reply permalink

Content curated by @MaxBurstein