Write a C++ program which will prompt the user for an int representing hours. This value will be in the range 0 - 167 inclusive. Your program should then display a day and time (hours only) followed by am, pm, midnight, or noon. Assume time begins at midnight 12am Monday. Finally, your program will output \'PROGRAM ENDS\' (the single quotes are not displayed), followed by a single newline. Then, your program terminates
Solution
#include <iostream>
#include <string>
using namespace std;
int main()
{int hours, day,ap;
cout<<\"Enter hour between 0 and 167: \";
cin>>hours;
cout<<\"hours is \";
day=hours/24;
hours=hours%24;
if(day==0)
cout<<\"Monday \";
else if(day==1)
cout<<\"Tuesday \";
else if(day==2)
cout<<\"Wednesday \";
else if(day==3)
cout<<\"Thursday \";
else if(day==4)
cout<<\"Friday \";
else if(day==5)
cout<<\"Saturday \";
else
cout<<\"Sunday \";
if(hours>=12)
{ap=1;
if(hours>=13)
hours-=12;
}
else
ap=0;
if(hours==0)
cout<<\"12\";
else
cout<<hours;
if(ap==0 )
if(hours==0)
cout<<\"midnight\ \";
else
cout<<\"am\ \";
else
if(hours==12)
cout<<\"noon\ \";
else
cout<<\"pm\ \";
cout<<\"PROGRAM ENDS\ \";
system(\"pause\");
return 0;
}
.