graphics

c program countdown

This c graphics program performs countdown for 30 seconds.

C program countdown code

#include<graphics.h>
#include<dos.h>
#include<conio.h>
 
main()
{
   int gd = DETECT, gm, i;
   char a[5];
 
   initgraph( &gd, &gm, "C:\\TC\\BGI");
 
   settextjustify( CENTER_TEXT, CENTER_TEXT );
   settextstyle(DEFAULT_FONT,HORIZ_DIR,3);
   setcolor(RED);
 
   for(i = 30 ; i >=0 ; i--)
   {
      sprintf(a,"%d",i);
      outtextxy(getmaxx()/2, getmaxy()/2, a);
      delay(1000);
 
      if ( i == 0 )
         break;
      cleardevice();
   }
 
   getch();

c program to draw circles in circles

This program draws circles in circles in two different colors.

C programming code

#include<graphics.h>
#include<conio.h>
#include<dos.h>
 
main()
{
   int gd = DETECT, gm, x, y, color, angle = 0;
   struct arccoordstype a, b;
 
   initgraph(&gd, &gm, "C:\\TC\\BGI");
   delay(2000);
 
   while(angle<=360)
   {
      setcolor(BLACK);
      arc(getmaxx()/2,getmaxy()/2,angle,angle+2,100);
      setcolor(RED);
      getarccoords(&a);
      circle(a.xstart,a.ystart,25);
      setcolor(BLACK);
      arc(getmaxx()/2,getmaxy()/2,angle,angle+2,150);

c smiling face animation

This animation using c draws a smiling face which appears at random position on screen. See output below the code, it will help you in understanding the code easily.

C programming code

#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
 
main()
{
   int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75;
   void *p;
 
   initgraph(&gd,&gm,"C:\\TC\\BGI");
 
   setcolor(YELLOW);
   circle(50,100,25);
   setfillstyle(SOLID_FILL,YELLOW);
   floodfill(50,100,YELLOW);
 
   setcolor(BLACK);
   setfillstyle(SOLID_FILL,BLACK);
   fillellipse(44,85,2,6);

c program to draw a 3d bar chart

This c program draws a 3d bar chart.

C programming code

#include<graphics.h>
#include<conio.h>
 
main()
{
   int gd = DETECT, gm;
 
   initgraph(&gd, &gm, "C:\\TC\\BGI");
 
   setcolor(YELLOW);
   rectangle(0,30,639,450);
   settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
   setcolor(WHITE);
   outtextxy(275,0,"Bar Chart");
 
   setlinestyle(SOLID_LINE,0,2);
 
   line(100,420,100,60);
   line(100,420,600,420);
   line(90,70,100,60);
   line(110,70,100,60);
   line(590,410,600,420);
   line(590,430,600,420);
 
   outtextxy(95,35,"Y");
   outtextxy(610,405,"X");

c program to draw pie chart

This c program draws a pie chart showing percentage of various components drawn with different filling styles and colors.

C programming code

/* Program to draw a pie chart */

#include<graphics.h>
#include<conio.h>
 
main()
{
   int gd = DETECT, gm, midx, midy;
 
   initgraph(&gd, &gm, "C:\\TC\\BGI");
 
   setcolor(MAGENTA);
   rectangle(0,40,639,450);
   settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
   setcolor(WHITE);
   outtextxy(275,10,"Pie Chart");
 
   midx = getmaxx()/2;
   midy = getmaxy()/2;
 
   setfillstyle(LINE_FILL,BLUE);

c program draw bar chart

This program draws bar chart using c graphics. Chart is drawn using bars filled with different styles and in different colors.

C programming code

#include<graphics.h>
#include<conio.h>
 
main()
{
   int gd = DETECT, gm;
 
   initgraph(&gd, &gm, "C:\\TC\\BGI");
 
   setcolor(YELLOW);
   rectangle(0,30,639,450);
   settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
   setcolor(WHITE);
   outtextxy(275,0,"Bar Chart");
 
   setlinestyle(SOLID_LINE,0,2);
 
   line(100,420,100,60);
   line(100,420,600,420);
   line(90,70,100,60);
   line(110,70,100,60);

Draw shapes using c graphics

This c graphics program draws basic shapes such as circle, line, rectangle, ellipse and display text on screen using c graphics. This can be a first graphics program for a beginner.

C programming code

#include<graphics.h>
#include<conio.h>
 
main()
{
   int gd = DETECT,gm,left=100,top=100,right=200,bottom=200,x= 300,y=150,radius=50;
 
   initgraph(&gd, &gm, "C:\\TC\\BGI");
 
   rectangle(left, top, right, bottom);
   circle(x, y, radius);
   bar(left + 300, top, right + 300, bottom);
   line(left - 10, top + 150, left + 410, top + 150);

C program to move a car

Program in c using graphics move a car. A car is made using two rectangles and two circles which act as tyres of car. A for loop is used to move the car forward by changing the rectangle and circle coordinates and erasing the previous contents on screen using clearviewport, you can also use cleardevice. Speed of car can be adjusted using delay function, more the delay lesser will be the speed or lesser the delay your car will move fast.

captcha program in c

This program generates captcha, a captcha is a random code generated using some algorithm. We will use random function in our code. These are used in typing tutors and in website to check whether a human is operating on a website.

C programming code

#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
 
main()
{
   int i = 0, key, num, midx, gd = DETECT, gm;
   char a[10];
 
   initgraph(&gd,&gm,"C:\\TC\\BGI");
 
   midx = getmaxx()/2;
 
   settextstyle(SCRIPT_FONT,HORIZ_DIR,5);
   settextjustify(CENTER_TEXT,CENTER_TEXT);
   setcolor(GREEN);

traffic light program in c, traffic light simulation

Traffic light Simulation: Traffic light program in c presents what happens in our daily life at traffic light signals. Firstly user will press a key to start the traffic light simulation.

C programming code

#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
 
main()
{
   int gd = DETECT, gm, midx, midy;
 
   initgraph(&gd, &gm, "C:\\TC\\BGI");
 
   midx = getmaxx()/2;
   midy = getmaxy()/2;
 
   setcolor(RED);
   settextstyle(SCRIPT_FONT, HORIZ_DIR, 3);
   settextjustify(CENTER_TEXT, CENTER_TEXT);

Pages

Subscribe to RSS - graphics
randomness