C Program to Make a Counter Using C Graphics

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
10
Here is the screen shot of a digital counter. C graphics program to draw a digital counter
Related Topics
C program to make a digital clock
C graphics program to draw concentric circles
C program for bouncing ball animation
C Program to draw a rectangle and bar
C Program to draw sine wave graph on screen
C Program to draw stars in night sky
C graphics program for moving car animation
List of all C Graphics Programs