Quantcast
Channel: Codeforwin
Viewing all articles
Browse latest Browse all 132

C program to find diameter, circumference and area of circle using function

$
0
0
Write a C program to input radius of circle from user and find diameter, circumference and area of the given circle using function. How to find diameter, circumference and area of a circle using function in C programming.

Example:
Input radius: 10
Output diameter: 20 units
Output circumference: 62.83 units
Output area: 314.16 sq. units

Required knowledge

Basic C programming, Function

Before moving on to this program also check how to find diameter, circumference and area of a circle without using functions.

Program


/**
* C program to find diameter, circumference and area of a circle using functions
*/

#include <stdio.h>
#include <math.h> //Used for constant PI referred as M_PI


/* Function declaration */
double diameter(double radius);
double circumference(double radius);
double area(double radius);


int main()
{
float radius, dia, circ, ar;

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

dia = diameter(radius); //Calls diameter function
circ = circumference(radius); //Calls circumference function
ar = area(radius); //Calls area function

printf("Diameter of the circle = %.2f units\n", dia);
printf("Circumference of the circle = %.2f units\n", circ);
printf("Area of the circle = %.2f sq. units\n", ar);

return 0;
}



/**
* Finds the diameter of a circle whose radius is given
* @param radius Radius of the circle
* @return Diameter of the circle
*/
double diameter(double radius)
{
return (2 * radius);
}


/**
* Finds circumference of the circle whose radius is given
* @param radius Radius of the circle
* @return Circumference of the circle
*/
double circumference(double radius)
{
return (2 * M_PI * radius); //M_PI = PI = 3.14 ...
}


/**
* Finds area of the circle whose radius is given
* @param radius Radius of the circle
* @return Area of the circle
*/
double area(double radius)
{
return (M_PI * radius * radius); //M_PI = PI = 3.14 ...
}


Output
X
_
Enter radius of the circle: 10
Diameter of the circle = 20.00 units
Circumference of the circle = 62.83 units
Area of the circle = 314.16 sq. units

Happy coding ;)

Viewing all articles
Browse latest Browse all 132

Trending Articles