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
- Copy the code provided above into a C++ source file, such as
scale_recipe.cpp
. - Compile the program using a C++ compiler (e.g.,
g++ scale_recipe.cpp -o scale_recipe
). - Run the compiled program (e.g.,
./scale_recipe
on Unix/Linux orscale_recipe.exe
on Windows). - 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.