Java

 

Introduction

Scaling a recipe allows you to adjust ingredient quantities based on the number of servings needed. Whether you are cooking for a large group or just a few people, scaling recipes ensures you use the correct amount of each ingredient. In this guide, we will learn how to write a Java program that scales a recipe’s ingredient quantities based on the number of servings.

Objective

The objective of this program is to create a Java application that can take an original recipe, including ingredient quantities and the number of servings, and adjust it to meet the desired number of servings. This program will allow users to input the original servings and their desired servings, and it will automatically calculate the required ingredient amounts for the new servings.

Java Code to Scale a Recipe

import java.util.Scanner;

public class RecipeScaler {
    public static void main(String[] args) {
        // Create scanner object for input
        Scanner scanner = new Scanner(System.in);

        // Original recipe details
        double originalServings = 4; // You can change this based on your original recipe
        double ingredientAmount = 2.0; // Example ingredient (in cups)

        // Input desired number of servings
        System.out.print("Enter the number of servings you want: ");
        double desiredServings = scanner.nextDouble();

        // Calculate the scaling factor
        double scalingFactor = desiredServings / originalServings;

        // Scale ingredient amounts
        double scaledIngredientAmount = ingredientAmount * scalingFactor;

        // Output the scaled ingredient amount
        System.out.println("To make " + desiredServings + " servings, you need " + scaledIngredientAmount + " cups of ingredient.");
        
        // Close the scanner
        scanner.close();
    }
}

Explanation of the Program

The program begins by defining the original number of servings and the amount of an ingredient (in this case, 2 cups of an ingredient for 4 servings). It then prompts the user to input the desired number of servings, calculates the scaling factor based on the ratio of desired servings to original servings, and uses that factor to scale the ingredient amount. Finally, it prints out the adjusted ingredient quantity.

How to Run the Program

  1. Copy the code into a text editor and save it as RecipeScaler.java.
  2. Open a terminal or command prompt.
  3. Navigate to the directory where you saved the file.
  4. Compile the Java program by typing: javac RecipeScaler.java.
  5. Run the compiled program by typing: java RecipeScaler.
  6. The program will prompt you to enter the desired number of servings, and it will output the scaled ingredient amount.
© 2025 Learn Programming

 

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 :)