Friday 1 July 2016

C++ Code for Digital Clock

C++ Code for Digital Clock

Description: C++ program that prints a Digital Clock on the console. The program first retrieves the time and then converts it into proper format and outputs it in GMT as well as in PST format.


Download C++ Code for Digital Clock



#include <iostream>
#include <time.h>
#include <Windows.h>

using namespace std;
int main()
{
     int hours, minutes, clockseconds, twelve_hour_format;
     char am_pm;

     while (1){

           //next two lines of the code will get the total number of seconds passed since January, 1, 1970 according to the Greenwich Meridian Time (GMT).

           time_t seconds;
           seconds = time(NULL);

           //as there are 60 seconds in a minute, hence we take modulus by 60 as there cannot be more than 60 seconds a minute.

           clockseconds = seconds % 60;

           //now we convert seconds into minutes by dividing seconds into 60. As there cannot be more than 60 minutes in an hour, therefore we take modulus by 60 to get values between 0-59

           minutes = (seconds / 60) % 60;

           //now we convert seconds into hours. As we know, that that there are 3600 seconds in 1 hour, so we convert the seconds into hours by dividing by 3600. Further, there cannot be more than 24 hours in a day so we take modulus to get values in between 0-23

           hours = (seconds / 3600) % 24;

           //conversion into 12-hour format

           if (hours>12){
                if ((hours % 24) != 0){
                     twelve_hour_format = hours - 12;
                     am_pm = 'P';
                }
           }
           else{
                twelve_hour_format = hours;
                am_pm = 'A';
           }

           //Displaying GMT Time

           cout << "DIGITAL CLOCK" << " " << endl;
           cout << endl << "GMT TIME" << endl;
           cout << endl << "24-HOUR FORMAT" << endl;
           cout << hours << ":" << minutes << ":" << clockseconds << endl << endl;
           cout << "12-HOUR FORMAT" << endl;
           cout << twelve_hour_format << ":" << minutes << ":" << clockseconds << " " << am_pm << endl << endl;

           //now we use this condition to print hours according to PAKISTAN STANDARD TIME (PST), which is 5 hours ahead of the GMT time. So, we will simply add 5 in the value of the GMT hours found out earlier and take modulus by 24 due to the same reason given above.

           hours = (hours + 5) % 24;

           //conversion into 12-hour format
           if (hours>12){
                if ((hours % 24) != 0){
                     twelve_hour_format = hours - 12;
                     am_pm = 'PM';
                }
           }
           else{
                twelve_hour_format = hours;
                am_pm = 'AM';
           }

           //Displaying PST Time

           cout << "PAKISTAN STANDARD TIME (PST)" << " " << endl;
           cout << endl << "24-HOUR FORMAT" << endl;
           cout << hours << ":" << minutes << ":" << clockseconds << endl << endl;
           cout << "12-HOUR FORMAT" << endl;
           cout << twelve_hour_format << ":" << minutes << ":" << clockseconds << " " << am_pm << endl;

           Sleep(1000);

           //We will clear the screen so that the last values are removed from screen before printing the new ones.
           system("cls");
     }
    
     return 0;
}


No comments

Post a Comment

Recent Posts