Area of Circle Program in C
Now that we know the algorithm let's write the actual C program to find the area of a circle:
#include <stdio.h>
int main() {
float radius, area;
const float PI = 3.14159;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = PI * radius * radius;
printf("The area of the circle is: %.2f\n", area);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
Enter the radius of the circle: 5
The area of the circle is: 78.54
In this code:
1. We include the stdio.h header file to use standard input/output functions like printf() & scanf().
2. We define the main() function, which is the entry point of our program.
3. We declare the variables radius and area as float to store decimal values. We also define the constant PI with its approximate value.
4. We use printf() to display a prompt asking the user to enter the radius of the circle.
5. We use scanf() to read the user's input & store it in the radius variable.
6. We calculate the area using the formula A = πr² & store the result in the area variable.
7. Finally, we use printf() to display the calculated area, rounded to two decimal places using the %.2f format specifier.
Complexity Analysis
When analyzing the complexity of our program to find the area of a circle, we consider both time complexity & space complexity.
Time Complexity
The time complexity of this program is O(1), which means it takes constant time regardless of the input size. This is because the program performs a fixed number of operations:
- Reading the user's input (scanf())
- Calculating the area using a simple formula (A = πr²)
- Printing the result (printf())
These operations do not depend on the size of the input (i.e., the value of the radius), so the time complexity remains constant.
Space Complexity
This program's space complexity is also O(1) because it uses a constant amount of extra space. The program declares only two float variables (radius and area) and one constant (PI), which occupy a fixed amount of memory regardless of the input size.
Code Implementation for C Program to Find Area of Circle
To implement the C program, follow these steps
1. Open a text editor or an Integrated Development Environment (IDE) of your choice (e.g., Visual Studio Code, CodeBlocks, or Dev-C++).
2. Create a new file & save it with a .c extension (e.g., area_of_circle.c).
3. Copy & paste the following code into the file:
#include <stdio.h>
int main() {
float radius, area;
const float PI = 3.14159;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = PI * radius * radius;
printf("The area of the circle is: %.2f\n", area);
return 0;
}

You can also try this code with Online C Compiler
Run Code
4. Save the file.
5. Open a terminal or command prompt & navigate to the directory where you saved the file.
6. Compile the code using a C compiler. For example, if you're using GCC, type the following command & press Enter:
gcc area_of_circle.c -o area_of_circle
7. If there are no compilation errors, run the executable file by typing the following command & pressing Enter:
./area_of_circle
8. The program will prompt you to enter the radius of the circle. Type a number & press Enter.
9. The program will calculate the area of the circle & display the result.
Example output:
Enter the radius of the circle: 5
The area of the circle is: 78.54
Frequently Asked Questions
What happens if I enter a negative value for the radius?
The program will still calculate the area using the negative value, resulting in a negative area, which needs to be corrected.
Can I use this program to calculate the area of other shapes?
No, this program is specifically designed to calculate the area of a circle using the formula A = πr². You would need to modify the program to calculate the area of other shapes.
Can I modify the program to calculate the circumference of the circle as well?
Yes, you can add another calculation using the formula C = 2πr, where C is the circumference & r is the radius. You would need to modify the code & add an additional printf() statement to display the circumference.
Conclusion
In this article, we learned how to create a C program to calculate the area of a circle. We discussed the algorithm, analyzed the program's complexity, and provided a step-by-step guide to implement the code.
You can also check out our other blogs on Code360.