C Program to Calculate Area of Circle

C Program to Calculate Area of Circle

One of the most common and simple problems beginners learn in C programming is how to calculate the area of a circle. It’s a classic example that introduces basic mathematical operations, variables, and the use of formulas in programming. Understanding how to calculate the area of a circle not only helps you strengthen your foundation in C but also builds confidence in handling real-world calculations using code.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

The formula to calculate the area of a circle is simple:
Area = π × r × r, where π (pi) is approximately 3.14159 and r is the radius of the circle. This formula is widely used in geometry, physics, and engineering applications. For instance, if you’re designing a program that measures space, shapes, or distances, knowing how to work with such formulas is essential. Let’s now explore several versions of C programs that calculate the area of a circle, each demonstrating a different approach suitable for beginners.

Program 1: Calculate Area of Circle Using Predefined Radius

This first program demonstrates how to calculate the area of a circle when the radius is already known or defined in the program. It’s a simple way to understand how to apply mathematical formulas in C.

#include <stdio.h>

int main() {

    float radius = 5.0;
    float area;

    area = 3.14159 * radius * radius;

    printf("Radius: %.2f\n", radius);
    printf("Area of Circle: %.2f\n", area);

    return 0;

}

In this program, the radius is predefined with a value of 5. The area is calculated using the formula π × r × r, and the result is displayed with two decimal points using the printf() function. This method is simple and clear, making it ideal for beginners to understand how arithmetic works in C. Even though it’s static, it’s a great starting point for practicing how to handle mathematical expressions and variable declarations.

Program 2: Calculate Area of Circle Using User Input

While predefined values are useful for learning, real-world programs often rely on user input. The next program allows the user to enter the radius value, making it more interactive.

#include <stdio.h>

int main() {

    float radius, area;

    printf("Enter the radius of the circle: ");
    scanf("%f", &radius);

    area = 3.14159 * radius * radius;

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

    return 0;

}

Here, the program uses scanf() to take input from the user. When the user enters a radius value, the program computes the area using the same formula. This version is more dynamic because it can calculate the area for any radius entered. Beginners will find this helpful for understanding input and output in C programming, and it builds the foundation for creating user-driven applications.

Program 3: Calculate Area of Circle Using the M_PI Constant

Instead of typing the value of π manually, we can use a predefined constant available in the math library. This approach improves accuracy and makes the code cleaner.

#include <stdio.h>
#include <math.h>

int main() {

    float radius, area;

    printf("Enter the radius of the circle: ");
    scanf("%f", &radius);

    area = M_PI * radius * radius;

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

    return 0;

}

This program includes the <math.h> header, which defines the constant M_PI for π. Using predefined constants like M_PI ensures precision and helps avoid manual typing errors. It also teaches beginners how to use C libraries, which are collections of functions and constants designed to make programming easier.

Program 4: Calculate Area of Circle Using a Function

To make your code reusable and structured, you can write a function that calculates the area. This approach helps you understand modular programming in C.

#include <stdio.h>
#include <math.h>

float calculateArea(float r) {
    return M_PI * r * r;
}

int main() {

    float radius, area;

    printf("Enter the radius: ");
    scanf("%f", &radius);

    area = calculateArea(radius);

    printf("Area of Circle: %.2f\n", area);

    return 0;

}

In this example, a function named calculateArea() is created to compute the area of the circle. The main function simply takes the radius input and calls this function. This approach makes the program more organized and easier to maintain. Beginners will benefit from learning how functions help reduce repetition and improve readability.

Program 5: Calculate Area of Multiple Circles

Sometimes you may want to calculate the area of several circles in a single program. The following example demonstrates how to do this using a simple loop.

#include <stdio.h>
#include <math.h>

int main() {

    int n, i;
    float radius, area;

    printf("Enter the number of circles: ");
    scanf("%d", &n);

    for(i = 1; i <= n; i++) {

        printf("\nEnter radius of circle %d: ", i);
        scanf("%f", &radius);

        area = M_PI * radius * radius;

        printf("Area of circle %d: %.2f\n", i, area);

    }

    return 0;

}

This program uses a for loop to take multiple inputs from the user, calculating and displaying each area one by one. This version is especially helpful for beginners who want to learn about loops and repetitive calculations in C. It’s also a practical exercise in managing user input and performing repeated operations efficiently.

Frequently Asked Questions (FAQ)

Beginners often have common questions when learning about calculating the area of a circle in C. Let’s answer some of them here for clarity.

Q1. What is the formula used to calculate the area of a circle in C?
The formula is Area = π × r × r, where r is the radius of the circle.

Q2. How do I use π in my program?
You can either define it manually as 3.14159 or include <math.h> and use the constant M_PI for more accuracy.

Q3. Why is <math.h> used in some programs?
The <math.h> library contains mathematical functions and constants like M_PI. It provides more precision and makes your code cleaner.

Q4. Can I calculate the area for negative radius values?
No, a radius cannot be negative in real-world geometry. Always check for valid input to ensure accurate results.

Q5. What data type should I use for radius and area?
Use float or double since the area of a circle often involves decimal values.

Conclusion

Calculating the area of a circle in C is a perfect exercise for beginners to understand how formulas, variables, and user inputs work together in programming. Whether you use predefined values, take input from users, or write a function, each version strengthens your skills and understanding of C syntax. The best way to learn is through practice—try modifying the programs, changing variable types, or adding features like validation. As you gain confidence, you’ll be ready to handle more advanced problems and real-world applications.

Additional & References

If you’re eager to explore more about C programming and mathematical operations, these resources will help you go beyond the basics and build stronger coding skills.

  1. C Standard Library (math.h) – Official reference for mathematical functions and constants like M_PI. Great for understanding how math operations are handled in C.
  2. Programiz C Language Tutorials – Step-by-step guides for learning C programming basics, perfect for beginners who want to practice.
  3. GeeksforGeeks C Programming Section – Offers clear explanations, examples, and quizzes to strengthen your understanding.
  4. Learn-C.org – An interactive platform to test and run C programs online, ideal for experimenting with small programs like area calculations.
Scroll to Top