Area of Circle Program In C

Calculating the area of a circle might look like a simple math problem, but in C programming, it's your first chance to combine math with code. You'll learn how to take input from a user, perform calculations, and display results, skills you'll use in almost every program you write.

For students and beginners, this exercise isn't just about circles. It teaches you how to declare variables correctly, use data types like float for decimals, handle user input safely, and organize code using functions. These are practical skills that form the foundation for bigger programs, apps, and projects.

In this blog, you'll see multiple ways to calculate the area: using radius, diameter, or circumference, and even with modular functions. You'll get step-by-step algorithms, full C programs, outputs to verify results, and troubleshooting tips so you can write code that works reliably. By the end, you'll understand not just the "how," but the why behind each step, which is what makes coding logical and easy to debug.

Published: 27 Oct 2025 | Reading Time: 6 min read


Table of Contents


What You'll Actually Get From This Blog

Here's a quick, realistic snapshot of what this blog offers for students:


Area of Circle Formula

There are three primary formulas for calculating the area of a circle, depending on what information you have:

Using Radius

The area of a circle using radius is given by:

Area (A) = πr²

Where:

Using Diameter

The diameter is two times the radius. Therefore:

Area (A) = π × (d²/4)

Where:

Using Circumference

Circumference is given by C = 2πr, therefore r = C/(2π). Substituting in the equation for the area gives:

Area (A) = C²/(4π)

Where:


Algorithm and Approach for Area of Circle Program in C

When calculating the area of a circle within C programming, it is always to think a few steps ahead, approaching your solution as a simple algorithm. If you are logical in your development, your program will be correct, efficient, and easy to understand.

Step-by-Step Algorithm

  1. Start the Program

    • Initialize your program and include the necessary header files, such as stdio.h for input and output.
  2. Declare Variables

    • Create two variables to hold the radius and the area of the circle. As we will be using decimals we will use float.
  3. Prompt the User for Input

    • Request the user to enter the radius of the circle.
  4. Read User Input

    • Read the user entered value using the scanf function and assign it to the radius variable.
  5. Calculate the Area

    • Apply the formula: area = π × radius × radius
    • Use a constant value for π (for example, 3.14159).
  6. Display the Result

    • Use the printf function to output the calculated area.
  7. End the Program

    • Return from the main function to end the program.

Algorithm in List Form

  1. Start
  2. Declare float variables: radius, area
  3. Print message: "Enter the radius of the circle:"
  4. Read value into radius
  5. area = π × radius × radius
  6. Print area
  7. Stop

Explanation of the Approach

Why Use float?

The radius and area can also be decimals, so using a float indicates that your program will handle non-integer values.

Why Prompt and Read Input?

Having the user interact with the program creates more versatility in your input values.

Why Use a Constant for π?

