Skip to content

Create DigitalClock.c #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions C/DigitalClock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*C program to design a digital clock.*/

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

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

hour=minute=second=0;

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

//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;
}