Program to Display Pie Chart Accepting User Input C Program

How to write a C Program to Display Pie Chart Accepting User Input C Programming Language ?


/* This program displays a pie chart. This Program slices the pie as per the input given by the user. Make sure the total percentage do not exceed 100 % */
/* The program was compiled using Turbo C++ ver 3.0 */

Input :    Number of slices and % of each slice.

#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
main()
{
   int gd = DETECT, gm, midx, midy,i,dx,dy,value[20],n,degree=0;
   long start,end;

   char str[5];
   clrscr();
   printf("Enter the no of slices");
   scanf("%d",&n);

   initgraph(&gd, &gm, "..\\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;

   start = 0;
   end=0;
   value[0]=0;
   printf("Enter the percentage");
   for( i=1;i<=n;i++)
  { 
   scanf("%d",&value[i]);
  degree= degree +((long)value[i] * 360)/100;
  if ( degree >360)
  {
    printf("Exceded 100 % ..Program terminating.. press any key to continue");
   getch();
   exit(0);
}
   for(i=0;i<n;i++)
   {

  end = start +(long)(value[i+1]*360)/100;
   setfillstyle(i+1,i+1);
   pieslice(midx, midy, start,end, 100);
   itoa(value[i+1],str,10);
  
   dx= midx+100*cos(((double)(start+end)/2)*(3.14f/180));
   dy= midy+100*sin((-1*(double)(start+end)/2)*(3.14f/180));
  outtextxy(dx, dy , str);
   start= start+((long)value[i+1]*360)/100;
  }
  getch();
closegraph();
   return 0;
}


Learn More :