In: Computer Science
Write a simple C program to generate a chart of circle properties. Output chart “iteration” is to be specified by user input. Your chart will include the radius, diameter, circumference, and Area values in a single table, and radius values will range from 0 to 50, floating point values, with three-decimal-place precision. Table should have all column entries right-justified and each column will have a heading above the entries. For example, assuming user enters a incrementation value of 5, the first few entries of the table could look something like:
Radius Diameter Circumference Area
———————————————————————
0.000 0.000 0.000 0.000
5.000 10.000 31.416 78.540
10.000 20.000 62.832 314.159
Use right alignment using printf() Diameter, Circumference, and Area must be used as functions. Output a blank line after every 10 radius entries.
C language Thanks!
//---------------- radius_chart.c ---------
#include<stdio.h>
//constant to define for PI VALUE
#define PI 3.14
//FUNCTION that returns the diameter for given radius.
float getDiameter(float radius)
{
return 2 * radius;
}
//FUNCTION that returns the area for given radius.
float getArea(float radius)
{
return PI * radius * radius;
}
//FUNCTION that returns the circumference for given radius.
float getCircum(float radius)
{
return 2 * PI * radius;
}
//FUNCTION that prints the chart for given radius.
void printChart(float incRadius)
{
//variables to store
float area;
float circum;
float diameter;
float range = 50.0;
float radius;
printf("\n Radius Diameter Circumference
Area\n");
printf("\n------------------------------------------------------\n");
//from radius 0 to 50(range) increment by incRadius
entered by user.
int count = 0;
for(radius = 0.0; radius <= range;radius +=
incRadius)
{
//get area,circumference and
diameter
area = getArea(radius);
circum = getCircum(radius);
diameter =
getDiameter(radius);
//use .3 to round by 3 decimal
places, and right align the values
//that are printed by using a
number after %
printf("%10.3f %10.3f %15.3f
%15.3f\n",radius,diameter,circum,area);
if(count%10 == 0 && count
!= 0)
{
printf("\n");
}
count++;
}
}
int main()
{
float incRadius;
//get input for incremented radius
printf("Enter Incrementation Value: ");
scanf("%f",&incRadius);
//if it is negative or zero print error msg and
exit.
if(incRadius <=(float) 0)
{
printf("Invalid Increment value. It
should be >=1\n");
printf("Exiting....");
return 1;
}
//if not print the chart by passing input from
user.
printChart(incRadius);
return 0;
}
//PLEASE LIKE THE ANSWER AND COMMENT IF YOU HAVE ANY DOUBTS.