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:

  1. Open your C programming environment or text editor (like Code::Blocks, Visual Studio, or any IDE with C support).
  2. Create a new C file (e.g., scale_recipe.c).
  3. Copy and paste the provided code into your file.
  4. 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.
  5. Run the compiled program by typing ./scale_recipe on the command line or directly running it from the IDE.
  6. Enter the original servings, the desired servings, and ingredient amounts when prompted, and the program will output the scaled ingredients.
© 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 :)