Write a C program to calculate the area of a circle using the given radius.
What is the area of a circle?
The area of a circle is defined as π*r*r, where π is a constant with the value of approximately 3.142 and r is the radius of the circle.
Formula to calculate the area of a circle:
Area = pi*r*r
Example:
If the radius of a circle is 30, then the area of circle is pi*r*r = 3.14*30*30 = 2827.80.
1. Start the program.
2. Get input for the radius of the circle (a floating-point value).
3. Calculate the area of the circle using the formula pi*(r2)
4. Print the area of the circle.
5. End the Program.
For a better understanding of the area of a circle program, consider the following two examples:
- C program to find the area of a circle using the given radius.
- C program to find the area of a circle using a function.
This approach calculates the area of a circle using the given radius and prints the result as a floating-point number.
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.
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); // Calculate the area of the circle float area=pi*radius*radius; // Print the area of the circle, rounded to two decimal places printf("The area of Circle with radius %0.2f is: %0.2f",radius,area); return 0; }
1. Get the radius of the circle from the user.
2. Calculate the area of the circle using the formula pi*r*r
3. Print the area of the circle, rounded to the desired precision.
Time Complexity: O(1)
The above program has no iterative statements, no function calls, and only performs input and output operations. Therefore, its time complexity is O(1).
Space Complexity: O(1)
The above program does not require any auxiliary space. Therefore, its space complexity is O(1).
Testcase 1: In this case, input the radius as “10” to calculate the area of the circle.
Enter Radius of Circle: 10 The area of Circle with radius 10 is: 314.16
Testcase 2: In this case, input the radius as “11.1” to calculate the area of the circle.
Enter Radius of Circle: 11.1 The area of Circle with radius 11.1 is: 387.08
In this method, we calculate the area of a circle using the pow function and then print the result as a floating-point number.
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); }
This C program calculates the area of a circle. It first asks the user for the radius of the circle and then uses the formula PI * radius * radius to calculate the area. Finally, it prints the area of the circle to the console.
Time Complexity: O(1)
The time complexity of the above program is O(1), as there are no iterative statements.
Space Complexity: O(1)
The space complexity of the program is also O(1), as no auxiliary space is required.
In this case, input the radius as “15” to calculate the area of the circle.
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”.
If you find any mistake above, kindly email to [email protected]- Practice Computer Science MCQs
- Apply for C Internship
- Apply for Computer Science Internship
- Check Computer Science Books
- Practice BCA MCQs