C Program to Find Perimeter of Circle
To find the perimeter of a circle in C, use the formula
perimeter = 2 * 3.14159 * radius and write a program that takes radius as input and prints the perimeter.Examples
Inputradius = 1
OutputPerimeter of circle = 6.28318
Inputradius = 5
OutputPerimeter of circle = 31.41590
Inputradius = 0
OutputPerimeter of circle = 0.00000
How to Think About It
To find the perimeter of a circle, you need the radius. The perimeter is the distance around the circle, calculated by multiplying 2 by pi (approximately 3.14159) and then by the radius. So, you get the radius from the user, multiply it by 2 and pi, and then show the result.
Algorithm
1
Get the radius value from the user2
Calculate perimeter using formula perimeter = 2 * pi * radius3
Print the calculated perimeterCode
c
#include <stdio.h> int main() { float radius, perimeter; const float pi = 3.14159f; printf("Enter radius of circle: "); scanf("%f", &radius); perimeter = 2 * pi * radius; printf("Perimeter of circle = %.5f\n", perimeter); return 0; }
Output
Enter radius of circle: 5
Perimeter of circle = 31.41590
Dry Run
Let's trace the example where radius = 5 through the code
1
Input radius
User enters 5, so radius = 5
2
Calculate perimeter
perimeter = 2 * 3.14159 * 5 = 31.4159
3
Print result
Output: Perimeter of circle = 31.41590
| radius | perimeter |
|---|---|
| 5 | 31.4159 |
Why This Works
Step 1: Getting radius input
The program asks the user to enter the radius, which is stored in a variable for calculation.
Step 2: Calculating perimeter
It uses the formula 2 * pi * radius to find the perimeter, where pi is approximated as 3.14159.
Step 3: Displaying the result
The program prints the perimeter with 5 decimal places to show a clear and precise output.
Alternative Approaches
Using math.h constant M_PI
c
#include <stdio.h> #include <math.h> int main() { double radius, perimeter; printf("Enter radius of circle: "); scanf("%lf", &radius); perimeter = 2 * M_PI * radius; printf("Perimeter of circle = %.5lf\n", perimeter); return 0; }
This uses the constant M_PI from math.h for better precision but requires linking with math library.
Using double type for more precision
c
#include <stdio.h> int main() { double radius, perimeter; const double pi = 3.141592653589793; printf("Enter radius of circle: "); scanf("%lf", &radius); perimeter = 2 * pi * radius; printf("Perimeter of circle = %.10lf\n", perimeter); return 0; }
Using double type and more precise pi value gives more accurate results for large radius values.
Complexity: O(1) time, O(1) space
Time Complexity
The program performs a fixed number of operations regardless of input size, so it runs in constant time O(1).
Space Complexity
Only a few variables are used to store input and output, so space complexity is constant O(1).
Which Approach is Fastest?
All approaches run in constant time; using math.h's M_PI may add slight overhead but improves precision.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Basic float with manual pi | O(1) | O(1) | Simple programs and beginners |
| Using math.h M_PI constant | O(1) | O(1) | Precision and standard constants |
| Double type with precise pi | O(1) | O(1) | High precision calculations |
Always use a constant for pi to avoid magic numbers and improve code readability.
Beginners often forget to multiply by 2 or use diameter instead of radius in the formula.