0% found this document useful (0 votes)
81 views3 pages

Circle

The document discusses calculating the diameter, circumference, and area of a circle given its radius in C programming. It provides the formulas, explains taking user input for the radius, performing the calculations using the radius and mathematical constants, and printing the output. The program code is included that demonstrates implementing this by first calculating without a constant pi value, then rewriting it to use the predefined M_PI constant from the math.h header file.

Uploaded by

Shanmuk_Ch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views3 pages

Circle

The document discusses calculating the diameter, circumference, and area of a circle given its radius in C programming. It provides the formulas, explains taking user input for the radius, performing the calculations using the radius and mathematical constants, and printing the output. The program code is included that demonstrates implementing this by first calculating without a constant pi value, then rewriting it to use the predefined M_PI constant from the math.h header file.

Uploaded by

Shanmuk_Ch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Write a C program to input radius of a circle from user and find diameter,

circumference and area of the circle. How to calculate diameter, circumference and
area of a circle whose radius is given by user in C programming. Logic to find
diameter, circumference and area of a circle in C.

Example
Input
Enter radius: 10
Output

Diameter = 20 units
Circumference = 62.79 units
Area = 314 sq. units
Required knowledge
Arithmetic operators, Data types, Defining constants, Basic input/output

Properties of circle
Diameter, circumference and area of a circle formula is given by -
Circle formulas

Where r is radius of the circle.


Logic to find diameter, circumference and area of circle
Below is the step by step descriptive logic to find diameter, circumference and
area of a circle -

Input radius of circle from user. Store it in a variable say radius.


Apply the formulas to calculate diameter, circumference and area. Use diameter = 2
* radius, circumference = 2 * 3.14 * radius and area = 3.14 * radius * radius.
Print all resultant value diameter, circumference and area.
Let us code the solution.

Program to find diameter, circumference and area of circle


/**
* C program to calculate diameter, circumference and area of circle
*/

#include <stdio.h>

int main()
{
float radius, diameter, circumference, area;

/*
* Input radius of circle from user
*/
printf("Enter radius of circle: ");
scanf("%f", &radius);

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

/*
* Print all results
*/
printf("Diameter of circle = %.2f units \n", diameter);
printf("Circumference of circle = %.2f units \n", circumference);
printf("Area of circle = %.2f sq. units ", area);

return 0;
}
\n is an escape sequence character used to add new line (move to next line).

Important note: The above program contains a constant value 3.14. It is always
recommended to use constant variable to represent such constants. The constant PI
is already defined in math.h header file with name M_PI.

Let us rewrite the above program using constant value.

Program to find diameter, circumference and area of circle using PI constant


/**
* C program to calculate diameter, circumference and area of 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 circle: ");
scanf("%f", &radius);

/*
* Calculate diameter, circumference and area of circle
*/
diameter = 2 * radius;
circumference = 2 * M_PI * radius;
area = M_PI * (radius * radius);

/*
* Print all results
*/
printf("Diameter of circle = %.2f units \n", diameter);
printf("Circumference of circle = %.2f units \n", circumference);
printf("Area of circle = %.2f sq. units ", area);

return 0;
}
%.2f is used to print the fractional value up to two decimal places. You can also
use %f to print up to default 6 decimal places.

Important note: C doesn't provides any exponential operator. Hence never play with
operations like radius ^ 2 to find power or to evaluate exponents.

Read more - Program to find power of any number.

Output
Enter radius of circle: 10
Diameter of circle = 20.00 units
Circumference of circle = 62.79 units
Area of circle = 314.00 sq. units

You might also like