Here is a C graphics program to make a digital counter using graphics.h header file. 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 <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(); } closegraph(); return 0; }Output
Enter a Number 10Here is the screen shot of a digital counter.
Conclusion
As we wrap up our exploration into the captivating world of Turbo C graphics with the creation of a counter program, it's evident that the amalgamation of code and creativity brings forth a unique form of expression. In this tutorial, Turbo C has been our trusted companion, providing a canvas for visual storytelling through the medium of programming.
The journey began with the foundational steps of setting up the Turbo C environment, creating a simple yet dynamic counter display. We adorned our counter with colors and styles, elevating its visual appeal. The program evolved into an interactive experience, allowing users to reset the counter with a keystroke.
Beyond the lines of code, this tutorial is an invitation to dive deeper into the realm of graphics programming. Consider adding more features, experimenting with diverse color palettes, or incorporating sound elements to enhance the user experience. Graphics programming, with its fusion of logic and creativity, offers boundless opportunities for exploration.
Turbo C, though vintage, has proven to be a timeless conduit for unleashing imagination in the digital realm. It's not merely a tool for writing code; it's a medium for crafting digital art, telling stories, and engaging users in unique ways.
As you venture forth, let this tutorial be a stepping stone towards more complex and imaginative projects. Embrace the journey of creative coding, where each line of code is a stroke on the canvas of possibility. May your future endeavors in graphics programming be as vibrant and dynamic as the counter you've brought to life.
Related Topics