You are on page 1of 4

Write a C program of this case.

Professor Nadech has scheduled a makeup class for the CS102 course in the evening. He thinks that if
the makeup class is scheduled in the evening, students might be hungry because they have been
studying all day.

Therefore, he consulted another instructor, Professor Yanya, expressing a desire to order pizza and
Pepsi to treat the students during the makeup class. However, due to Professor Nadech's limited
budget, he wants to calculate in advance how many students he can afford to treat with pizza.

Based on the information found on the internet, a pizza costs 599 baht per tray, which contains 8 slices.
Professor Yanya wants to help write a program to calculate the pizza orders. The program will receive
the budget as input and calculate the number of students who can be treated to pizza. The program
should ensure that each student gets to have at least 2 slices of pizza. Finally, the program should
display the remaining budget.

For example, if the input budget is 1000 baht, the program should indicate that it can support 4
students, leaving 401 baht.

Example run 1 (Input highlighted in yellow):

Please enter a budget amount: 500

We can provide support for 0 students.

The amount of budget remains 500 bahts.

Example run 2 (Input highlighted in yellow):

Please enter a budget amount: 5000

We can provide support for 32 students.

The amount of budget remains 208 bahts.

Example run 3 (Input highlighted in yellow):

Please enter a budget amount: 10000

We can provide support for 64 students.

The amount of budget remains 416 bahts.


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

The whole code

#include <stdio.h>

int main() {

int budget, costPerPizza = 599, slicesPerPizza = 8, slicesPerStudent = 2;

printf("Please enter a budget amount: ");

scanf("%d", &budget);

int students = (budget / costPerPizza) * slicesPerPizza / slicesPerStudent;

int remainingBudget = budget % costPerPizza;

printf("We can provide support for %d students.\n", students);

printf("The amount of budget remains %d bahts.\n", remainingBudget);

return 0;

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Explaination

1. Include Header Files:

#include <stdio.h>
This line includes the standard input-output library, which is necessary for functions like `printf` and
`scanf`.

2. Main Function:

int main() {

The program begins execution from the `main` function.

3. Variable Declaration:

int budget, costPerPizza = 599, slicesPerPizza = 8, slicesPerStudent = 2;

Here, we declare variables to store the budget amount, cost per pizza, slices per pizza, and slices per
student. The cost and slices information is provided in the problem statement.

4. Input:

printf("Please enter a budget amount: ");

scanf("%d", &budget);

The program prompts the user to enter a budget amount and reads the input using `scanf`.

5. Calculation:

int students = (budget / costPerPizza) * slicesPerPizza / slicesPerStudent;


int remainingBudget = budget % costPerPizza;

The program calculates the number of students who can be supported and the remaining budget after
purchasing pizzas.

6. Output:

printf("We can provide support for %d students.\n", students);

printf("The amount of budget remains %d bahts.\n", remainingBudget);

Finally, the program displays the number of students who can be supported and the remaining budget
using `printf`.

7. Return Statement:

return 0;

The program returns 0 to indicate successful execution to the operating system.

This program is a basic implementation to calculate the number of students who can be supported with
pizza given a budget. Note that it assumes a simple scenario without considering factors like rounding
slices or providing fractional slices to students. valid.

You might also like