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 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 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 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
 */
double diameter(double radius) 
{
    return (2 * radius);
}


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


/**
 * Finds area of the circle whose radius is given
 */
double area(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 ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment