Introduction
When you’re cooking, it can be useful to adjust the quantity of ingredients in a recipe depending on how many servings you need to make. Scaling a recipe is a common task that allows you to easily increase or decrease ingredient amounts. This is especially helpful when you’re cooking for a larger or smaller group. In this tutorial, we will write a program in C that will allow users to scale a recipe up or down based on the number of servings they need.
Objective
The objective of this C program is to take a basic recipe with known ingredients and adjust the quantities of those ingredients according to the desired number of servings. Users can enter the desired number of servings, and the program will calculate the correct amount of each ingredient.
Code
#include int main() { // Declare variables int original_servings, desired_servings; float ingredient1, ingredient2, ingredient3; // Input the number of servings for the original recipe printf("Enter the number of servings in the original recipe: "); scanf("%d", &original_servings); // Input the number of servings desired printf("Enter the desired number of servings: "); scanf("%d", &desired_servings); // Input ingredient amounts for the original recipe (e.g., cups or grams) printf("Enter the amount of ingredient 1 (e.g., cups, grams): "); scanf("%f", &ingredient1); printf("Enter the amount of ingredient 2: "); scanf("%f", &ingredient2); printf("Enter the amount of ingredient 3: "); scanf("%f", &ingredient3); // Scale the ingredients based on the desired servings ingredient1 = ingredient1 * (float)desired_servings / original_servings; ingredient2 = ingredient2 * (float)desired_servings / original_servings; ingredient3 = ingredient3 * (float)desired_servings / original_servings; // Output the scaled ingredients printf("\nScaled Ingredients for %d servings:\n", desired_servings); printf("Ingredient 1: %.2f\n", ingredient1); printf("Ingredient 2: %.2f\n", ingredient2); printf("Ingredient 3: %.2f\n", ingredient3); return 0; }
Explanation of the Program
The program starts by declaring variables to hold the number of servings, the ingredients’ quantities, and the scaling factors. Here’s the structure:
- Input the original servings: The user is asked to enter the number of servings the recipe originally makes.
- Input the desired servings: The user specifies how many servings they want to prepare.
- Ingredient input: The program then asks for the amounts of each ingredient in the original recipe.
- Scaling: The program calculates the new amount of each ingredient by multiplying the original amount by the ratio of the desired servings to the original servings.
- Output: Finally, the scaled ingredient amounts are displayed to the user, providing the correct quantities based on the desired servings.
How to Run the Program
To run this program, follow these steps:
- Open your C programming environment or text editor (like Code::Blocks, Visual Studio, or any IDE with C support).
- Create a new C file (e.g.,
scale_recipe.c
). - Copy and paste the provided code into your file.
- Compile the program by clicking the ‘Run’ or ‘Build’ button in your IDE, or use the command line with a compiler like GCC:
gcc scale_recipe.c -o scale_recipe
. - Run the compiled program by typing
./scale_recipe
on the command line or directly running it from the IDE. - Enter the original servings, the desired servings, and ingredient amounts when prompted, and the program will output the scaled ingredients.