In this tutorial, you will learn how to write a C program to calculate area and circumference of a circle. This program is pretty simple as both the circle area and circle circumference need radius value.
Formula to calculate are and circumference of circle
Here 3.14159 is the value of Pi, represented by symbol π.
Area of circle = Pi * radius * radius
Circumference of circle = 2 * Pi * radius
Program to calculate Area and Circumference based on user input
This program takes radius value from the user. The program prompts user to enter the radius value and calculates the area and circumference of circle based on the entered value.
#include <stdio.h>
#define PI 3.14159
int main() {
float radius, area, circumference;
//Prompt user to enter the radius of circle
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
// Calculate area and circumference of circle
area = PI * radius * radius;
circumference = 2 * PI * radius;
// Print are and circumference of circle
printf("Area of the circle: %.2f\n", area);
printf("Circumference of the circle: %.2f\n", circumference);
return 0;
}
Output: User enters the radius value as 2 and program calculates and prints the area and circumference using this entered value.
Enter the radius of the circle: 2
Area of the circle: 12.57
Circumference of the circle: 12.57
Explanation:
- Include Standard Libraries:
#include <stdio.h>
for standard input and output functions such asprintf
andscanf
.#define PI 3.14159
defines the value of π (pi) for calculating the area and circumference of circle.
- Declare Variables:
float radius, area, circumference;
These variables hold the radius, area, and circumference of the circle.
- User Input:
printf("Enter the radius of the circle: ");
prompts the user to enter the radius of the circle.scanf("%f", &radius);
reads and stores the entered value inradius
variable.
- Calculate Area and Circumference:
area = PI * radius * radius;
This formula is used for area calculation.circumference = 2 * PI * radius;
This formula is used for circumference calculation.
- Print Results:
printf("Area of the circle: %.2f\n", area);
prints the area with two decimal places.printf("Circumference of the circle: %.2f\n", circumference);
prints the circumference with two decimal places.
- Return Statement:
return 0;
indicates that the program executed successfully.
Tumelo says
can you guys help me out here.
write a console program that calculate the area a and circuference c of the circle
a= p x r2
c=2 x p x r
you can send it on my email please.