# C Program: Analogue Clock Using C Graphics

0
7181

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

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

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.

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.

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.

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.

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

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.    [sociallocker]