How to make a digital clock using C
The following code creates a digital clock using a 1 sec. delay. <time.h> provides various functions to delay the time.
Source Code:
#include <stdio.h>
#include <time.h>
#include <windows.h>
COORD coord = {0, 0};
void gotoxy (int x, int y)
{
coord.X = x; coord.Y = y; // X and Y coordinates
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void delay(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock());
}
void showClock(int x_1, int x_2, int x_3,int y){
long i = 0; /* Loop counter */
clock_t now = 0; /* Holds initial clock time */
int interval = 1; /* Seconds interval for o/p */
int elapsed = 0;
int min=0,MIN=0,hrs=0,sec=0;
int d=0,f=0;
now = clock();
/* Get current clock time */
for(i = 0L ; ; i++){
elapsed = (clock()-now)/CLOCKS_PER_SEC;
if(elapsed>=interval){
interval += 1;
if(elapsed%60==0){
min=elapsed/60;
d=60*min;
if(min%60==0){
hrs=min/60;
f=60*hrs;
}
}
sec=elapsed-d;
MIN=min-f;
if(hrs<10){
gotoxy(x_1,y);printf("0%d",hrs);
}else{
gotoxy(x_1,y);printf(":%d",hrs);
}
if(min<10){
gotoxy(x_2,y);printf(":0%d",MIN);
}else{
gotoxy(x_2,y);printf(":%2d",MIN);
}
if(sec<10){
gotoxy(x_3,y);printf(":0%d",sec);
}else{
gotoxy(x_3,y);printf(":%2d",sec);
}
}
}
}
int main()
{
showClock(2,4,7,4);
return 0;
}
Output:

it ws usefull thnx
Hey, Any idea how I make the clock more complicated?
example:
hours:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. (11) . 12 .
same with minutes etcetra?
Hello,
Any idea how I change the hours, minutes and seconds into:
Hours:
1 . 2 . 3 . 4 . 5 . 6 . 7 . 8 . 9 . 10 . 11 . 12 . (where I highlight the current hour?) This is an assignment and im stuck
but how to show it in Linux? the is not detected with my compiler.
#include
#include
int main()
{
using namespace std;
int minutes = 0; // set the minutes
int hours = 0; //set the hour
Loop:
for (int a = 0; a < 61; a++)
{
if (a == 60)
{
minutes = minutes + 1;
a = a – 60;
goto Loop;
}
if (minutes == 60)
{
hours = hours + 1;
minutes = 0;
goto Loop;
}
if (hours == 13)
{
hours = hours – hours;
hours = hours + 1;
}
system("cls");
cout <<"Hours:"<<hours<<"/ Minutes:"<<minutes<<"/ Seconds:"<<a;
Sleep(1000);
}
cin.get();
return 0;
}
thanx rixwan bhai
give me the .exe file with source code.
my id is
[email protected]
#include…..?
#include……?