Introduction

Sometimes, you need to adjust a recipe to serve more or fewer people. This C++ program allows you to scale a recipe’s ingredients according to the desired number of servings. You simply input the original number of servings, the new number of servings, and the program will adjust the quantities of each ingredient accordingly. This is useful for cooking larger meals or reducing a recipe to fit smaller gatherings.

Objective

The objective of this program is to provide an easy-to-use solution for scaling a recipe based on the number of servings. By taking the original quantities and adjusting them according to a user-defined scaling factor, this program ensures you have the right amount of ingredients for any number of servings.

C++ Code: Scale Recipe Based on Servings

#include 
using namespace std;

int main() {
    // Declare variables for ingredients and servings
    double original_servings, new_servings;
    double ingredient_1, ingredient_2, ingredient_3; // Example ingredients

    // Get input from user for the original servings and new servings
    cout << "Enter the number of servings in the original recipe: "; cin >> original_servings;

    cout << "Enter the number of servings you want to scale the recipe to: "; cin >> new_servings;

    // Example ingredients in the original recipe for 1 serving
    cout << "Enter the amount of ingredient 1 for 1 serving: "; cin >> ingredient_1;

    cout << "Enter the amount of ingredient 2 for 1 serving: "; cin >> ingredient_2;

    cout << "Enter the amount of ingredient 3 for 1 serving: "; cin >> ingredient_3;

    // Calculate the scaling factor
    double scale_factor = new_servings / original_servings;

    // Scale the ingredients
    ingredient_1 *= scale_factor;
    ingredient_2 *= scale_factor;
    ingredient_3 *= scale_factor;

    // Output the scaled ingredients
    cout << "\nScaled recipe for " << new_servings << " servings:" << endl;
    cout << "Ingredient 1: " << ingredient_1 << endl;
    cout << "Ingredient 2: " << ingredient_2 << endl;
    cout << "Ingredient 3: " << ingredient_3 << endl;

    return 0;
}

Program Structure and Explanation

This C++ program is designed to scale a recipe based on the number of servings input by the user. Here’s a breakdown of the program’s structure:

  • Input Section: The program first prompts the user to enter the number of servings in the original recipe and the number of servings they want. Additionally, the user is asked to input the quantities of three ingredients in the original recipe.
  • Scaling Factor: The program calculates the scaling factor by dividing the desired servings by the original servings.
  • Scaling Ingredients: The program multiplies each ingredient by the scaling factor to adjust the quantities.
  • Output Section: Finally, the program displays the scaled quantities for each ingredient based on the new number of servings.

How to Run the Program

  1. Copy the code provided above into a C++ source file, such as scale_recipe.cpp.
  2. Compile the program using a C++ compiler (e.g., g++ scale_recipe.cpp -o scale_recipe).
  3. Run the compiled program (e.g., ./scale_recipe on Unix/Linux or scale_recipe.exe on Windows).
  4. Follow the on-screen prompts to input the original servings, new servings, and ingredient quantities. The program will output the adjusted quantities for each ingredient.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)