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,
the loop should ask the user for the number of rooms on the floor and how many of them are occupied.
After all the iterations, the program should display how many rooms the hotel has, how many of them
are occupied, how many are unoccupied, and the percentage of rooms that are occupied. The
percentage may be calculated by dividing the number of rooms occupied by the number of rooms.
Input Validation: Do not accept a value less than 1 for the number of floors. Do not accept a number
less than 10 for the number of rooms on a floor.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h>
int main()
{
clrscr();
int roomsOnFloor = 0;
float totalRooms = 0.00;
int totalFloors = 0;
int occupiedRoomsOnFloor = 0;
float totalOccupiedRooms = 0.00;
float percentOccupied = 0.00;
cout << "Enter number of floors: ";
cin >> totalFloors;
while (totalFloors < 1)
{
cout << "Number of floors must be at least 1. Please re-enter ";
cin >> totalFloors;
}
for (int i = 1; i <= totalFloors; i++)
{
if (i != 13)
{
cout << "Enter the number of rooms on the floor ";
cin >> roomsOnFloor;
while (roomsOnFloor < 10)
{
cout << "Number of rooms on floor must be at least 10. Please re-enter ";
cin >> roomsOnFloor;
}
cout << "How many rooms are occupied? ";
cin >> occupiedRoomsOnFloor;
totalRooms += roomsOnFloor;
totalOccupiedRooms += occupiedRoomsOnFloor;
}
}
percentOccupied = (totalOccupiedRooms / totalRooms)*100;
clrscr();
cout << "The hotel has total of " << totalFloors << " floors\n";
cout << "The hotel has total of " << totalRooms << " rooms\n";
cout << "There are " << totalOccupiedRooms << " rooms occupied\n";
cout << "There are " << totalRooms - totalOccupiedRooms << " empty rooms\n";
cout << "Percentage of occupied rooms is " << setprecision(2) << percentOccupied << "%\n";
getch();
return 0;
}
Comments
Leave a comment