C Program to Find the Area of a Circle

«
»
Problem Description

Write a C program to find the area of a circle using the given radius.

Area of circle is defined as pi*r*r where pi is a constant whose value is (22/7 or 3.142) and r is the radius of a circle.

Formula to calculate the area of circle is: Area = pi*r*r

Example:
If the radius of a circle is 30, then area of circle is pi*r*r = 3.14*30*30 = 2827.80.

Problem Solution

Step 1: Start the program
Step 2: Input the value of radius (float value).
Step 3: Calculate area using formula pi*(r2)
Step 4: Print area of circle
Step 5: End the Program

For a better understanding of the Area of circle program, consider the following two scenarios.

advertisement
advertisement
Method 1: (Naive Approach)

In this approach, we calculate the area of a circle using the given radius and print the result as a float.

Examples:
If the radius of a circle is 10, then its area would be 3.141592*10*10 = 314.16 approx.
If the radius is 11.1,then the area of the circle would be 3.141592*11.1*11.1 = 387.08 approx.

Program/Source Code

Here is source code of the C program to calculate the area of a circle. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

#include <stdio.h>
//Defining the value of pi upto 6 decimal places
#define pi 3.141592
int main()
{
    float radius;
    printf("Enter Radius of Circle:\n");
    scanf("%f",&radius);
    float area=pi*radius*radius;
    //Precision of the radius and area in this program is upto two decimal places
    printf("The area of Circle with radius %0.2f is: %0.2f",radius,area);
    return 0;
}
Program Explanation

First, Input the radius which is a float value. Then calculate its area using pi*r*r and print the area upto desired precision. (In the above program the precision is of 2 decimal places, for which we have used %0.2f. For 5 decimal places use %0.5f)

Time Complexity: O(1)
In the above program there are no iterative statements,no function calls, the only operation performed is input output, so time complexity is O(1)

Space Complexity: O(1)
Since no auxiliary space is required, space complexity is O(1).

Runtime Test Cases

Testcase 1: Here, the given radius is “10”

Enter Radius of Circle:
10
The area of Circle with radius 10 is: 314.16

Testcase 2: Here, the given radius is “11.1”

advertisement
Enter Radius of Circle:
11.1
The area of Circle with radius 11.1 is: 387.08

Method 2: (Calculate Area of Circle using Function)

In this approach, we calculate the area of a circle using the pow function and print the result as a float.

Examples:
If the radius of a circle is 20, then its area would be 3.142*15*15 = 706.95 approx.

advertisement
Program/Source Code

Here is source code of the C program to calculate the area of a circle using function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

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

In this C program, library function defined in <math.h> header file is used to compute mathematical functions. We are reading the radius of a circle using ‘radius’ variable. To find the area of a circle, the following formula is used.
Area = PI * pow (radius, 2)

Time Complexity: O(1)
In the above program there are no iterative statements, so time complexity is O(1).

Space Complexity: O(1)
Since no auxiliary space is required, space complexity is O(1).

Runtime Test Cases
 
Enter the radius of a circle
15
Area of a circle = 706.95

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.