C Program to Find Area and Circumference of a Circle
In mathematics and programming, calculating the area and circumference of a circle is one of the most common beginner exercises. When learning the C programming language, simple programs like this help you understand how mathematical formulas can be implemented using variables, constants, and arithmetic operators.
In this tutorial, we will learn how to write a C program that calculates both the area and circumference of a circle using a given radius. The program uses the mathematical formulas πr² for area and 2πr for circumference. These formulas are applied using basic arithmetic operations in C.
This example is especially useful for beginners because it introduces several important concepts such as defining constants with the preprocessor directive, using variables, performing calculations, and displaying results using output functions.
Understanding the Circle
Before writing the program, it is important to understand what a circle is and how its measurements are calculated. A circle is a round geometric shape where every point on the boundary is at an equal distance from the center. This distance is called the radius.
Two important measurements of a circle are its area and circumference. The area represents the total space enclosed by the circle, while the circumference represents the distance around the circle.
To calculate these values, mathematicians use the constant π (pi), which is approximately equal to 3.14159. In programming, we usually use a simplified value such as 3.1415 or 3.14 depending on the required precision.
Mathematical Formulas Used
The calculations used in this program are based on the following mathematical formulas:
- Area of a circle = π × r × r
- Circumference of a circle = 2 × π × r
- π (pi) is approximately equal to 3.1415
- r represents the radius of the circle
These formulas are very easy to implement in a programming language like C because the language supports arithmetic operations such as multiplication and addition.
Concepts Used in This Program
To fully understand the program, you should be familiar with several basic C programming concepts. These include constants, variables, arithmetic operators, and output functions.
A constant is a fixed value that does not change during program execution. In this program, we define π as a constant using the #define preprocessor directive.
Variables are used to store values that may change during program execution. For example, the radius, area, and circumference are stored in variables.
Arithmetic operators such as multiplication (*) are used to perform mathematical calculations. These operators allow the program to implement formulas easily.
Finally, the printf() function is used to display the results on the screen.
C Program
#include <stdio.h>
#define pi 3.1415
int main()
{
int r = 5;
float area, circum;
area = pi * r * r;
circum = 2 * pi * r;
printf("Area of circle is %f\n", area);
printf("Circumference of circle is %f", circum);
return 0;
}
Program Output
Area of circle is 78.537498 Circumference of circle is 31.415001
Step by Step Explanation
Let us understand the program step by step so that you can clearly see how the calculations are performed.
Including the Header File
The program begins with the line #include
Defining the Value of Pi
The line #define pi 3.1415 defines a constant value for π. The #define directive is handled by the preprocessor before the program is compiled. This means that every time the program uses the word 'pi', it will automatically be replaced with the value 3.1415.
Using a constant like this makes the program easier to maintain because the value can be changed in one place if needed.
Declaring Variables
Next, the program declares variables used in the calculations. The variable r represents the radius of the circle. In this example, it is assigned the value 5.
Two floating point variables named area and circum are declared to store the calculated results. Floating point variables are used because the results may contain decimal values.
Calculating the Area
The area is calculated using the formula π × r × r. In the program, this is written as area = pi * r * r. The multiplication operator (*) performs the arithmetic operation.
This calculation multiplies the radius by itself and then multiplies the result by the value of π.
Calculating the Circumference
The circumference is calculated using the formula 2 × π × r. In the program, this is written as circum = 2 * pi * r.
This formula calculates the distance around the circle.
Displaying the Results
The printf() function is used to display the calculated area and circumference. The format specifier %f is used because the values are floating point numbers.
When the program runs, it prints the calculated values on the screen.
Visual Representation
To understand the program better, imagine a circle with a radius of 5 units. The program simply applies the formulas to compute the area and the circumference of this circle.
Radius (r) = 5 π ≈ 3.1415 Area = π × r² Area = 3.1415 × 5 × 5 Area ≈ 78.5375 Circumference = 2 × π × r Circumference = 2 × 3.1415 × 5 Circumference ≈ 31.415
Taking Radius Input from the User
Instead of using a fixed radius, you can modify the program so that the user enters the radius. This makes the program more flexible and interactive.
To do this, you can use the scanf() function to read the radius value from the keyboard.
Common Mistakes Beginners Make
- Forgetting to include the stdio.h header file.
- Using integer variables for area and circumference which removes decimal values.
- Writing incorrect formulas for area or circumference.
- Using incorrect format specifiers in printf().
Carefully checking your variables and formulas will help you avoid these mistakes.
Practice Exercises
To improve your understanding of C programming, try modifying this program in the following ways:
- Allow the user to input the radius.
- Use double instead of float for more precision.
- Calculate the diameter of the circle.
- Display the results in a formatted output.
Practicing these exercises will strengthen your understanding of variables, constants, and arithmetic operations in C.
Conclusion
In this tutorial, we created a simple C program to calculate the area and circumference of a circle. The program demonstrated how mathematical formulas can be implemented using variables and constants in C programming.
We also learned how to define constants using the #define directive, perform calculations using arithmetic operators, and display results using the printf() function.
Programs like this are essential for beginners because they help build a strong foundation in programming logic. Once you understand these basics, you can move on to more complex programs involving user input, loops, and functions.
Codecrown