0
0
CProgramBeginner · 2 min read

C Program to Find Area of Circle with Input and Output

To find the area of a circle in C, use the formula area = 3.14159 * radius * radius and write a program that takes radius input and calculates this value.
📋

Examples

Inputradius = 0
OutputArea of circle: 0.000000
Inputradius = 5
OutputArea of circle: 78.539750
Inputradius = 10.5
OutputArea of circle: 346.360275
🧠

How to Think About It

To find the area of a circle, first get the radius from the user. Then multiply the radius by itself and by the constant pi (approximately 3.14159). This gives the area inside the circle.
📐

Algorithm

1
Get the radius value from the user
2
Calculate area using formula: area = pi * radius * radius
3
Display the calculated area
💻

Code

c
#include <stdio.h>

int main() {
    float radius, area;
    const float pi = 3.14159f;
    printf("Enter radius of circle: ");
    scanf("%f", &radius);
    area = pi * radius * radius;
    printf("Area of circle: %f\n", area);
    return 0;
}
Output
Enter radius of circle: 5 Area of circle: 78.539750
🔍

Dry Run

Let's trace the program with radius = 5 through the code

1

Input radius

User enters 5, so radius = 5

2

Calculate area

area = 3.14159 * 5 * 5 = 78.53975

3

Print area

Program prints 'Area of circle: 78.539750'

radiusarea
578.53975
💡

Why This Works

Step 1: Getting radius input

The program uses scanf to read the radius value entered by the user.

Step 2: Calculating area

It multiplies the radius by itself and by pi to find the area inside the circle.

Step 3: Displaying result

Finally, it prints the calculated area using printf.

🔄

Alternative Approaches

Using math.h constant M_PI
c
#include <stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>

int main() {
    double radius, area;
    printf("Enter radius of circle: ");
    scanf("%lf", &radius);
    area = M_PI * radius * radius;
    printf("Area of circle: %lf\n", area);
    return 0;
}
Uses the predefined constant M_PI for better precision but requires including <math.h> and defining _USE_MATH_DEFINES on some compilers.
Using double type for more precision
c
#include <stdio.h>

int main() {
    double radius, area;
    const double pi = 3.141592653589793;
    printf("Enter radius of circle: ");
    scanf("%lf", &radius);
    area = pi * radius * radius;
    printf("Area of circle: %lf\n", area);
    return 0;
}
Uses double type and more precise pi value for higher accuracy.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of operations regardless of input size, so it runs in constant time O(1).

Space Complexity

It uses a fixed amount of memory for variables, so space complexity is O(1).

Which Approach is Fastest?

All approaches run in constant time; using math.h constants may add slight overhead but improves precision.

ApproachTimeSpaceBest For
Basic float with manual piO(1)O(1)Simple programs, beginners
Using math.h M_PIO(1)O(1)Precision and standard constants
Double type with precise piO(1)O(1)High precision calculations
💡
Always use a constant for pi to avoid magic numbers and improve readability.
⚠️
Forgetting to multiply radius by itself and using radius * 2 instead of radius squared.