In this program we have designed an analogue clock which tells the current time on the systems clock using graphics in c programming language.

We start the code by include the header files “stdio.h” “conio.h” “dos.h” “graphics.h” and “process.h”.

In the beginning we will make three functions namely:-

- “calculatehrs(int h)”
- “calculatemin(int m)”
- “changehrs( int m, int a)”

In first function, hour’s value is received by the formal parameters and then check and whichever case it satisfies and x stores the value of the certain angle where it has to point on the clock. For example

1 2 3 4 5 6 7 8 9 10 11 12 13 |
int calculatehrs(int h) {int x; switch(h) {case 0: x=90; break; case 1: case 13: x=60; break; case 2: case 14: x=30; break; case 3: case 15: x=0; |

Second function, minute’s value is received by the formal parameters and then check and whichever case it satisfies “x” is assigned some value, but in this case minutes are first divided by 5. For example

1 2 3 4 5 6 7 8 9 10 11 12 13 |
int calculatemin(int m) {int x; if(m%5==0) {switch(m) {case 0: x=90; break; case 5: x=60; break; case 10: x=30; break; case 15: x=360; break; case 20: x=330; |

Its code is same as the code of hours as it marks that the minutes are either 5, 10, 15, 20, etc so it is either on 1,2,3 till 12.

If it is not a multiple of 5 then again it is checked if it is more than 0 and less than 15 then x is assigned the value according to the graphics screen.

1 2 3 4 5 6 7 |
if(m>0&&m<15) {switch(m) {case 1: x=84; break; case 2: x=78; break; case 3: x=72; break; case 4: x=66; break; |

This process is followed 3 more times as to find if the minutes are between “0-15”, “15-30”,”30-45” or “45-60” and “x” is returned which saves the value of the certain angle where it has to point on the clock same as first function.

Third function is for changing the position of the hour hand so as to give a lively look to the clock as its position is changed three times according if the minutes are between 15-30,30-45,45-60.

1 2 3 4 5 6 7 8 |
int changehrs(int m,int a) {if(m>15&&m<=30) a-=12; if(m>30&&m<=45) a-=18; if(m>45&&m<60) a-=24; return (a);} |

Now the main code starts as we enter the main function, “struct time t” is used to get the current time on the system and then it is later stored in h, m, s.

1 2 3 4 5 6 7 |
struct time t; int gd=DETECT,gm,h,m,s,a,b,c,i,j,k; initgraph(&gd,&gm,"C:\\TC\\BGI"); gettime(&t); h=t.ti_hour; m=t.ti_min; s=t.ti_sec; |

And after receiving the time all the tree functions are called to set angle of the hour and minute hand according to the current time.

After if three loops are initialized one after another first one being for hour, second one for minute and third one for seconds. A clock is created using circles and points are marked by small circles for each hour.

1 2 3 4 5 6 7 8 9 10 |
for(i=a;i>0;i-=6) for( j=b;j>0;j-=6) for( k=c;k>0;k-=6) {outtextxy(190,20,"Analog Clock"); circle(300,200,102); circle(300,200,100); outtextxy(385,185,"3"); outtextxy(288,110,"12"); outtextxy(207,185,"9"); outtextxy(295,290,"6"); |

Pieslice function is used to denote hour, minute and seconds hand instead of using a line function as it is easier to denote.

1 2 3 |
pieslice(300,200,i-1,i,75); pieslice(300,200,j-1,j,85); pieslice(300,200,k-1,k,95); |

Its syntax is

pieslice (x, y, starting angle, end angle, radius);

The whole process is repeated until user presses any key.

The program is really simple, you just need to understand the basic analogue clock and divide the clock in 360 degree angle and calculate all the angles for hour, minutes and seconds.

### Download source code C Program: Analogue Clock Using C Graphics

[sociallocker]