128k views
15 votes
Write a program that calculates the occupancy rate for a hotel. The program should start by asking the user how many floors the hotel has. A for loop should then iterate once for each floor. In each iteration of the for loop, the program should ask the user for the number of rooms of the floor and how many of them are occupied. After all of the iterations are complete the program should display how many rooms the hotel has, how many of them are occupied, and the percentage of rooms that are occupied.

User Shreyansp
by
4.4k points

1 Answer

9 votes

Answer:

In Python:

floor = int(input("Number of floors: "))

totalrooms = 0

totaloccupied = 0

for i in range(floor):

rooms = int(input("Rooms in floor "+str(i+1)+": "))

occupied = int(input("Occupied Rooms in floor "+str(i+1)+": "))

totalrooms = totalrooms + rooms

totaloccupied = totaloccupied + occupied

print("Total Rooms: "+str(totalrooms))

print("Total Occupied: "+str(totaloccupied))

print("Percentage Occupied: "+str(round(100*totaloccupied/totalrooms,2))+"%")

Step-by-step explanation:

This line prompts the user for number of rooms

floor = int(input("Number of floors: "))

This line initializes totalrooms to 0

totalrooms = 0

This line initializes totaloccupied to 0

totaloccupied = 0

This iterates through the floors

for i in range(floor):

This gets the number of rooms in each floor

rooms = int(input("Rooms in floor "+str(i+1)+": "))

This gets the number of occupied rooms in each floor

occupied = int(input("Occupied Rooms in floor "+str(i+1)+": "))

This calculates the total number of rooms

totalrooms = totalrooms + rooms

This calculates the total number of occupied rooms

totaloccupied = totaloccupied + occupied

This prints the total number of rooms

print("Total Rooms: "+str(totalrooms))

This prints the total number of occupied rooms

print("Total Occupied: "+str(totaloccupied))

This prints the percentage of occupied rooms to 2 decimal places

print("Percentage Occupied: "+str(round(100*totaloccupied/totalrooms,2))+"%")

User Kiran Kumar Kotari
by
4.4k points