Using a constant to define π (i.e. #define PI 3.14159) ensures accuracy along with readability of your code.

Why This Sequence?

The logical flow of the program follows the input → process → output pattern, which is the pattern all programmers use.

Pseudocode Representation

Start
    Prompt user to enter the radius
    Read radius
    area ← π × radius × radius
    Display area
Stop

C Program to Find the Area of Circle Using Radius

The radius of a circle is the simplest and most basic method for calculating the area of any object. You've learned how to accept user input and calculate a program using scientifically reasoned written code, which encapsulates the fundamental reasons why most C programmers learn.

Example Code

/* 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);
}

Code Explanation

Advantages and Best Practices

Sample Output

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

C Program to Find Area of Circle Using Circumference

Calculating area using the circumference is a different way to find area and supports your ongoing understanding of the connection between radius, circumference, and area. Calculating area this way also allows students to practice using those arithmetic operations with C coding.

Example Code

#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;
}

Code Explanation

Advantages and Best Practices


C Program to Find the Area of a Circle Using Diameter

On occasion, pupils find it easier to measure the diameter than the radius. Working with the diameter to calculate the area helps students understand how related formulas work and how to adjust input data.

Example Code

#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;
}

Code Explanation

Advantages and Best Practices


C Program to Find the Area of a Circle Using Functions

Using functions to modularize code in C is a great way to keep your code organized, reusable, and maintainable. Defining an explicit function for the area of a circle separates calculations from user interaction and output while providing a textbook example of good programming practice.

Example: Modular C Program with a Function

#include <stdio.h>

#define PI 3.14159

// Function declaration: calculates and returns the area of a circle
float calculateArea(float radius) {
    return PI * radius * radius;
}

int main() {
    float radius, area;

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

    // Function call: calculates the area
    area = calculateArea(radius);

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

    return 0;
}

Code Explanation

Advantages of This Structure


Complexity Analysis of Area of Circle Program in C

Along with looking at time complexity in an area of a circle C program, it is also important to consider space complexity. Along with time complexity, space complexity provides different measures of efficiency of the C code, for example, how quickly it executes and how much memory it utilizes.

Time Complexity

The time complexity of the program to calculate the area of a circle in C is O(1) or constant time. This is because, regardless of the value of the radius or the size of the input, the program performs a fixed number of operations: reads the input, performs a simple calculation, and prints the output.

Space Complexity

The space complexity is O(1) (constant space). The program declares only a few variables (radius, area, and possibly a constant for π) that will consume the same amount of memory regardless of input. A circular C program uses no other data structures or dynamic memory allocation.

Key Terms

Summary Table: Complexity Analysis

Aspect Complexity Explanation
Time Complexity O(1) Fixed steps for input, calculation, and output.
Space Complexity O(1) Only a few variables are used; no dynamic memory allocation.

Troubleshooting User Input Errors in Area of Circle C Programs

It is very common for beginners writing area of circle programs in C to face user input validation problems. For example, if the program does not validate input properly, it could (1) accept negative numbers, (2) accept non-numeric values, and/or (3) leave variables un-initialized, which could lead to an incorrect area calculation or a program crash.

Why Input Validation Matters

How to Validate User Input in C

You can add checks after reading user input to ensure the radius is a positive number. If the input is invalid, prompt the user to enter the value again.

Example: Handling Negative and Non-Numeric Input

#include <stdio.h>

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

    do {
        // Prompt user for input
        printf("Enter the radius of the circle (positive number): ");
        inputStatus = scanf("%f", &radius);

        // Clear input buffer if non-numeric input is detected
        if (inputStatus != 1) {
            while (getchar() != '\n'); // Discard invalid input
            printf("Invalid input. Please enter a numeric value.\n");
            continue;
        }

        // Check if radius is positive
        if (radius <= 0) {
            printf("Radius must be a positive number. Please try again.\n");
        }
    } while (inputStatus != 1 || radius <= 0);

    // Calculate area
    area = PI * radius * radius;

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

    return 0;
}

Code Explanation

Common Issues and How to Fix Them

1. Negative Area Output

If you get a negative area, check what the user entered as the radius. An area cannot be negative unless the radius value is negative. The radius must always be positive.

2. Unexpected Results or No Output

This could happen because the user entered non-numeric values. Validating input and clearing the buffers will correct this.

3. Compilation Errors

4. Area Always Zero

Radius not read correctly. Ensure correct use of scanf and %f.

Summary Table: Troubleshooting Common Input Issues

Problem Likely Cause Solution
Negative area Negative radius input Validate input; only accept positive numbers.
No output or program stuck Non-numeric input Clear the input buffer and prompt again.
Compilation error Missing header or typo Check includes and variable names.
Area always zero Radius not read correctly Ensure correct use of scanf and %f.

Conclusion

Learning to calculate the area of circle program in C might look simple at first, but it's more than just applying a formula; it's about building a mindset for logical thinking and problem-solving. By exploring different approaches using the radius, diameter, circumference, or modular functions, you learn how to break a problem into smaller parts, handle user input carefully, and write clean, reusable code.

This exercise also introduces essential programming habits: validating input, using constants for accuracy, structuring code with functions, and understanding the flow from input → process → output. Mastering these concepts equips you to handle larger problems and more complex programs in the future.

Key Highlights from This Blog

By practicing these concepts, you are not simply learning how to calculate areas; you are learning how to think like a programmer, a prerequisite for any career in the software industry. Understanding multiple methods to tackle the same problem builds problem-solving skills, which is a cornerstone of good coding practices.


Frequently Asked Questions

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.

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.

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

It is advisable to use float or double for the radius or diameter, allowing for decimal values. The rationale is that circles are often calculated with the constant pi, which is a decimal.

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

You can add validation steps to deterministic the input as a valid positive number. For example, ensuring the user has not input a negative or non-numeric value before prompting them for new input.

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.


Source: NxtWave CCBP 4.0 Academy

Original URL: https://www.ccbp.in/blog/articles/area-of-circle-program-in-c