Question #110900

You've been hired by Clock Towers Unlimited to write software to list each hour of a 24-hour day. Write a Visual C++ program to list the hours of the day using a for statement, Start at midnight (hour 0) and end at 11pm (hour 23). For each hour, also list which part of the day it is. Use an if statement, the hour of the day, and the following table to determine which part of the day it is.

Expert's answer

// the preprocessing directives

#include <iostream>              // cin, cout

#include <iomanip>               // setw(n)

using namespace std;

int main()

{
     int i;

     cout<<"Hour  Part of day"<<endl;

     cout<<"================="<<endl;

for(i=0;i<=23;i++)

   {

        cout<<setw(2)<<i<<":  ";

        if(i>=0 && i<=5)                             // Hour range:  0 - 5

              cout<< "Early moning"<<endl;           // Part of day: Early moning

         else if(i>=6 && i<=11)                       // Hour range:  6 - 11

              cout<< "Moning"<<endl;                   // Part of day: Moning

          else if(i>=12 && i<=17)                      // Hour range:  12 - 17

              cout<< "Afternoon"<<endl;                // Part of day: Afternoon

          else                                          // Hour range:  18 - 23

               cout<< "Evening"<<endl;                   // Part of day: Evening

}

   cout<<endl<<endl;

   return 0;
}




LATEST TUTORIALS
APPROVED BY CLIENTS