How to Find Area of a Circle with Diameter in C Program | Functions
How to find area of a circle with diameter in C program.
In this article, you will learn how to find area of a circle with diameter in C program.
Example
Enter the radius of the circle::
7
Diameter :: = 14.000000 units
Circumference :: = 43.959999 units
Area :: = 153.860001 sq. units
You should have knowledge of the following topics in c programming to understand this program:
- C
main()
function - C
printf()
function - C
math.h
library
Standard Formula
D = 2 * r
C = 2 * PI * r
A = PI * r2
Where
R = radius of the circle
D = diameter of the circle
C = circumference of the circle
A = area of the circle
In this article, we used three ways to solve this problem
- By using the default value of PI = 3.14
- Second, by using the M_PI constant
- By using the Functions
1. By using the default value of PI = 3.14
// How to Find Area of a Circle with Diameter in C Program using PI=3.14
#include <stdio.h>
int main() {
float r, d = 0, c = 0, a = 0;
// r = radius, d = diameter, c = circumference, a = area
printf("Enter the radius of the circle::\n");
scanf("%f", &r);
// Calculation of diameter, circumference, and area
d = 2 * r;
c = 2 * 3.14 * r;
a = 3.14 * (r * r);
// It will print the final output
printf("\n");
printf("Diameter :: = %f units \n", d);
printf("Circumference :: = %f units \n", c);
printf("Area :: = %f sq. units ", a);
return 0;
}
Output
Enter the radius of the circle::
7
Diameter :: = 14.000000 units
Circumference :: = 43.959999 units
Area :: = 153.860001 sq. units
Explanation
In this given program, we have taken input 7
from the user then we applied the standard formula to calculate the diameter, circumference, and area.
Then this will return the final outputs 14.000000
, 43.959999
, and 153.860001
from the above calculation.
2. By using the M_PI constant
// How to Find Area of a Circle with Diameter in C using M_PI Constant
#include <stdio.h>
#include <math.h> // To import M_PI constant
int main() {
float r, d = 0, c = 0, a = 0;
// r = radius, d = diameter, c = circumference, a = area
printf("Enter the radius of the circle::\n");
scanf("%f", &r);
// Calculation of diameter, circumference, and area
d = 2 * r;
c = 2 * M_PI * r;
a = M_PI * (r * r);
// It will print the final output
printf("\n");
printf("Diameter :: = %f units \n", d);
printf("Circumference :: = %f units \n", c);
printf("Area :: = %f sq. units ", a);
return 0;
}
How to Find Area of a Circle with Diameter in C using Functions
// How to Find Area of a Circle with Diameter in C using Functions
#include <stdio.h>
// This function will print the diameter, circumference, and area
void CalcCircumferenceArea(float r) {
float d = 0, c = 0, a = 0;
// d = diameter, c = circumference, a = area
// Calculation of diameter, circumference, and area
d = 2 * r;
c = 2 * 3.14 * r;
a = 3.14 * (r * r);
// It will print the final output
printf("\n");
printf("Diameter :: = %f units \n", d);
printf("Circumference :: = %f units \n", c);
printf("Area :: = %f sq. units ", a);
}
// It's the driver function
int main() {
float r; // radius of the circle
printf("Enter the radius of the circle::\n");
scanf("%f", &r);
CalcCircumferenceArea(r);
return 0;
}
Output
Enter the radius of the circle::
10
Diameter :: = 20.000000 units
Circumference :: = 62.799999 units
Area :: = 314.000000 sq. units
Also, visit these links
C Program to Enter Marks of Five Subjects and Calculate Percentage and Grade
C++ Program to Calculate Percentage and Grade
Java Program to Calculate Grades of students
Python Program to Calculate Total Marks Percentage and Grade of a Student