C Program to design a digital clock
Digital Clock With current time & Date
#include <stdio.h>
#include <time.h>//for sleep() function
#include <unistd.h>
#include<conio.h>
#include <stdlib.h>
int main()
{
int hour, minute, second;
hour=minute=second=0;
while(1)
{
//clear output screen
clrscr();
//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:-
00 : 01 : 17
Digital Clock With current time & Date
#include<stdio.h> // Use Input Output #include<conio.h> // Use Hold Screen #include<time.h> // use For Time #include<graphics.h> // use for graphics void main () { int graphdriver=DETECT, graphmode,i; // variable time_t rawTime; // variable char a[100]; // string variable struct tm * currentTime; // Structure pointer variable initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi"); while(1) // infinite loop { // design rectangle for display rectangle(1,1,639,444); rectangle(2,2,638,110); rectangle(2,111,638,222); rectangle(2,222,638,333); rectangle(2,333,638,444); rectangle(450,0,638,110); settextstyle(1,0,7); // text style outtextxy(15,10,"Watch Time"); // display text settextstyle(1,0,7); // text style outtextxy(15,350,"Welcome To Friends"); // Display text rawTime = time(NULL); // set null time currentTime = localtime(&rawTime); // set pc local time // print time I FOR HOUR & M FOR MINTUNE & S FOR SECOND strftime(a,100,"%I:%M:%S",currentTime); setcolor(5); // set time color settextstyle(1,0,10); // set text style outtextxy(40,80,a); // display time // print Am And Pm setcolor(15); settextstyle(3,0,12); strftime(a,100,"%p",currentTime); // P for Am & Pm outtextxy(470,-29,a); // print AM & Pm // print date setcolor(15); settextxy(1,0,7); strftime(a,100,"Date :- %d\\%m\\%y",currentTIme); // d for date & m for month & y for year outtextxy(40,230,a); // print date delay(1000); // delay means wait 1000 ms cleardevice(); // use clear data } getch(); // Use for hold Screen closegraph(); // graph close }
Digital clock in c program
#include <stdio.h>
#include<conio.h>
#include <time.h>
void delay(int time)
{
long pause;
clock_t time1 , time2 ;
pause = time ;
time2 = time1 = clock();
while( (time1-time2) < pause )
{
time1 = clock();
}
}
void main()
{
int a,b,c ;
for( a=1 ; a<=60 ; a++ )
{
for( b=1 ; b<=60 ; b++ )
{
for(c =1 ; c<=60 ; c++ )
{
printf("%d : %d : %d ", a , b ,c );
delay(1000);
clrscr();
}
}
}
}
output:-
1 : 2 : 38
Post a Comment
If you have any doubts, Please let me know
Thanks!