Write a C program to design a digital clock

By | 03.12.2016

C program to design a digital clock


Write a C program to design a digital clock.This program will generate a digital clock using c program. The logic behind to implement this program –

  • Initialize hour, minute, seconds with 0.

  • Run an infinite loop.

  • Increase second and check if it is equal to 60 then increase minute and reset second to 0.

  • Increase minute and check if it is equal to 60 then increase hour and reset minute to 0.

  • Increase hour and check if it is equal to 24 then reset hour to 0.


Here,the below C program to design a digital clock is successfully compiled and run(on Codeblocks) on the Windows System to produce desired output.Let’s check out the program below.


SOURCE CODE : :


 

#include <stdio.h>
#include <time.h> //for sleep() function

int main()
{
    int hour, minute, second;

    hour=minute=second=0;

    while(1)
    {
        //clear output screen
        system("cls");

        //print time in HH : MM : SS format
        printf("%02d : %02d : %02d ",hour,minute,second);

         //clear output buffer in gcc
        fflush(stdout);

         //increase second
        second++;

        //update hour, minute and second
        if(second==60){
            minute+=1;
            second=0;
        }
        if(minute==60){
            hour+=1;
            minute=0;
        }
        if(hour==24){
            hour=0;
            minute=0;
            second=0;
        }

        sleep(1);   //wait till 1 second
    }

    return 0;
}

Output : :


 

digital clock

3.3 12 votes
Article Rating
Category: Advance Programs C Programming Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
SAAd

How can i get the am pm in 12 hour clock?

Ariful

yes, you can add AM / PM same loop.

Ishu Sharma

please make background page white in color bcoz we are unable read programming section…
Thnx for your kind coperation