How to write a C Program to find Diameter, Circumference, and Area Of a Circle using Functions with example.
The mathematical formulas behind these calculations are:
- Diameter of a Circle = 2r = 2 * radius
- Circumference of a Circle = 2πr = 2 * π * radius
- Area of a circle is: A = πr² = π * radius * radius
TIP: Please refer C Program to Calculate Area Of a Circle article to understand the various ways to calculate Area of a Circle.
C Program to find Diameter, Circumference, and Area Of a Circle
This program allows the user to enter radius of a circle. Using that radius value, it will find the Diameter, Circumference, and Area Of a Circle
/* C Program to find Diameter, Circumference, and Area Of a Circle */ #include<stdio.h> #define PI 3.14 int main() { float radius, area, circumference, diameter; printf("\n Please Enter the radius of a circle : "); scanf("%f",&radius); diameter = 2 * radius; circumference = 2 * PI * radius; area = PI * radius * radius; printf("\n Diameter Of a Circle = %.2f\n", diameter); printf("\n Circumference Of a Circle = %.2f\n", circumference); printf("\n Area Of a Circle = %.2f\n", area); return 0; }
OUTPUT
C Program to find Diameter, Circumference, and Area Of a Circle using Functions
In this program we are using Functions to finds the Diameter, Circumference, and Area Of a Circle. Remember, you have to include math.h library to use M_PI otherwise, manually define PI value as we did in first example.
/* C Program to find Diameter, Circumference, and Area Of a Circle */ #include<stdio.h> #include<math.h> double find_Diameter(double radius); double find_Circumference(double radius); double find_Area(double radius); int main() { float radius, area, circumference, diameter; printf("\n Please Enter the radius of a circle : "); scanf("%f",&radius); diameter = find_Diameter(radius); circumference = find_Circumference(radius); area = find_Area(radius); printf("\n Diameter Of a Circle = %.2f\n", diameter); printf("\n Circumference Of a Circle = %.2f\n", circumference); printf("\n Area Of a Circle = %.2f\n", area); return 0; } double find_Diameter(double radius) { return 2 * radius; } double find_Circumference(double radius) { return 2* M_PI * radius; } double find_Area(double radius) { return M_PI * radius * radius; }
From the above code snippet, you can see we created three functions
- double find_Diameter(double radius) function to calculate the Diameter of a circle
- double find_Circumference(double radius) function to calculate the Circumference of a circle
- And double find_Area(double radius) function to find the Area of a circle
- Next, we called those functions in main() program to calculate those values
When the compiler reaches to the above specified functions in main() program, the compiler will immediately jump to their respective function definitions
OUTPUT