In this article, we will write a C program to find diameter, circumference and area of a circle using functions.
The program takes the radius of the circle as input from the user and prints its diameter, circumference and area on the screen as an output.
Sample Input:
Enter the Radius of the Circle: 3
Output:
The diameter of the circle is: 6.000000
The circumference of the circle is: 18.840000
The Area of the Circle is: 28.260000
If we have a circle having a radius r, then its diameter, circumference, and area are given by the following formulas:
Where π is a constant having a fixed value of 22/7 or 3.14
To calculate the diameter, circumference and area of the circle, we have defined three functions circleDiameter()
, circleCircumference()
and circleArea()
.
Each function calculates the respective value and returns the result.
See implementation in the following C program:
// C program to calculate the diameter, // circumference and area of a circle #include <stdio.h> // User defined function to calculate circle's diameter float circleDiameter(float radius){ // Return Circle's diameter return 2 * radius; } // User defined function to calculate circle's circumference float circleCircumference(float radius){ const float PI = 3.14; // Return the circumference return 2 * PI * radius; } // User defined function to calculate circle's area float circleArea(float radius){ const float PI = 3.14; // Return the area return PI * radius * radius; } int main(){ // Declare the variables float radius, diameter, circumference, area; printf("Enter the Radius of the Circle: "); scanf("%f", &radius); // Call the functions diameter = circleDiameter(radius); circumference = circleCircumference(radius); area = circleArea(radius); // Print the result printf("The diameter of the circle is: %f", diameter); printf("\nThe circumference of the circle is: %f", circumference); printf("\nThe Area of the Circle is: %f", area); return 0; }
Output:
Enter the Radius of the Circle: 3 The diameter of the circle is: 6.000000 The circumference of the circle is: 18.840000 The Area of the Circle is: 28.260000
Program Explanation:
- The program first asks the user to enter the radius of the circle which is stored in the
radius
variable. - To calculate the diameter, circumference and area of the circle, we have defined three functions circleDiameter(), circleCircumference(), circleArea().
- Each function takes the radius of the circle as an argument, calculates the respective value and returns the result.
- The returned values are stored in the respective variables.
- Finally, the calculated value of the diameter, circumference and area of the circle is printed on the screen using the printf() function.
Thanks for reading.