Back

Area of Circle Program In C

05 Dec 2024
6 min read

Finding the area of a circle is one of the most fundamental exercises we all do in school. The formula is simple, the calculation is straightforward, and the same applies to programming. Calculating the area of circle program in C is one of the most basic coding exercises when you’re learning C. In this article, we take a look at how to write the algorithm for it and how the code can be written to find the area of a circle in different ways by using the radius, diameter, circumference, and function.

Area of Circle Formula

  1. The area of a circle using radius is given by:

Area (A)=πr2Where A is the area in unit squares, r is the radius of the circle in units.

  1. The area of a circle using diameter: 

The diameter is two times the radius. Therefore, the area A = π x d24

  1. Area of a circle using circumference: 

Circumference is given by C = 2 x π x r, therefore r = c/2 πSubstituting in the equation for area gives; A = C24

Algorithm for Area of Circle program in C

The algorithm for the area of circle in C looks like this: 

  • Begin by setting up the program structure and initialising any required libraries, like stdio.h, for input and output functions.
  • Create variables to store the radius and the area of the circle. For instance, float radius and float area can be used to handle decimal values.
  • Display a message asking the user to enter the radius of the circle. 
  • Use the scanf function to read the value entered by the user and store it in the radius variable. Ensure the input is handled as a floating-point number if decimals are expected. 
  • Apply the formula area = π * radius * radius, where π is approximated as 3.14159. Perform the calculation and store the result in the area variable.

Print the calculated area to the console using a formatted output function like printf.

C Program to Find the Area of Circle Using Radius

Now, let’s examine how to find the area of a circle using the radius and the formula discussed above.

/* C program to find the area of a circle, given the radius */
 
#include <stdio.h>
#include <math.h>
#define PI 3.142
 
void main()
{
    float radius, area;
 
    printf("Enter the radius of a circle \n");
    scanf("%f", &radius);
    area = PI * pow(radius, 2);
    printf("Area of a circle = %5.2f\n", area);
}

Output:

Enter the radius of a circle 
1
Area of a circle =  3.14

C Program to Find Area of Circle Using Circumference

In this program, the variable π is first initialised, and then the circumference is input using the formula: 

A = C2 / 4π

#include <stdio.h>

int main()
{
   // declaring the variables circumference and area
   float circumference, area;

   // initialising the value of pi
   float pi = 22.0 / 7.0;

   // taking the user input
   printf("Enter the circumference of the circle: ");
   scanf("%f", &circumference); 

   // calculating the area
   area = (circumference * circumference)/(4 * pi);

   // printing the area
   printf("The area of the circle is %f", area);

   return 0;
}

C Program to Find the Area of a Circle Using Diameter

Now we look at how to calculate the area of circle program in c using diameter:

#include <stdio.h>

int main() {
    float diameter, radius, area;
    const float PI = 3.14159;

    // Prompt the user to enter the diameter
    printf("Enter the diameter of the circle: ");
    scanf("%f", &diameter);

    // Calculate the radius
    radius = diameter / 2;

    // Calculate the area of the circle
    area = PI * radius * radius;

    // Print the area
    printf("The area of the circle is: %.2f\n", area);

    return 0;
}

C Program to Find the Area of a Circle Using Function

Here, we use a function to calculate the area of the circle. The function is created to find the area; within it, it multiplies pi with the radius square. The radius is set as 5 units, and the area is 78.5 square units.

// C program to find the area of 
// the circle using radius 
#include <math.h> 
#include <stdio.h> 
#define PI 3.142 
  
// Function to find the area of 
// of the circle 
double findArea(int r) { return PI * r * r; } 
  
// Driver code 
int main() 
{ 
    printf("Area is %f", findArea(5)); 
    return 0; 
}

Conclusion

Learning to write code for calculating the area of a circle might seem like a simple exercise, but it’s a powerful steppingstone in your programming journey. Exploring different approaches, like using the radius, diameter, or even user-defined functions, teaches us how to think critically and adapt solutions to specific problems. This flexibility is important to solving real-world challenges in coding. Understanding multiple methods to tackle the same problem builds problem-solving skills, which is a cornerstone of good coding practices. To learn more and build skills that will set you up as a software professional, join the CCBP 4.0 Academy coaching today!

Frequently Asked Questions

1. What is the algorithm for the area of a circle?

You can calculate the area using the radius, circumference, or diameter. To calculate the area of a circle, the first step is to input one of the three values from the user. If the diameter is provided, divide it by 2 to get the radius or extract the radius from the circumference. 

2. How to use functions to calculate the circle area in C? 

You can create a separate function, such as float calculate Area (float radius), to perform the calculation. Then, define the function with the formula you want to use. 

3. What data type should I use for the radius or diameter in C?

It's recommended to use float or double for the radius or diameter to handle decimal values. This is because circles often involve calculations using pi, which is a decimal number. 

4. How can I make my circle area program handle user input errors?

You can add validation steps to check if the input is a valid positive number. For example, verify that the user has not entered a negative or non-numeric value and prompt them again if the input is invalid.

5. Can I reuse this program logic for other shapes or problems?

The principles of this program, such as taking user input, performing calculations, and displaying results can be adapted to compute the area of other shapes.

Read More Articles

Chat with us
Chat with us
Talk to career expert