Table of contents
1.
Introduction
2.
Algorithm to Find Area of Circle in C
3.
Area of Circle Program in C
4.
Complexity Analysis
4.1.
Time Complexity
4.2.
Space Complexity
5.
Code Implementation for C Program to Find Area of Circle
6.
Frequently Asked Questions 
6.1.
What happens if I enter a negative value for the radius?
6.2.
Can I use this program to calculate the area of other shapes?
6.3.
Can I modify the program to calculate the circumference of the circle as well?
7.
Conclusion
Last Updated: Nov 24, 2024
Easy

Area of Circle Program in C

Author Rinki Deka
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Calculating the area of a circle is a fundamental concept in mathematics and even in programming. In C, we can create a simple program that takes the radius of a circle as input and calculates its area using the formula A = πr². 

Area of Circle Program in C

In this article, we will discuss the process of writing a C program to find the area of a circle. We'll cover the algorithm, the program's complexity, and the code implementation. 

Algorithm to Find Area of Circle in C

To find the area of a circle, we use the formula A = πr², where A is the area & r is the radius of the circle. In our C program, we'll follow these steps to calculate the area:

1. Declare the necessary variables: radius (float) & area (float).
 

2. Prompt the user to enter the radius of the circle.
 

3. Read the user's input & store it in the radius variable.
 

4. Calculate the area using the formula A = πr², where π is approximately 3.14159.
 

5. Print the calculated area.
 

The algorithm is very easy to understand. We'll use the printf() function to display messages and prompts to the user and the scanf() function to read the user's input. The area calculation will be performed using the standard multiplication operator (*) and the constant value of π.

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.

Live masterclass