C Program to Generate Analog Clock Using Graphics

It is a sample C program to generate analog clock using graphics in c programming. You run this code and enter the time Hour, Minutes and Second and press Enter. You will get Analog clock that start from your set time.

Program Code
//C program For Analog Clock
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
main()
{
int gd=DETECT,gm;
int xm,ym,i,j,k,hrs,min,sec;
clrscr();
// Entering the correct and current time with verification
printf("Enter the current time\n Hours: ");
scanf("%d",&hrs);
printf("Minutes: ");
scanf("%d",&min);
printf("Seconds: ");
scanf("%d",&sec);

while (hrs<=0||hrs>12)
 {
 printf("Enter the correct hour:");
 scanf("%d",&hrs);
 }

while (min<0||min>=60)
 {
 printf("Enter the correct minute:");
 scanf("%d",&min);
 }

while (sec<0||sec>=60)
 {
 printf("Enter the correct second:");
 scanf("%d",&sec);
 }

//   Initilization of graphics mode
initgraph(&gd,&gm,"..\\bgi");
xm=getmaxx();
ym=getmaxy();

//   Generating the gasic outline of the clock
for(i=0;i<3;i++)         //   Drawing outer circle in dark
 circle(xm/2,ym/2,200+i);

setcolor(4);

for (i=-1;i<2;i++)      //Drawing the 2 vertical lines in thick
 {
 line(xm/2+i,ym/2-200,xm/2+i,ym/2-170);
 line(xm/2+i,ym/2+200,xm/2+i,ym/2+170);
 }

for (i=-1;i<2;i++)        //Drawing 2 horizontal lines in thick
 {
 line(xm/2-200,ym/2+i,xm/2-170,ym/2+i);
 line(xm/2+200,ym/2+i,xm/2+170,ym/2+i);
 }

setcolor(15);

for (i=1;i<12;i++)                  //Drawing the remaining lines in light
 if ((i!=3)&&(i!=6)&&(i!=9))
  line(xm/2+200*cos(3.14*i/6),ym/2+200*sin(3.14*i/6),xm/2+170*cos

(3.14*i/6),ym/2+170*sin(3.14*i/6));

// outtextxy(xm/2+50,ym/2+225,"CLOCK MADE BY  D VAMSI KRISHNA");

// Setting the error in hours hand
if (hrs==12)
 hrs=0;
if (hrs==11)
 hrs=-1;
if (hrs==10)
 hrs=-2;
if (hrs==9)
 hrs=-3;


k=sec;
j=min*60+i;
i=hrs*60*60+j;

//   Drawing the lines of minutes hours n seconds and updating it regularly
while(!kbhit())
 {
  
 setcolor(9);
 outtextxy(xm/2-60,ym/2+75,"D VAMSI KRISHNA");
 setcolor(14);
 line(xm/2,ym/2,xm/2+100*cos(0.000145*i-1.570),ym/2+100*sin(0.000145*i-1.570));
 circle(xm/2+100*cos(0.000145*i-1.570),ym/2+100*sin(0.000145*i-1.570),3);
 setcolor(3);
 line(xm/2,ym/2,xm/2+125*cos(0.001745*j-1.570),ym/2+125*sin(0.001745*j-1.570));
 circle(xm/2+125*cos(0.001745*j-1.570),ym/2+125*sin(0.001745*j-1.570),3);
 setcolor(5);
 line(xm/2,ym/2,xm/2+150*cos(0.1047*k-1.570),ym/2+150*sin(0.1047*k-1.570));
 //circle(xm/2+150*cos(0.1047*k-1.570),ym/2+150*sin(0.1047*k-1.570),3);
 delay(1000);

 setcolor(0);
 line(xm/2,ym/2,xm/2+100*cos(0.000145*i-1.570),ym/2+100*sin(0.000145*i-1.570));
 circle(xm/2+100*cos(0.000145*i-1.570),ym/2+100*sin(0.000145*i-1.570),3);
 line(xm/2,ym/2,xm/2+125*cos(0.001745*j-1.570),ym/2+125*sin(0.001745*j-1.570));
 circle(xm/2+125*cos(0.001745*j-1.570),ym/2+125*sin(0.001745*j-1.570),3);
 line(xm/2,ym/2,xm/2+150*cos(0.1047*k-1.570),ym/2+150*sin(0.1047*k-1.570));
 //circle(xm/2+150*cos(0.1047*k-1.570),ym/2+150*sin(0.1047*k-1.570),3);

 if (i==32400)
  i=-10800;
 if (k==60)
  k=0;
 if (j==3600)
  j=3600;

 i++;
 j++;
 k++;

 }
getch();
closegraph();
return 0;
}

Output




Post a Comment

1 Comments
Unknown said…
Why so complicated i have written simple code using functions. I will post that when i make some time.
This is also very good not but very very best buddy.