48.2k views
3 votes
// Assume hot dogs come in packages of 10, and hot dog buns come in packages of 8.

// Write a program that calculates the number of packages of hot dogs and the

// number of packages of hot dog buns needed for a cookout, with the minimum amount

// of leftovers. The program should ask the user for the number of people attending

// the cookout. Assume each person will eat 1 hotdog.

//

// The program should display the following details:

//

// The minimum number of packages of hot dogs required

// The minimum number of packages of hot dog buns required

// The number of hot dogs that will be left over

// The number of hot dog buns that will be left over

//

// Hint: You might want to use the % operator in one of your calculation

//

function packagesNeeded(numberOfPeople, itemsPerPack) {

User Rakitin
by
4.3k points

2 Answers

1 vote

Final answer:

To calculate the needed packages of hot dogs and buns for a cookout with minimum leftovers, use a program that rounds up the division of attendees by the items per package using Math.ceil and then computes the leftovers with the modulus operator.

Step-by-step explanation:

Writing a program to calculate the minimum number of packages of hot dogs and packages of hot dog buns needed for a cookout involves using the modulus operator (%) to determine the leftovers and accommodate a given number of attendees. To determine the packages needed, the program must calculate how many hot dogs and buns are consumed by the attendees, and then figure out how many packages of each are required, ensuring there's the least amount of waste.

Here's how the function might look:

function packagesNeeded(numberOfPeople, hotDogsPerPack, bunsPerPack) {
var hotDogPackages = Math.ceil(numberOfPeople / hotDogsPerPack);
var bunPackages = Math.ceil(numberOfPeople / bunsPerPack);
var hotDogsLeftover = (hotDogPackages * hotDogsPerPack) - numberOfPeople;
var bunsLeftover = (bunPackages * bunsPerPack) - numberOfPeople;

console.log('Minimum number of hot dog packages required: ' + hotDogPackages);
console.log('Minimum number of hot dog bun packages required: ' + bunPackages);
console.log('Number of hot dogs left over: ' + hotDogsLeftover);
console.log('Number of hot dog buns left over: ' + bunsLeftover);
}

The key aspect here is using the Math.ceil function to round up to the nearest whole number to ensure there are enough hot dogs and buns for everyone, and then calculating the leftover items.

User Art Shendrik
by
4.2k points
4 votes

Answer:

I am writing the JavaScript program Let me know if you want the program in some other programming language.

function packagesNeeded(numberOfPeople, itemsPerPack)

{ return (numberOfPeople * itemsPerPack); }

var noOfPpl= prompt("Enter the number of people attending the cookout:");

var hotdogsNeeded = packagesNeeded(noOfPpl, 1)

var hotdog_pack = 10;

var bun_pack = 8;

hotdog_packs_required = Math.ceil(hotdogsNeeded / hotdog_pack);

console.log('The minimum number of packages of hot dogs required: ' + (hotdog_packs_required));

hotdogs_leftover = hotdog_packs_required * hotdog_pack - hotdogsNeeded;

console.log('The number of hot dog buns that will be left over: ' + (hotdogs_leftover));

bun_packs_required = Math.ceil(hotdogsNeeded / bun_pack);

console.log('The minimum number of packages of hot dog buns required: ' + (bun_packs_required));

bunPacksleftover = bun_packs_required * bun_pack - hotdogsNeeded;

console.log('The number of hot dog buns that will be left over: ' + (bunPacksleftover));

Step-by-step explanation:

The function packagesNeeded() takes two arguments numberOfPeople which contains the number of people attending the cookout and itemsPerPack contains 1 as each person will eat 1 hot dog.

Then the program prompts the user to enter the number of people attending the cookout and stores this number in noOfPpl. prompt() function is used to take input from the user.

hotdogsNeeded variable holds the result produced by packagesNeeded which is passed two arguments number of people attending cookout and 1. variable hotdog_pack = 10 and variable bun_pack = 8 as given that the hot dogs come in packages of 10, and hot dog buns come in packages of 8.

In order to find the hot dog packages required, the required hot dogs is divided by the pack of hot dog. Math.ceil() function is used here to round the result of this division up to the next largest integer.

In order to computer number of hot dogs that will be left over, the the hot dogs required is subtracted from the product of required hot dog packages and hot dog pack.

The same calculations and formulas are used to compute the minimum number of packages of hot dog buns required and number of hot dog buns that will be left over . The only difference is that it will be computed using bun_pack= 8 as the buns come in the pack of 8.

Output:

The minimum number of packages of hot dogs required: 1

The number of hot dog buns that will be left over: 0

The minimum number of packages of hot dog buns required: 2

The number of hot dog buns that will be left over: 6

The screenshot of the code along with its output is attached.

// Assume hot dogs come in packages of 10, and hot dog buns come in packages of 8. // Write-example-1
// Assume hot dogs come in packages of 10, and hot dog buns come in packages of 8. // Write-example-2
User Mikegross
by
3.6k points