Knowledge Required
Diameter of Circle = 2 x radius of circle
circumference of Circle = 2 x 3.14 x radius of circle.
Area of circle = 3.14 x (radius of circle)2
As shown below:
D = 2 r
where,
D = Diameter of circle
C = Circumference of circle
A = Area of circle
r = radius of circle
pi(Ï€) = 3.142., which is constant
Logic to Solve Problem:
1. Take the radius in the form of input from user says in radius variable
2. Apply the above formulae and keep the result in required variable.
3. Finally, prints the resultant values of diameter, circumference and area of the circle in the respective variables.
Full Code to Calculate the diameter, circumference and area of circle.
/*
Program to find out the diameter, circumference and area of a circle of given
radius
*/
#include<stdio.h>
int main()
{
float Radius, DiameterOfCircle, CircumferenceOfCircle, AreaOfCircle;
/*
* Radius is for takes the value of radius in the form of input
* DiameterOfCircle is for putting the value of diameter of circle
* CircumferenceOfCircle is for putting the value of circle
* AreaOfCircle is for putting the value of area of circle
*/
printf("Enter the value of radius of circle\n");
scanf("%f",&Radius); // this takes the value of radius in the Radius variable
// Now calculate Diameter of circle
DiameterOfCircle = 2 * Radius;
// Now calculate the circumference of circle;
CircumferenceOfCircle = 2 * 3.14 * Radius;
//Now calculate the area of circle
AreaOfCircle = 3.14 * Radius * Radius;
// Now print all the values
// Print Diameter Of Circle
printf("Diameter Of Circle = %.2f\n",DiameterOfCircle);
// Print Circumference Of Circle
printf("Circumference Of Circle = %.2f\n",CircumferenceOfCircle);
// Print Area Of Circle
printf("Area Of Circle = %.2f",AreaOfCircle);
return 0;
}
\n is an escape sequence used to change the line in c language.
%f is used as a format specifier because we need to enter float value as value of pi is 3.14. Thus, the resultant value of circumference and area of the circle could be of float value . Therefore, we use %f format specifier.
Please write this code on your own and Run this code and put the values of radius different and see the output of the diameter, circumference and area of circle every time and comment your answer with input and output value.
As, everything is clear in the code, I hope you will be able to write a program how to find the diameter, circumference and area of rectangle in C language. If you have any query related to the above code or with this post. You are always welcome to post your query in the comment box!
0 Comments