Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Sunday, June 21, 2020

Program to find area, diameter and circumference of a circle


C Program to find area, diameter and circumference of a circle:
By Ahmad Irshad | Programming Exercises and Solutions > find area, diameter and circumference


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:

Circle formulas
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 * radiusand 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

  1. /* C program to find area, diameter, and circumference of a circle */

#include <stdio.h> #define PI 3.14 int main() { float radius, diameter, circumference, area; /* * Input radius of circle from user */ printf("Enter radius of a circle: "); scanf("%f", &radius); /* * Calculate area, diameter and areacircumference of a circle.
* ------------------------------------------------------------------
* Remembeer: Since PI has constant value 3.14 so We need to define
* the value of PI by using pre-processor directive #define PI 3.14 .
*/
area = PI * (radius * radius); // diameter = 2 * radius; circumference = 2 * PI * radius; /* * Print all results */
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

  1. /* C program to find area, diameter, and circumference of a circle */

#include <stdio.h> #include <math.h> // Used for M_PI int main() { float radius, diameter, circumference, area; /* * Input radius of circle from user */ printf("Enter radius of a circle: "); scanf("%f", &radius); /* * Calculate area, diameter and areacircumference of a circle.
* ------------------------------------------------------------------
* Remembeer: Since PI has fixed value 3.14, It is always recommended
* to use constant variable to represent such constant. The constant
* PI is already defined in <math.h> header file with name M_PI.
*/
area = M_PI * (radius * radius); // diameter = 2 * radius; circumference = 2 * M_PI * radius; /* * Print all results */
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.
Visit to know more about constant variable.  


Output:

  1. Enter radius of a circle: 4
  1. Area of circle = 50.24 sq. units
  2. Diameter of circle = 8.00 units
  3. 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. 

  1. /*
* Calculate area, diameter and areacircumference of a circle.
* ------------------------------------------------------------
* Remembeer: Since PI has fixed value 3.14, It is always recommended
* to use constant variable to represent such constant. The constant
* PI is already defined in <math.h> header file with name M_PI.
*/

area = 3.14 * (radius * radius); // diameter = 2 * radius; circumference = 2 * 3.14 * radius;





Next Topic to be updated soon..>>




Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

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.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us