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

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

Input radius: 10

Output

Diameter = 20 units
Circumference = 62.83 units
Area = 314.16 sq. units

Required knowledge

Basic C programming, Functions, Returning value from function

Read more – Program to find diameter, circumference and area of circle without functions.

In previous exercise we learned to declare and use program with single user defined function. Here in this program we will define more than one user-defined functions in a single program.

Declare functions to find diameter, circumference and area of circle

  1. First assign a meaningful name to all the three functions. Say function to calculate diameter, circumference and area are – getDiameter(), getCircumference() and getArea() respectively.
  2. All the above three functions uses one input i.e. radius of circle to calculate output. Hence all the three function must accept a parameter of double or int type.
  3. Finally, all the three functions returns either double or int as output. Hence, return type of the function must be either double or int.

After considering above points function declaration looks like –


double getDiameter(double radius);
double getCircumference(double radius);
double getArea(double radius);

Program to find diameter, circumference and area using functions

/**
 * 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 getDiameter(double radius);
double getCircumference(double radius);
double getArea(double radius);


int main() 
{
    float radius, dia, circ, area;
    
    /* Input radius of circle from user */
    printf("Enter radius of circle: ");
    scanf("%f", &radius);
    
    dia  = getDiameter(radius);       // Call getDiameter function
    circ = getCircumference(radius);  // Call getCircumference function
    area = getArea(radius);           // Call getArea 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", area);
    
    return 0;
}


/**
 * Calculate diameter of circle whose radius is given
 */
double getDiameter(double radius) 
{
    return (2 * radius);
}


/**
 * Calculate circumference of circle whose radius is given
 */
double getCircumference(double radius) 
{
    return (2 * M_PI * radius); // M_PI = PI = 3.14 ... 
}


/**
 * Find area of circle whose radius is given
 */
double getArea(double radius)
{
    return (M_PI * radius * radius); // M_PI = PI = 3.14 ...
}

Output

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 😉