Write a C program to input radius of circle from user and find diameter, circumference and area of the given circle using function. How to find diameter, circumference and area of a circle using function in C programming.
Example
Input
Input radius: 10
Output
Diameter = 20 units Circumference = 62.83 units Area = 314.16 sq. units
Required knowledge
Basic C programming, Functions, Returning value from function
Read more – Program to find diameter, circumference and area of circle without functions.
In previous exercise we learned to declare and use program with single user defined function. Here in this program we will define more than one user-defined functions in a single program.
Declare functions to find diameter, circumference and area of circle
- First assign a meaningful name to all the three functions. Say function to calculate diameter, circumference and area are –
getDiameter()
,getCircumference()
andgetArea()
respectively. - All the above three functions uses one input i.e. radius of circle to calculate output. Hence all the three function must accept a parameter of
double
orint
type. - Finally, all the three functions returns either
double
orint
as output. Hence, return type of the function must be eitherdouble
orint
.
After considering above points function declaration looks like –
double getDiameter(double radius);
double getCircumference(double radius);
double getArea(double radius);
Program to find diameter, circumference and area using functions
/**
* C program to find diameter, circumference and area of a circle using functions
*/
#include <stdio.h>
#include <math.h> // Used for constant PI referred as M_PI
/* Function declaration */
double getDiameter(double radius);
double getCircumference(double radius);
double getArea(double radius);
int main()
{
float radius, dia, circ, area;
/* Input radius of circle from user */
printf("Enter radius of circle: ");
scanf("%f", &radius);
dia = getDiameter(radius); // Call getDiameter function
circ = getCircumference(radius); // Call getCircumference function
area = getArea(radius); // Call getArea function
printf("Diameter of the circle = %.2f units\n", dia);
printf("Circumference of the circle = %.2f units\n", circ);
printf("Area of the circle = %.2f sq. units", area);
return 0;
}
/**
* Calculate diameter of circle whose radius is given
*/
double getDiameter(double radius)
{
return (2 * radius);
}
/**
* Calculate circumference of circle whose radius is given
*/
double getCircumference(double radius)
{
return (2 * M_PI * radius); // M_PI = PI = 3.14 ...
}
/**
* Find area of circle whose radius is given
*/
double getArea(double radius)
{
return (M_PI * radius * radius); // M_PI = PI = 3.14 ...
}
Output
Enter radius of the circle: 10 Diameter of the circle = 20.00 units Circumference of the circle = 62.83 units Area of the circle = 314.16 sq. units
Happy coding 😉
Recommended posts
- Function programming exercises index.
- C program to find cube of any number using functions.
- C program to find maximum and minimum between two numbers using functions.
- C program to check even or odd using functions.
- C program to check Prime, Strong, Armstrong and perfect numbers using functions.
- C program to find all prime numbers between given interval using functions.