In: Computer Science
#include <iostream>
using namespace std;
int main()
{
int hour;
int min;
for (hour = 1; hour <= 12; hour++)
{
for (min = 0; min <= 59; min++)
{
cout << hour << ":" << min << "AM" << endl;
}
}
return 0;
}
1. Type in the above program as time.cpp. Add a comment to include your name and date.
Compile and run.
2. What is the bug or logic error in the above program? Add the lines to fix it.
3. This is an example of ____________ for loops.
4. How many total times does the outer loop execute?
5. How many total times does the inner loop execute?
6. Turn in a copy of the final program and output – just the first and last hours are sufficient
2) In the program there is a logical error it prints 12:00 Am after 11:59 Am but after 11:59 Am 12:00 pm comes
so i added if condition.
Code:
#include <iostream>
using namespace std;
int main()
{
int hour;
int min;
for (hour = 1; hour <= 12; hour++)
{
for (min = 0; min <=
59; min++)
{
if(hour!=12){
cout << hour << ":" << min
<< "AM" << endl;
}
else
{
cout << hour << ":" << min
<< "PM" << endl;
}
}
}
return 0;
}
3)This is a example of nested for loops.
4) 12 times outer loop executes.
5)60 times inner loop execute
6)output for first hour:
Output for last hour: