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;
}
/* 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 :
Display
- DISPLAY SOURCE CODE AS OUTPUT IN C PROGRAM
- Sort Three Numbers - program reads in three Integers and displays them in ascending order.
- C Program To Display The Number In A Specific Formats
- C Program that Display a IBM Logo
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- LED ON OFF For One Sec/Count and Display on the Attached Serial Monitor
- Pre Order, Post order, In order Implement Binary Tree using linked list
- C Program to accept m*n matrix from user and display the elements of given matrix using function
- C Program to accept n numbers & store all prime numbers in an array & display this result
- C program to display the transpose of given 3 X 3 matrix
- C Program to accept a string from user, delete all vowels from that string & display the result
- C Program To Create Two Singly Linked List and Perform Following Operation
- Create Two Singly Linked List Perform Differences Display It C Program
- C Program to Create, Display, Insert and Deletion of Queue Elements
- Creation and Display of Elements in Both Forward and Reverse Direction in a Doubly Linked List
- C Program to Display a real time clock (HH:MM:SS) on the LCD
- Linked List For Getting Employee Details, Display and Search For Salary C Program
- Menu driven program in the creation,display,search, insertion and deletion of a node in the linked list
- Program to display the following pattern in C
- Display the Following Pattern * ** *** **** ***** C Program
Graphics
Input
- C Program to Get User Input and Compare
- C Program Basic logging of user input/output
- C Program File Input & Output Example
- C Program To Input & Print More Than One Words In Single Line
- Take microphone input and send to headphones
- A small I/O bound program to copy N bytes from an input file to an output file.
- C Program That in a Given Natural Number x Insert Figure c at Position p
- Input reads in the array positions and range for random generation.
- C Program that prompts the user to input a string, (whitespaces allowed)
- C Program to Input Number by the user between 1-100
- How to Gets User Input For Height in C Programming
- DEMULTIPLEXER CO ANALOG INPUTS
- C Program to Simulation program for FCFS ( First Come First Serve )
- C Program to Input Student Details into a File For Two Subjects
- Input a Number and Check if it's Palindrome or not C Program
- Input Number and Print it's Reverse C Program
- Input Number and Check if it's Armstrong C Program
- Input Number and Check if it's Even or Odd C Program
- Input Two Numbers and Print Greater Number C Program
- Input Number and Calculate Sum of it's Digits C Program
Pie Chart