Calculating the area and circumference of a circle is a common task in geometry. So, let's say you have a circle with a radius of 5 units. You probably wanna find out how much space it covers (its area) and how long its boundary is (its circumference). This simple C program helps you do both.
A potential pitfall is using the wrong value of PI. Make sure you're using it right for accurate results. When you run this program, it asks for the circle's radius and then prints the area and circumference.
What You Need Before Writing the C Program for Calculating Area and Circumference of a Circle
Ensure you have these ready:
- Basic understanding of
printfandscanffunctions - Knowledge of variables and data types like
float - Using the
math.hlibrary for PI - Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler
Source Code of C Program to Calculate the Area and Circumference of a Circle
Below is the full source code to calculate the area and circumference of a circle based on the given radius.
#include <stdio.h>
#include <math.h>
int main() {
float radius, area, circumference;
const float PI = 3.14159;
// Taking input from user
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
// Calculating area
area = PI * radius * radius;
// Calculating circumference
circumference = 2 * PI * radius;
// Printing results
printf("Area of the circle: %.2f\n", area);
printf("Circumference of the circle: %.2f\n", circumference);
return 0;
}Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Circle Area and Circumference Program
Here's what you should see when you run the program with a radius of 5:
Enter the radius of the circle: 5
Area of the circle: 78.54
Circumference of the circle: 31.42
Algorithm to Calculate Area and Circumference in C
To calculate the area and circumference, the program first reads the radius and then uses standard formulas for a circle.
- Read the radius from the user.
- Calculate the area using the formula
PI * radius * radius. - Calculate the circumference using
2 * PI * radius. - Print the area and circumference.
flowchart TD A([Start]) --> B[/Read radius/] B --> C[area = PI * radius * radius] C --> D[circumference = 2 * PI * radius] D --> E[/Print area and circumference/] E --> F([End])
How the Circle Area and Circumference Program Works
Let's break down each part of the code.
#include <stdio.h>and#include <math.h>are used to include standard input-output functions and mathematical constants.- We declare
radius,area, andcircumferenceasfloatfor precise calculations. PIis defined as a constant3.14159.- The program prompts for the circle's radius with
printfand reads it usingscanf. - Calculates area using
PI * radius * radius. - Calculates circumference with
2 * PI * radius. - Finally, it prints the results with precision up to two decimal points.
Quick Quiz — test your understanding
Tap an option to check your answerWhat library is required to use the constant PI in C?
math.h library contains the constant PI.Time and Space Complexity of the C Program to Calculate Area and Circumference
Analyzing the performance of the program reveals the following:
Time Complexity: O(1). The program performs a constant number of operations: a multiplication for the area and another for the circumference, regardless of the input radius.
Space Complexity: O(1). It uses a fixed amount of memory for radius, area, circumference, and PI — all float variables.
Common mistake: Forgetting to include the math.h library can lead to undefined behavior if you try using M_PI for PI.
Key takeaway: For accurate calculations, always use a consistent value of PI and the right data types.
FAQs About Calculating Area and Circumference in C
Here are some common questions students have:
double gives more precision, but float usually works fine for most stuff.printf format specifier to show more decimals, like %.4f for four decimal places.Related Programs
If you found this program helpful, check out these related programs:
- C Program to Find the Roots of a Quadratic Equation
- C Program to Calculate Standard Deviation — Full Code & Output
- C Program to Calculate Simple and Compound Interest
- C Program to Calculate Difference Between Two Time Periods
- C Program to Check Leap Year with Code and Output
Conclusion
Think about using double for even more precision if you need it. Next up, you might wanna look into calculating the volume of a sphere. If this program feels too easy, try extending it to handle multiple circles in one go!
Practice coding in C by trying out more programs. Test your skills with our C quiz and run the code in the C online compiler.