Showing posts with label c code. Show all posts
Showing posts with label c code. Show all posts

Friday, April 20, 2012

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




Thursday, December 15, 2011

C Program to String Reverse

It is a basic C program to reverse a string.
In this c programming example user input a string and get reverse of accepted string.

Aim - C program to reverse accepted string [Using strrev function]


Program Code
//String Reverse using strrev function
#include"stdio.h"
#include"conio.h"
#include"string.h"
void main()
{
   char str[20];
   clrscr();
   printf("********* String Reverse Using strrev Function *******\n");
   printf("Enter a string:  ");
   gets(str);
   strrev(str);
   printf("\n Reverse string is:  %s",str);
   getch();
}
Output




Aim - C program to reverse accepted string [without using strrev function]


Program Code
//String Reverse without using strrev function
#include"stdio.h"
#include"conio.h"
#include"string.h"
void main()
{
 char str1[20],str2[20];
 int i,j=0;
 clrscr();
 printf("***********String Reverse*********\n");
 printf("Enter String:");
 gets(str1);
 for(i=strlen(str1)-1;i>=0;i--,j++)
 {
  str2[j]=str1[i];
 }
 str2[j]=NULL;
 printf("\nReverse String is:");
 puts(str2);
 getch();
}

Output