Program to find area, diameter and circumference of a circle
C Program to find area, diameter and circumference of a circle:
Write a C program to find area, diameter, and circumference of a circle by taking radius as an input from user. See the below logic to find area, diameter, and circumference of a circle with step by step.
Required knowledge for this exercise is:
Before writing any program you just imagine about output screen first. For example: what input to be given, and output to be displayed. Let's have a look at the below example:
INPUT:
Enter radius of a circle: 4
OUTPUT:
Area of circle = 50.24 sq. units
Diameter of circle = 8.00 sq. units
Circumference of circle = 25.12 sq. units
Farmula to be used for Circle:
Where, D = diameter | C = Circumference | A = Area | r = Radius | and Value of PI = 3.14
Logic to find area, diameter and circumference of a circle:
1) Print a massage to the user like "Enter radius of a circle:" using printf();
2) Input radius using scanf(); and store it in a variable radius.
3) Now find area, diameter and circumference of a circle using farmula: area = PI * radius * radius,
diameter = 2 * radius
and circumference = 2 * PI * radius.
4) At last print the value of area, diameter and circumference. that's it!
Program to find area, diameter, and circumference of a circle:
#include <stdio.h>
#define PI 3.14
int main()
{
float radius, diameter, circumference, area;
printf("Enter radius of a circle: ");
scanf("%f", &radius);
Since PI has constant value 3.14 so
* by using pre-processor directive #define PI 3.14 .
area = PI * (radius * radius); //
diameter = 2 * radius;
circumference = 2 * PI * radius;
printf("Area of circle = %.2f sq. units ", area);
printf("Diameter of circle = %.2f units \n", diameter);
printf("Circumference of circle = %.2f units \n", circumference);
return 0;
}
Note: #define is preprcessor directive which is used to define canstant value. Such as #define PI 3.14 as you can see in the above program.
Let's rewrite the above program by another way using constant variable M_PI.
Program to find area, diameter, and circumference of a circle:
#include <stdio.h>
#include <math.h>
int main()
{
float radius, diameter, circumference, area;
printf("Enter radius of a circle: ");
scanf("%f", &radius);
Since PI has fixed value 3.14, It is always recommended
* to use constant variable to represent such constant. The constant
in <math.h> header file .
area = M_PI * (radius * radius); //
diameter = 2 * radius;
circumference = 2 * M_PI * radius;
printf("Area of circle = %.2f sq. units ", area);
printf("Diameter of circle = %.2f units \n", diameter);
printf("Circumference of circle = %.2f units \n", circumference);
return 0;
}
Note: M_PI is nothing but a constant variable which is already defined in #include <math.h> deader file
Constant variable: In C programming language, such variables or value which cannot be modified once they are defined. They are fixed values in a program. There can be any types of constant like integer, float, character constant etc.
Output:
- Enter radius of a circle: 4
- Area of circle = 50.24 sq. units
- Diameter of circle = 8.00 units
- Circumference of circle = 25.12 units
In place of constant variable M_PI, You can direcly use 3.14 since the value of PI = 3.14. Have a look at the below statements taken from above program.
-
Since PI has fixed value 3.14, It is always recommended
* to use constant variable to represent such constant. The constant
in <math.h> header file .
area = 3.14 * (radius * radius); //
diameter = 2 * radius;
circumference = 2 * 3.14 * radius;
Next Topic to be updated soon..>>
Help others by sharing this page.
Ahmad Irshad
Author & Editor
I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.