Write a program in C to make a digital counter using graphics.h header file
Counter program in c: In this program, we will make a digital counter that counts from 1 to N in interval of 1 seconds(1000 milliseconds). We will use below mentioned graphics functions in this program.
C program to make a digital counter using graphics
#include <stdio.h> #include <conio.h> #include <graphics.h> #include <string.h> #include <dos.h> int main() { int gd = DETECT, gm; int i, midx, midy, count; char string[100]; printf("Enter a Number\n"); scanf("%d", &count); initgraph(&gd, &gm, "X:\\TC\\BGI"); /* get mid positions in x and y-axis */ midx = getmaxx()/2; midy = getmaxy()/2; for (i = 0; i <= count; i++) { /* draws the gray board */ setcolor(WHITE); setfillstyle(SOLID_FILL, WHITE); rectangle(midx - 50, midy - 50, midx + 50, midy + 50); floodfill(midx, midy, WHITE); /* place the counter inside rectangle */ setcolor(BLUE); sprintf(string, "%s", "Counter"); settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 5); settextjustify(CENTER_TEXT, CENTER_TEXT); outtextxy(midx, midy - 100, "Counter"); /* print the counter value */ sprintf(string, "%d", i); outtextxy(midx, midy, string); /* delay for a second(1000 milli second) */ delay(1000); /* clears screen */ cleardevice(); } getch(); closegraph(); return 0; }
Program Output
Enter a Number 10
Here is the screen shot of a digital counter.