Function Pizza Slices

PHOTO EMBED

Tue Jun 11 2024 09:37:37 GMT+0000 (Coordinated Universal Time)

Saved by @meanaspotato #c #function

#include <stdio.h>

int calculate_pizza_share(int number_of_people);

int main(void)
{
    int number_of_people;

    // Prompt the user for input
    printf("How many people? \n");
    scanf("%d", &number_of_people);

    // Calculate the pizza share
    int slices_per_person = calculate_pizza_share(number_of_people);

    // Output the result
    if (slices_per_person == -1) 
    {
        printf("Error\n");
    } 

	else 
    {
        printf("%d people get %d slice(s) each.\n", number_of_people, slices_per_person);
    }

    return 0;
}

int calculate_pizza_share(int number_of_people)
{
    if (number_of_people <= 0) 
    {
        return -1; // Error for non-positive number of people
        
    }

    int total_slices = 8; // Total slices in a pizza

    return total_slices / number_of_people;
}
content_copyCOPY

Objective: Design a program that calculates the number of pizza slices each individual receives based on the number of participants. Guidelines: Function Declaration & Definition: Develop a function called calculate_pizza_share. This function should accept an integer parameter named number_of_people. Given a pizza has eight slices, the function should determine and return how many slices each participant will get by evenly distributing the pizza slices. Make sure the function is side-effect-free: It shouldn't produce any display nor capture user input. Main Program Execution: In the main() function: Display a message asking the user to specify the number of participants. Capture the user's input. Invoke the calculate_pizza_share function with the user's input as the argument. Present the resulting output obtained from the calculate_pizza_share function. Code Presentation: The code should be clean and systematically organized. Ensure proper indentation, spacing, and the selection of intuitive variable names. Use meaningful names for variables. Expected Outputs: "How many people? 2" → Output: "2 people get 4 slice(s) each." "How many people? 4" → Output: "4 people get 2 slice(s) each." "How many people? 6" → Output: "6 people get 1 slice(s) each." "How many people? 8" → Output: "8 people get 1 slice(s) each." Testing: Run your program using a range of inputs to confirm its accuracy and reliability. Ensure consistent outputs as outlined above. For example: Input Result 2 How many people? 2 people get 4 slice(s) each.