C Program to find circumference, diameter and area of a circle
Learn how to write a C program to calculate the circumference, diameter, and area of a circle using functions. This article provides a complete explanation and code example, highlighting the benefits of using functions in C programming.
Calculating the circumference, diameter, and area of a circle is a common problem in geometry and mathematics. These calculations are fundamental for various applications, from simple geometry problems to complex engineering designs. In this article, we will write a C program that calculates these three properties of a circle using separate functions for each calculation. This approach will help us understand the modularity and reusability of functions in C programming.
Understanding the Properties of a Circle
To compute the circumference, diameter, and area of a circle, we need to understand the following formulas:
-
Diameter (D): The diameter of a circle is twice the radius.
D = 2 x r
-
Circumference (C): The circumference is the distance around the circle and is calculated using the formula:
C = 2 x pi x r
-
Area (A): The area is the space enclosed by the circle and is given by:
A = pi x r^2
Where r is the radius of the circle and Π (Pi) is approximately 3.14159.
Write a C Program to find circumference, diameter and area of a circle
We will implement a C program that defines three functions: calculateDiameter
, calculateCircumference
, and calculateArea
. Each function will perform one of the calculations based on the radius provided by the user.
#include <stdio.h>
#define PI 3.14159
// Function to calculate the diameter of a circle
double calculateDiameter(double radius) {
return 2 * radius;
}
// Function to calculate the circumference of a circle
double calculateCircumference(double radius) {
return 2 * PI * radius;
}
// Function to calculate the area of a circle
double calculateArea(double radius) {
return PI * radius * radius;
}
int main() {
double radius, diameter, circumference, area;
// Input the radius from the user
printf("Enter the radius of the circle: ");
scanf("%lf", &radius);
// Call the functions to calculate diameter, circumference, and area
diameter = calculateDiameter(radius);
circumference = calculateCircumference(radius);
area = calculateArea(radius);
// Output the results
printf("Diameter of the circle: %.2lf\n", diameter);
printf("Circumference of the circle: %.2lf\n", circumference);
printf("Area of the circle: %.2lf\n", area);
return 0;
}
Output
Enter the radius of the circle: 5
Diameter of the circle: 10.00
Circumference of the circle: 31.42
Area of the circle: 78.54
Explanation of the Code
- Constants and Libraries:
- We define
PI
as 3.14159, which is used in the formulas for circumference and area.
- We define
- Function
calculateDiameter
:- Takes the radius as input and returns the diameter, calculated as
2 * radius
.
- Takes the radius as input and returns the diameter, calculated as
- Function
calculateCircumference
:- Takes the radius as input and returns the circumference, calculated as
2 * PI * radius
.
- Takes the radius as input and returns the circumference, calculated as
- Function
calculateArea
:- Takes the radius as input and returns the area, calculated as
PI * radius * radius
.
- Takes the radius as input and returns the area, calculated as
- Main Function
main
:- The user is prompted to enter the radius of the circle.
- The functions are called to compute the diameter, circumference, and area, and their results are stored in respective variables.
- The results are then printed to the console.